From c47c9488fee504e67529e0013f20d41633e7af59 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Tue, 12 Apr 2016 17:15:43 +0300 Subject: [PATCH] Return string from context --- src/kemal/dsl.cr | 2 +- src/kemal/route.cr | 8 +++++--- src/kemal/route_handler.cr | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/kemal/dsl.cr b/src/kemal/dsl.cr index 6b5d589..9ede832 100644 --- a/src/kemal/dsl.cr +++ b/src/kemal/dsl.cr @@ -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 %} diff --git a/src/kemal/route.cr b/src/kemal/route.cr index f73e0b9..38b9668 100644 --- a/src/kemal/route.cr +++ b/src/kemal/route.cr @@ -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 diff --git a/src/kemal/route_handler.cr b/src/kemal/route_handler.cr index 6481f90..5ced2e2 100644 --- a/src/kemal/route_handler.cr +++ b/src/kemal/route_handler.cr @@ -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