router

The default web server of the Crystal is quite good :smile: but it weak at routing :cry:.
Kemal is an awesome defacto standard web framework for Crystal :smile:, but it's too fat for some purpose :cry:.
router.cr is a minimum but High Performance middleware for Crystal web server.
See the amazing performance of router.cr here.:rocket:
Installation
Add this to your application's shard.yml
:
dependencies:
router:
github: tbrand/router.cr
Usage
Basic usage
require "router"
Include Router
to utilize router.cr.
class WebServer
include Router
end
In the following sample codes, class WebServer ... end
will be omitted.
To initialize RouteHandler
@route_handler = RouteHandler.new
To define API, call API.new with context
and params
(optional) where context is HTTP::Server::Context and params is Hash(String, String). All APIs have to return the context at end of the method. In this example, params is omitted. (The usage of params is later)
@index = API.new do |context|
context.response.print "Hello router.cr"
context # returning context
end
Define your routes in a draw
block.
draw(@route_handler) do # Draw routes
get "/", @index
end
To activate the routes
def run
server = HTTP::Server.new(3000, @route_handler) # Set RouteHandler to your server
server.listen
end
Finally, run your server.
web_server = WebServer.new
web_server.run
See sample and tips for details.
Path parameters
params
is a Hash(String, String) that is used when you define a path parameters such as /user/:id
(:id
is a parameters). Here is an example.
class WebServer
@route_handler = RouteHandler.new
@user = API.new do |context, params|
context.response.print params["id"] # get :id in url from params
context
end
def initialize
draw(@route_handler) do
get "/user/:id", @user
end
end
end
params
also includes query params such as /user?id=3
. Here is an example.
class WebServer
@route_handler = RouteHandler.new
@user = API.new do |context, params|
response_body = "user: "
# Get a query param like /user?id=3
response_body += params["id"] if params.has_key?("id")
context.response.print response_body
context
end
def initialize
draw(@route_handler) do
get "/user", @user
end
end
end
See sample and tips for details.
Contributing
- Fork it ( https://github.com/tbrand/router.cr/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
- tbrand Taichiro Suzuki - creator, maintainer