http_router
A simple and fast HTTP server router. Combines radix trees with the Crystal HTTP server.
Installation
-
Add the dependency to your
shard.yml
:dependencies: http_router: github: data-niklas/http_router
-
Run
shards install
Usage
require "http_router"
router = HTTP::Router.new
router.get "/test/:test/123" { |context|
puts "Called",context.request.path
context.response.print "Hello there"
}
router.post "/test" { |context|
puts "Called",context.request.path
context.response.print "Called /test with the post method"
}
router.bind_tcp 8080
router.listen
Other methods like put, patch, head, ... are available.
The HTTP::Router extends the HTTP::Server, so the default server code will work.
"/test/:sth/123" will match any url, which contains something between /test/ and 123,
so e.g.: "/test/testing/123
Router can be started blocking or non blocking. The above code blocks. Below is a non blocking example:
proc = router.listen_non_blocking
# do something
spawn do
# Block the fiber until the server is closed
proc.call
puts "Server closed"
end
sleep 10
router.close
sleep 1
puts "Will exit!"
TODO Write usage instructions here
Development
TODO Write development instructions here
Contributing
- Fork it (https://github.com/data-niklas/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
- Niklas Loeser - creator and maintainer