Router

The router handles HTTP request routing using a radix tree for high performance.

Route Registration

Routes are automatically registered when defining endpoints:

struct MyEndpoint
  include Azu::Endpoint(EmptyRequest, MyResponse)

  get "/path"  # Registers GET /path

  def call : MyResponse
    # Handle request
  end
end

Route Patterns

Static Routes

get "/"
get "/users"
get "/api/v1/health"

Dynamic Parameters

Wildcard Routes

HTTP Methods

Method
Macro
Description

GET

get

Retrieve resource

POST

post

Create resource

PUT

put

Replace resource

PATCH

patch

Update resource

DELETE

delete

Remove resource

OPTIONS

options

Get options

HEAD

head

Get headers only

Route Matching

Routes are matched in order of specificity:

  1. Exact static matches

  2. Parameterized routes

  3. Wildcard routes

Accessing Route Parameters

Wildcard Parameters

Route Constraints

Custom Constraints

Router Configuration

Path Caching

Enable path caching for performance:

Router API

routes

Get all registered routes.

find

Find a route for a request.

Route Groups

Organize routes with common prefixes:

Performance

The router uses a radix tree (Radix) for O(k) route matching where k is the path length.

Benchmarks

  • Static routes: ~150ns

  • Dynamic routes: ~200ns

  • Wildcard routes: ~250ns

Optimization Tips

  1. Put most common routes first in handler chain

  2. Enable path caching for repeated paths

  3. Use static paths when possible

Error Handling

Not Found

When no route matches:

Method Not Allowed

When path matches but method doesn't:

Complete Example

See Also

Last updated

Was this helpful?