mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Receive Frank::Context and allow setting content-type
This commit is contained in:
parent
fc4f648c1a
commit
889debfd59
6 changed files with 41 additions and 14 deletions
|
@ -10,4 +10,14 @@ describe "Frank::Handler" do
|
|||
response = frank.call(request)
|
||||
response.body.should eq("hello")
|
||||
end
|
||||
|
||||
it "sets content type" do
|
||||
frank = Frank::Handler.new
|
||||
frank.add_route "GET", "/" do |env|
|
||||
env.response.content_type = "application/json"
|
||||
end
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
response = frank.call(request)
|
||||
response.headers["Content-Type"].should eq("application/json")
|
||||
end
|
||||
end
|
||||
|
|
14
src/frank/context.cr
Normal file
14
src/frank/context.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
class Frank::Context
|
||||
getter request
|
||||
|
||||
def initialize(@request)
|
||||
end
|
||||
|
||||
def response
|
||||
@response ||= Response.new
|
||||
end
|
||||
|
||||
def response?
|
||||
@response
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
def get(path, &block : Frank::Request -> String)
|
||||
def get(path, &block : Frank::Context -> _)
|
||||
Frank::Handler::INSTANCE.add_route("GET", path, &block)
|
||||
end
|
||||
|
||||
def post(path, &block : Frank::Request -> String)
|
||||
def post(path, &block : Frank::Context -> _)
|
||||
Frank::Handler::INSTANCE.add_route("POST", path, &block)
|
||||
end
|
||||
|
|
|
@ -8,18 +8,11 @@ class Frank::Handler < HTTP::Handler
|
|||
end
|
||||
|
||||
def call(request)
|
||||
if body = exec_request(request)
|
||||
begin
|
||||
HTTP::Response.new("HTTP/1.1", 200, "OK", {"Content-Type" => "text/plain"}, body)
|
||||
rescue ex
|
||||
HTTP::Response.new("HTTP/1.1", 500, "Internal Server Error", {"Content-Type" => "text/plain"}, ex.to_s)
|
||||
end
|
||||
else
|
||||
call_next(request)
|
||||
end
|
||||
response = exec_request(request)
|
||||
response || call_next(request)
|
||||
end
|
||||
|
||||
def add_route(method, path, &handler : Frank::Request -> String)
|
||||
def add_route(method, path, &handler : Frank::Context -> _)
|
||||
@routes << Route.new(method, path, &handler)
|
||||
end
|
||||
|
||||
|
@ -29,7 +22,14 @@ class Frank::Handler < HTTP::Handler
|
|||
params = route.match(request.method, components)
|
||||
if params
|
||||
frank_request = Request.new(params)
|
||||
return route.handler.call(frank_request)
|
||||
context = Context.new(frank_request)
|
||||
begin
|
||||
body = route.handler.call(context).to_s
|
||||
content_type = context.response?.try(&.content_type) || "text/plain"
|
||||
return HTTP::Response.new("HTTP/1.1", 200, "OK", {"Content-Type" => content_type}, body)
|
||||
rescue ex
|
||||
return HTTP::Response.new("HTTP/1.1", 500, "Internal Server Error", {"Content-Type" => "text/plain"}, ex.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
nil
|
||||
|
|
3
src/frank/response.cr
Normal file
3
src/frank/response.cr
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Frank::Response
|
||||
property content_type
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
class Frank::Route
|
||||
getter handler
|
||||
|
||||
def initialize(@method, path, &@handler : Frank::Request -> String)
|
||||
def initialize(@method, path, &@handler : Frank::Context -> _)
|
||||
@components = path.split "/"
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue