class

Kemal::Router

Inherits Reference < Object

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

Constructors

new(prefix : String = "")
Source

Instance methods

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

Defines an after filter for all HTTP methods.

router.after do |env|
  puts "Request completed"
end
Source
after_all(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for ALL requests.

Source
after_delete(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for DELETE requests.

Source
after_get(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for GET requests.

Source
after_options(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for OPTIONS requests.

Source
after_patch(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for PATCH requests.

Source
after_post(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for POST requests.

Source
after_put(path : String = "*", &block : HTTP::Server::Context -> _)

Defines an after filter for PUT requests.

Source
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
Source
before_all(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for ALL requests.

Source
before_delete(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for DELETE requests.

Source
before_get(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for GET requests.

Source
before_options(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for OPTIONS requests.

Source
before_patch(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for PATCH requests.

Source
before_post(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for POST requests.

Source
before_put(path : String = "*", &block : HTTP::Server::Context -> _)

Defines a before filter for PUT requests.

Source
delete(path : String, &block : HTTP::Server::Context -> _)

Defines a DELETE route.

router.delete "/path" do |env|
  "response"
end
Source
get(path : String, &block : HTTP::Server::Context -> _)

Defines a GET route.

router.get "/path" do |env|
  "response"
end
Source
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
Source
mount(router : Router)

Mounts another router without additional prefix.

Source
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
Source
options(path : String, &block : HTTP::Server::Context -> _)

Defines a OPTIONS route.

router.options "/path" do |env|
  "response"
end
Source
patch(path : String, &block : HTTP::Server::Context -> _)

Defines a PATCH route.

router.patch "/path" do |env|
  "response"
end
Source
post(path : String, &block : HTTP::Server::Context -> _)

Defines a POST route.

router.post "/path" do |env|
  "response"
end
Source
prefix
Source
put(path : String, &block : HTTP::Server::Context -> _)

Defines a PUT route.

router.put "/path" do |env|
  "response"
end
Source
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:

Source
sse(path : String, &block : EventStream, HTTP::Server::Context -> )

Defines a Server-Sent Events (SSE) route.

router.sse "/events" do |stream, env|
  stream.send("hello")
end
Source
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
Source

Nested types