HTTP::Server

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

Overview

spinner

Basic Setup

1. Define Session Data

require "session"

class UserSession < Session::Base
  property user_id : Int64?
  property username : String?
  property role : String = "guest"
end

2. Configure Session

Session.configure do |config|
  config.secret = ENV["SESSION_SECRET"]
  config.timeout = 24.hours
  config.session_key = "_myapp_session"
  config.store = Session::MemoryStore(UserSession).new
end

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?