class Kemal::Router

Overview

Router provides modular routing capabilities for Kemal applications.

It allows grouping routes under a common prefix and applying filters to specific route groups. Routers can be nested using #namespace.

Example

api = Kemal::Router.new

api.before do |env|
  env.response.content_type = "application/json"
end

api.get "/users" do |env|
  User.all.to_json
end

api.namespace "/admin" do
  get "/dashboard" do |env|
    {status: "ok"}.to_json
  end
end

mount "/api/v1", api

Defined in:

kemal/router.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(prefix : String = "") #

[View source]

Instance Method Detail

def after(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for all HTTP methods.

router.after do |env|
  puts "Request completed"
end

[View source]
def after_all(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for ALL requests.


[View source]
def after_delete(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for DELETE requests.


[View source]
def after_get(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for GET requests.


[View source]
def after_options(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for OPTIONS requests.


[View source]
def after_patch(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for PATCH requests.


[View source]
def after_post(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for POST requests.


[View source]
def after_put(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines an after filter for PUT requests.


[View source]
def before(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for all HTTP methods.

router.before do |env|
  env.response.content_type = "application/json"
end

[View source]
def before_all(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for ALL requests.


[View source]
def before_delete(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for DELETE requests.


[View source]
def before_get(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for GET requests.


[View source]
def before_options(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for OPTIONS requests.


[View source]
def before_patch(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for PATCH requests.


[View source]
def before_post(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for POST requests.


[View source]
def before_put(path : String = "*", &block : HTTP::Server::Context -> _) #

Defines a before filter for PUT requests.


[View source]
def delete(path : String, &block : HTTP::Server::Context -> _) #

Defines a DELETE route.

router.delete "/path" do |env|
  "response"
end

[View source]
def get(path : String, &block : HTTP::Server::Context -> _) #

Defines a GET route.

router.get "/path" do |env|
  "response"
end

[View source]
def mount(path : String, router : Router) #

Mounts another router at the given path prefix.

NOTE The path must start with a /.

users_router = Kemal::Router.new
users_router.get "/" { |env| "users" }

api = Kemal::Router.new
api.mount "/users", users_router

mount "/api", api
# Result: GET /api/users

[View source]
def mount(router : Router) #

Mounts another router without additional prefix.


[View source]
def namespace(path : String, &) #

Creates a nested namespace/group with the given path prefix.

NOTE The path must start with a /.

All routes defined inside the block will be prefixed with the given path.

router.namespace "/users" do
  get "/" do |env|
    User.all.to_json
  end

  get "/:id" do |env|
    User.find(env.params.url["id"]).to_json
  end
end

[View source]
def options(path : String, &block : HTTP::Server::Context -> _) #

Defines a OPTIONS route.

router.options "/path" do |env|
  "response"
end

[View source]
def patch(path : String, &block : HTTP::Server::Context -> _) #

Defines a PATCH route.

router.patch "/path" do |env|
  "response"
end

[View source]
def post(path : String, &block : HTTP::Server::Context -> _) #

Defines a POST route.

router.post "/path" do |env|
  "response"
end

[View source]
def prefix : String #

[View source]
def put(path : String, &block : HTTP::Server::Context -> _) #

Defines a PUT route.

router.put "/path" do |env|
  "response"
end

[View source]
def register_routes(base_prefix : String = "") #

Registers all routes, filters, and websockets with Kemal's handlers. This is called automatically when using #mount from DSL.

:nodoc:


[View source]
def ws(path : String, &block : HTTP::WebSocket, HTTP::Server::Context -> ) #

Defines a WebSocket route.

router.ws "/chat" do |socket, env|
  socket.on_message do |msg|
    socket.send "Echo: #{msg}"
  end
end

[View source]