Awesome Open Source
Awesome Open Source


Built with Crystal Build status API Docs Releases Awesome Patrons count Gitter chat

The requests dispatcher shard for Crystal.


Thanks to all my patrons, I can continue working on beautiful Open Source Software! 🙏

Lauri Jutila, Alexander Maslov, Dainel Vera

You can become a patron too in exchange of prioritized support and other perks

Become Patron


HTTP::Multiserver dispatches a server request to another vanilla HTTP::Server depending on its path similar to Ruby's Rack::URLMap.


Add this to your application's shard.yml:

    github: vladfaust/
    version: ~> 0.2.0

This shard follows Semantic Versioning 2.0.0, so see releases and change the version accordingly.


Basic example

require "http-multiserver"

simple_server =[]) do |context|
  context.response.print("Hello from Simple Server!")

# For example purposes
resque =

multiserver ={
  "/resque" => resque,
  "/"       => simple_server,
}, []) do |context|
  # This is an optional custom fallback handler; by default returns "404 Not Found"
  context.response.status_code = 418
  context.response.print("☕ #{context.request.path} not found")


HTTP::Multiserver extends from a regular HTTP::Server, so it CAN have its own handlers. Same for underlying servers, they obviously CAN have their own handlers too.


Mapping relies on either String or Regex keys; when using String as a key, a slash is automatically appended to it.

Assuming that this map is passed to HTTP::Multiserver initializer:

map = {
  "/foo"   => foo_server,
  %r{/bar} => bar_server,
  "/"      => fallback_server

multiserver =, map)

This is how the request is dispatched under the hood (simplified):

internal_map = {
  %r{^/foo/} => foo_server,
  %r{/bar}   => bar_server,
  %r{^/}     => fallback_server

path = request.path.append_slash # "/foo/abc" turns into "foo/abc/"
server = internal_map.find { |regex, _| regex.match(path) }
server ? : not_found

Please see specs for dispatching specifications.


As mentioned above, HTTP::Multiserver is just a regular HTTP::Server which dispatches the handling to underlying servers based on super-fast Regex.match. A very tiny impact on the performance is expected if any.


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Crystal (3,372
Router (2,091
Handler (345
Dispatcher (175
Mount (138
Related Projects