Use macro to generate DSL and support delete,patch verbs
This commit is contained in:
parent
f2e385801f
commit
fa00265345
3 changed files with 8 additions and 8 deletions
|
@ -37,7 +37,7 @@ dependencies:
|
||||||
|
|
||||||
## Status
|
## Status
|
||||||
|
|
||||||
Basic `get`, `put`, `post` and `head` routes can be matched, and request parameters can be obtained.
|
Basic `get`, `put`, `post`, `patch`, `delete` and `head` routes can be matched, and request parameters can be obtained.
|
||||||
|
|
||||||
## Thanks
|
## Thanks
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
def get(path, &block : Kemal::Context -> _)
|
HTTP_METHODS = %w(get post put patch delete)
|
||||||
Kemal::Handler::INSTANCE.add_route("GET", path, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def post(path, &block : Kemal::Context -> _)
|
{% for method in HTTP_METHODS %}
|
||||||
Kemal::Handler::INSTANCE.add_route("POST", path, &block)
|
def {{method.id}}(path, &block : Kemal::Context -> _)
|
||||||
end
|
Kemal::Handler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||||
|
end
|
||||||
|
{% end %}
|
||||||
|
|
|
@ -17,7 +17,7 @@ class Kemal::Handler < HTTP::Handler
|
||||||
@routes << Route.new(method, path, &handler)
|
@routes << Route.new(method, path, &handler)
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec_request(request)
|
def exec_request(request)
|
||||||
components = request.path.not_nil!.split "/"
|
components = request.path.not_nil!.split "/"
|
||||||
@routes.each do |route|
|
@routes.each do |route|
|
||||||
params = route.match(request.method, components)
|
params = route.match(request.method, components)
|
||||||
|
|
Loading…
Reference in a new issue