HTTP::Server

Integrate Session with Crystal's built-in HTTP::Server for lightweight applications.

Overview

spinner

Basic Setup

1. Define Session Data

require "session"

struct UserSession
  include Session::SessionData

  property user_id : Int64?
  property username : String?
  property role : String = "guest"
end

2. Configure Session

3. Use the Built-in Handler

Session provides a ready-to-use SessionHandler:

4. Access Sessions in Your Handler

Custom Session Handler

If you need custom behavior, implement your own handler:

Complete Example

Production Configuration

Handler Order

Place the session handler early in the chain, after request tracking but before your application logic:

Last updated

Was this helpful?