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
spec/all_spec.cr
Normal file
1
spec/all_spec.cr
Normal file
|
@ -0,0 +1 @@
|
||||||
|
require "./*"
|
13
spec/frank_handler_spec.cr
Normal file
13
spec/frank_handler_spec.cr
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe "Frank::Handler" do
|
||||||
|
it "routes" do
|
||||||
|
frank = Frank::Handler.new
|
||||||
|
frank.add_route "GET", "/" do
|
||||||
|
"hello"
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/")
|
||||||
|
response = frank.call(request)
|
||||||
|
response.body.should eq("hello")
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,15 +2,20 @@ require "spec_helper"
|
||||||
|
|
||||||
describe "Route" do
|
describe "Route" do
|
||||||
describe "match" do
|
describe "match" do
|
||||||
it "doesn't match" do
|
it "doesn't match because of route" do
|
||||||
route = Route.new("/foo/bar", nil)
|
route = Route.new("GET", "/foo/bar") { "" }
|
||||||
route.match(nil, "/foo/baz".split("/")).should be_nil
|
route.match("GET", "/foo/baz".split("/")).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't match because of method" do
|
||||||
|
route = Route.new("GET", "/foo/bar") { "" }
|
||||||
|
route.match("POST", "/foo/bar".split("/")).should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "matches" do
|
it "matches" do
|
||||||
route = Route.new("/foo/:one/path/:two", nil)
|
route = Route.new("GET", "/foo/:one/path/:two") { "" }
|
||||||
request = route.match(nil, "/foo/uno/path/dos".split("/"))
|
params = route.match("GET", "/foo/uno/path/dos".split("/"))
|
||||||
request.not_nil!.params.should eq({"one" => "uno", "two" => "dos"})
|
params.should eq({"one" => "uno", "two" => "dos"})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
def get(path, &block : Frank::Request -> String)
|
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
|
end
|
||||||
|
|
|
@ -19,15 +19,16 @@ class Frank::Handler < HTTP::Handler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_route(path, handler)
|
def add_route(method, path, &handler : Frank::Request -> String)
|
||||||
@routes << Route.new(path, handler)
|
@routes << Route.new(method, path, &handler)
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec_request(request)
|
def exec_request(request)
|
||||||
components = request.path.split "/"
|
components = request.path.split "/"
|
||||||
@routes.each do |route|
|
@routes.each do |route|
|
||||||
frank_request = route.match(request, components)
|
params = route.match(request.method, components)
|
||||||
if frank_request
|
if params
|
||||||
|
frank_request = Request.new(params)
|
||||||
return route.handler.call(frank_request)
|
return route.handler.call(frank_request)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
class Frank::Route
|
class Frank::Route
|
||||||
getter handler
|
getter handler
|
||||||
|
|
||||||
def initialize(path, @handler)
|
def initialize(@method, path, &@handler : Frank::Request -> String)
|
||||||
@components = path.split "/"
|
@components = path.split "/"
|
||||||
end
|
end
|
||||||
|
|
||||||
def match(request, components)
|
def match(method, components)
|
||||||
|
return nil unless method == @method
|
||||||
return nil unless components.length == @components.length
|
return nil unless components.length == @components.length
|
||||||
|
|
||||||
params = nil
|
params = nil
|
||||||
|
@ -20,6 +21,6 @@ class Frank::Route
|
||||||
end
|
end
|
||||||
|
|
||||||
params ||= {} of String => String
|
params ||= {} of String => String
|
||||||
Request.new(params)
|
params
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue