Archived project. No maintenance.
This project is not maintained anymore and is archived. Feel free to fork and make your own changes if needed.
Simple router implementation w/http handler for Crystal, named after "Router Man" - William Yeager. It supports basic router requirements with speed.
While the Yeager::Router
provides a basic router functionality, Yeager::App
aims to provide similar interface with Express.js 4.x on top of
Yeager::Router
and built-in HTTP
module.
Add this to your application's shard.yml
:
dependencies:
yeager:
github: gokmen/yeager
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
require "yeager"
# Create the app
app = Yeager::App.new
# Add a glob handler to call before everything else
# will print "A new visit!" for each request
app.get "*" do |req, res, continue|
puts "A new visit!"
continue.call
end
# Add GET handler for "/" to response back with "Hello world!"
app.get "/" do |req, res|
res.send "Hello world!"
end
# Redirect GET requests to "/google" to https://google.com
app.get "/google" do |req, res|
res.redirect "https://google.com"
end
# Response with JSON on GET requests to "/json"
app.get "/json" do |req, res|
res.status(200).json({"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
# Enable CORS
app.use do |req, res, continue|
res.headers.add "Access-Control-Allow-Origin", "*"
continue.call
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 advanced examples and documentation can be accessed from here.
- 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
- Gokmen Goksel - creator, maintainer