class Azu::Router

Overview

Defines an Azu Router

The router provides a set of methods for mapping routes that dispatch to specific endpoints or handers. For example

MyAppWeb.router do
  root :web, ExampleApp::HelloWorld
  ws "/hi", ExampleApp::ExampleChannel

  routes :web, "/test" do
    get "/hello/", ExampleApp::HelloWorld
    get "/hello/:name", ExampleApp::HtmlEndpoint
    get "/hello/json", ExampleApp::JsonEndpoint
  end
end

You can use most common HTTP verbs: GET, POST, PUT, PATCH, DELETE, TRACE and OPTIONS.

endpoint = ->(env) { [200, {}, ['Hello from Hanami!']] }

get     '/hello', to: endpoint
post    '/hello', to: endpoint
put     '/hello', to: endpoint
patch   '/hello', to: endpoint
delete  '/hello', to: endpoint
trace   '/hello', to: endpoint
options '/hello', to: endpoint

Defined in:

azu/router.cr

Constant Summary

METHOD_OVERRIDE = "_method"
RADIX = Radix::Tree(Route).new
RESOURCES = ["connect", "delete", "get", "head", "options", "patch", "post", "put", "trace"] of ::String

Instance Method Summary

Instance Method Detail

def add(path : Path, endpoint : HTTP::Handler, method : Method = Method::Any) #

Registers a route for a given path

add path: '/proc', endpoint: ->(env) { [200, {}, ['Hello from Hanami!']] }, method: Method::Get
add path: '/endpoint',   endpoint: Handler.new, method: Method::Get

[View source]
def connect(path : Router::Path, handler : HTTP::Handler) #

[View source]
def delete(path : Router::Path, handler : HTTP::Handler) #

[View source]
def get(path : Router::Path, handler : HTTP::Handler) #

[View source]
def head(path : Router::Path, handler : HTTP::Handler) #

[View source]
def options(path : Router::Path, handler : HTTP::Handler) #

[View source]
def patch(path : Router::Path, handler : HTTP::Handler) #

[View source]
def post(path : Router::Path, handler : HTTP::Handler) #

[View source]
def process(context : HTTP::Server::Context) #

[View source]
def put(path : Router::Path, handler : HTTP::Handler) #

[View source]
def root(endpoint : HTTP::Handler) #

Registers the main route of the application

root :web, ExampleApp::HelloWorld

[View source]
def routes(scope : String = "", &) #

Adds scoped routes


[View source]
def trace(path : Router::Path, handler : HTTP::Handler) #

[View source]
def ws(path : String, channel : Channel.class) #

Registers a websocket route

ws "/hi", ExampleApp::ExampleChannel

[View source]