http-router
Simple HTTP Router
Installation
-
Add the dependency to your
shard.yml
:dependencies: http-router: github: willhbr/http-router
-
Run
shards install
Usage
require "http/server"
require "http-router"
class MyHander
include HTTP::Handler
include HTTP::Router
@[HTTP::Route(path: "/stuff")]
@[HTTP::Route(path: "/stuff", method: :HEAD)]
def get_stuff(context)
context.ok_json(result: "success!")
end
@[HTTP::Route(path: "/stuff", method: :POST)]
def post_stuff(context)
contents = context.request.body.get_to_end
puts contents
context.ok_json(result: "success!")
end
end
This will generate a call(context)
method like this:
def call(context : HTTP::Server::Context)
req = context.request
case { req.method, req.path }
when { "GET", "/stuff" }
self.get_stuff(context)
when { "HEAD", "/stuff" }
self.get_stuff(context)
when { "POST", "/stuff" }
self.post_stuff(context)
else
call_next context
end
end
http-router
doesn't support any destructuring of the path. Maybe you could send a PR for that.
Contributing
- Fork it (https://github.com/willhbr/http-router/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Will Richardson - creator and maintainer
Repository
http-router
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- 16 days ago
- December 6, 2024
License
MIT License
Links
Synced at
Sun, 22 Dec 2024 13:50:12 GMT
Languages