class Amber::Router::Router

Overview

This is the main application handler all routers should finally hit this handler.

Defined in:

amber/router/router.cr

Constant Summary

PATH_EXT_REGEX = Controller::Helpers::Responders::Content::TYPE_EXT_REGEX

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def add(route : Route) #

[View source]
def add_socket_route(route, handler : WebSockets::Server::Handler) #

[View source]
def all_routes : Array(RouteInfo) #

Returns all registered routes as RouteInfo structs for introspection.


[View source]
def draw(valve : Symbol, namespace : String, &) #

[View source]
def draw(valve : Symbol, &) #

This registers all the routes for the application


[View source]
def get_socket_handler(request) : WebSockets::Server::Handler #

[View source]
def match(http_verb, resource, request : HTTP::Request | Nil = nil) : RoutedResult(Route) #

Matches a request path to a route. Within one lookup, routes compete in the order they were drawn. A path that ends in a known response extension may take two lookups, and the first that qualifies wins, so a later get "/overview.md" answers /overview.md ahead of an earlier get "/overview".

A path ending in a known response extension (.json, .md, ...) is matched in this order:

  1. the full path, when the route it reaches spells the extension as the fixed end of its own path (get "/overview.md", get "/overview(.md)") and that route's request constraint, if any, accepts request;
  2. the path without the extension (/users/1.json matches /users/:id, and the controller responds in that format);
  3. the full path as written.

Step 1 runs only when some drawn route spells a known extension, so an app without one matches exactly as before. A declared route whose request constraint rejected request in step 1 is not offered again, neither in step 3 nor in step 2 through another expansion of the same route (get "/notes(.md)" also matches /notes). A different route that matches the path without the extension still answers; when none does, the path is not found.


[View source]
def match_by_controller_action(controller, action) #

[View source]
def match_by_name(name : Symbol) : Route | Nil #

Looks up a named route by its symbol name.


[View source]
def match_by_request(request) #

[View source]
def route_defined?(request) #

[View source]
def route_table : String #

Returns a formatted route table string suitable for display.


[View source]
def route_table_for(routes : Array(RouteInfo)) : String #

Returns a formatted route table string for a given list of routes.


[View source]

[View source]
def routes=(routes : Amber::Router::RouteSet(Amber::Route)) #

[View source]
def routes_hash : Hash(String, Amber::Route) #

[View source]
def routes_hash=(routes_hash : Hash(String, Amber::Route)) #

[View source]
def scope : Amber::Router::Scope #

[View source]
def scope=(scope : Amber::Router::Scope) #

[View source]
def socket_route_defined?(request) #

[View source]
def socket_routes : Array({path: String, handler: Amber::WebSockets::Server::Handler}) #

[View source]
def socket_routes=(socket_routes : Array(NamedTuple(path: String, handler: Amber::WebSockets::Server::Handler))) #

[View source]