dream/context
Request context for your application
Context holds per-request data that changes with every request—user authentication, session info, request IDs, etc. It’s passed to every middleware and controller.
The Default Context
Dream provides AppContext with just a request_id field. It’s fine for simple apps,
but most applications will want their own context type.
Custom Context
Define your own context type to hold whatever per-request data you need:
pub type MyContext {
MyContext(
request_id: String,
user: Option(User),
session: Session,
permissions: List(String),
)
}
Then pass it to your server:
dream.new()
|> dream.context(MyContext(
request_id: "",
user: None,
session: empty_session(),
permissions: []
))
|> dream.services(my_services)
|> dream.router(my_router)
|> dream.listen(3000)
Enriching Context in Middleware
Middleware can update the context as requests flow through:
pub fn auth_middleware(request, context, services, next) {
case extract_token(request) {
Ok(token) -> {
let user = verify_token(services.db, token)
let new_context = MyContext(..context, user: Some(user))
next(request, new_context, services)
}
Error(_) -> unauthorized_response()
}
}
The type system ensures your controllers receive the right context type.
Types
Default context with just a request ID
Use this for simple applications or replace it with your own context type. Most real applications will want custom context for user data, sessions, etc.
pub type AppContext {
AppContext(request_id: String)
}
Constructors
-
AppContext(request_id: String)
Values
pub fn new_context(request_id: String) -> AppContext
Create a new AppContext
Helper for creating the default context. If you’re using a custom context, you’ll create it directly without this function.