mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Return string from context
This commit is contained in:
parent
05e44e68c6
commit
c47c9488fe
3 changed files with 8 additions and 6 deletions
|
@ -1,7 +1,7 @@
|
||||||
HTTP_METHODS = %w(get post put patch delete options)
|
HTTP_METHODS = %w(get post put patch delete options)
|
||||||
|
|
||||||
{% for method in HTTP_METHODS %}
|
{% 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)
|
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
|
@ -2,9 +2,11 @@
|
||||||
# It takes 3 parameters: Method, path and a block to specify
|
# It takes 3 parameters: Method, path and a block to specify
|
||||||
# what action to be done if the route is matched.
|
# what action to be done if the route is matched.
|
||||||
class Kemal::Route
|
class Kemal::Route
|
||||||
getter handler : (HTTP::Server::Context -> )
|
getter handler
|
||||||
getter method
|
@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
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,7 +19,7 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
|
|
||||||
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
||||||
# a corresponding `HEAD` route.
|
# 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 method, path, Route.new(method, path, &handler)
|
||||||
add_to_radix_tree("HEAD", path, Route.new("HEAD", path, &handler)) if method == "GET"
|
add_to_radix_tree("HEAD", path, Route.new("HEAD", path, &handler)) if method == "GET"
|
||||||
end
|
end
|
||||||
|
@ -33,7 +33,7 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
def process_request(context)
|
def process_request(context)
|
||||||
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
||||||
route = context.route_lookup.payload as Route
|
route = context.route_lookup.payload as Route
|
||||||
context.response.print(route.handler.call(context).to_s)
|
context.response.print(route.handler.call(context))
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue