mux
A simple http router for crystal. No fancy methods just a simple
frontend for luislavena/radix
.
Installation
-
Add the dependency to your
shard.yml
:dependencies: mux: github: threez/mux
-
Run
shards install
Usage
require "mux"
mux = Mux::Router.new
mux.get "/foo/:id" do |context|
context.response.content_type = "text/plain"
context.response.print "Hello bar, got #{context.request.path} .. #{context.request.path_param[:id]}!"
end
mux.post "/" do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world, got #{context.request.path}!"
end
server = HTTP::Server.new(mux)
puts "Listening on http://127.0.0.1:8080"
server.listen(8080)
Handler Class
Adding a regular handler (not HandlerProc) can be done the same way:
class Foo
include HTTP::Handler
def call(context)
context.response.content_type = "text/plain"
context.response.print "Hello foo, got #{context.request.path}!"
end
end
# using add handler for all methods
mux.add_handler("/all/*", Foo.new)
# or ...
mux.post "/all/*", Foo.new
Contributing
- Fork it (https://github.com/threez/mux/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
- Vincent Landgraf - creator and maintainer