Endpoints

This document explains the concept of endpoints in Azu and how they provide a structured approach to handling HTTP requests.

What is an Endpoint?

An endpoint is a structured handler for a specific HTTP route. It combines:

  • Route definition - The HTTP method and path

  • Request contract - Expected input shape

  • Response contract - Output format

  • Business logic - The actual handling code

struct CreateUserEndpoint
  include Azu::Endpoint(CreateUserRequest, UserResponse)

  post "/users"

  def call : UserResponse
    # Business logic here
  end
end

Why Endpoints?

Traditional Approach Problems

In traditional MVC frameworks:

Azu's Solution

Endpoints make contracts explicit:

Endpoint Components

1. Route Declaration

The route macro registers the HTTP method and path:

Routes support:

  • Static segments: /users

  • Parameters: :id

  • Wildcards: *path

2. Request Contract

The first type parameter defines expected input:

For endpoints without body data:

3. Response Contract

The second type parameter defines the output:

4. Call Method

The call method contains business logic:

Available Context

Inside call, you have access to:

Request Access Pattern

The request object is accessed via a generated method:

Single Responsibility

Each endpoint handles one route:

Benefits:

  • Clear responsibility

  • Easy to find code

  • Simple to test

  • No action dispatch overhead

Struct vs Class

Endpoints are typically structs:

Why structs?

  • Value semantics

  • Stack allocation when possible

  • Immutable by default

  • Better performance

Use class only if you need:

  • Inheritance

  • Reference semantics

  • Instance-level state (rare)

Error Handling

Endpoints can raise typed errors:

The handler chain catches and converts these to HTTP responses.

Testing Endpoints

Endpoints are easy to test in isolation:

See Also

Last updated

Was this helpful?