Yeager
Simple router implementation w/ http server for Crystal, named after "Router Man" - William Yeager. It supports basic router requirements with speed but not battle-tested.
Installation
Add this to your application's shard.yml
:
dependencies:
yeager:
github: gokmen/yeager
Usage
Router only
require "yeager"
# Create router instance
router = Yeager::Router.new
# Define your routes
router.add "/foo"
router.add "/foo/:hello"
# Run a route on router which will return nil or an
# Hash(Symbol | String => String) if there is a match
router.run "/foo" # -> {:path => "/foo"}
router.run "/foo/world" # -> {"hello" => "world", :path => "/foo/:hello"}
router.run "/bar" # -> nil
WebServer with Yeager::App
require "yeager"
# Create the app
app = Yeager::App.new
# Add GET handler for "/" to response back with "Hello world!"
app.get "/" do |req, res|
res.send "Hello world!"
end
# Add another GET handler for "/:user"
# which will render "Hello yeager!" for "/yeager" route
app.get "/:user" do |req, res|
res.send "Hello #{req.params["user"]}!"
end
# Start the app on port 3000
app.listen 3000 do
print "Example app listening on 0.0.0.0:3000!"
end
You can checkout specs for more examples and documentation can be accessed from here.
Todo
Router
- Add optional argument support like
/foo/:bar?
- Add glob support like
/foo/*
App
- Add middleware support
- ~~Add chain handlers support with a.k.a
next
~~ - Add parse form data support
- Handle WebSocket requests
Contributing
- Fork it ( https://github.com/gokmen/yeager/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
- Gokmen Goksel - creator, maintainer