Return string from context

This commit is contained in:
sdogruyol 2016-04-12 17:15:43 +03:00
parent 05e44e68c6
commit c47c9488fe
3 changed files with 8 additions and 6 deletions

View File

@ -1,7 +1,7 @@
HTTP_METHODS = %w(get post put patch delete options)
{% for method in HTTP_METHODS %}
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
def {{method.id}}(path, &block : HTTP::Server::Context -> String)
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
end
{% end %}

View File

@ -2,9 +2,11 @@
# It takes 3 parameters: Method, path and a block to specify
# what action to be done if the route is matched.
class Kemal::Route
getter handler : (HTTP::Server::Context -> )
getter method
getter handler
@handler : HTTP::Server::Context -> String
@method : String
def initialize(@method : String, @path : String, &@handler : HTTP::Server::Context -> )
def initialize(@method, @path : String, &handler )
@handler = ->(context : HTTP::Server::Context){ handler.call(context).to_s }
end
end

View File

@ -19,7 +19,7 @@ class Kemal::RouteHandler < HTTP::Handler
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
# a corresponding `HEAD` route.
def add_route(method, path, &handler : HTTP::Server::Context -> _)
def add_route(method, path, &handler : HTTP::Server::Context -> String)
add_to_radix_tree method, path, Route.new(method, path, &handler)
add_to_radix_tree("HEAD", path, Route.new("HEAD", path, &handler)) if method == "GET"
end
@ -33,7 +33,7 @@ class Kemal::RouteHandler < HTTP::Handler
def process_request(context)
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
route = context.route_lookup.payload as Route
context.response.print(route.handler.call(context).to_s)
context.response.print(route.handler.call(context))
context
end