mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Handle POST request, and some refactors
This commit is contained in:
parent
e601730a0c
commit
fc4f648c1a
6 changed files with 39 additions and 14 deletions
|
@ -1,3 +1,7 @@
|
|||
def get(path, &block : Frank::Request -> String)
|
||||
Frank::Handler::INSTANCE.add_route(path, block)
|
||||
Frank::Handler::INSTANCE.add_route("GET", path, &block)
|
||||
end
|
||||
|
||||
def post(path, &block : Frank::Request -> String)
|
||||
Frank::Handler::INSTANCE.add_route("POST", path, &block)
|
||||
end
|
||||
|
|
|
@ -19,15 +19,16 @@ class Frank::Handler < HTTP::Handler
|
|||
end
|
||||
end
|
||||
|
||||
def add_route(path, handler)
|
||||
@routes << Route.new(path, handler)
|
||||
def add_route(method, path, &handler : Frank::Request -> String)
|
||||
@routes << Route.new(method, path, &handler)
|
||||
end
|
||||
|
||||
def exec_request(request)
|
||||
components = request.path.split "/"
|
||||
@routes.each do |route|
|
||||
frank_request = route.match(request, components)
|
||||
if frank_request
|
||||
params = route.match(request.method, components)
|
||||
if params
|
||||
frank_request = Request.new(params)
|
||||
return route.handler.call(frank_request)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
class Frank::Route
|
||||
getter handler
|
||||
|
||||
def initialize(path, @handler)
|
||||
def initialize(@method, path, &@handler : Frank::Request -> String)
|
||||
@components = path.split "/"
|
||||
end
|
||||
|
||||
def match(request, components)
|
||||
def match(method, components)
|
||||
return nil unless method == @method
|
||||
return nil unless components.length == @components.length
|
||||
|
||||
params = nil
|
||||
|
@ -20,6 +21,6 @@ class Frank::Route
|
|||
end
|
||||
|
||||
params ||= {} of String => String
|
||||
Request.new(params)
|
||||
params
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue