Expand description
Per-request extractors for handlers — tenancy-aware DI. Ships
extractors::Tenant, extractors::SessionUser, and
extractors::SessionOperator.
Tenancy-aware extractors for handlers.
| Extractor | What it gives you |
|---|---|
Tenant | Tenant-scoped DB connection (org + pool) |
SessionUser | Browser-session tenant user (None = anonymous) |
SessionOperator | Browser-session operator (None = anonymous) |
They all read request extensions that
crate::server::Builder fills in, so there is no state to wire
up.
ⓘ
use rustango::extractors::{Tenant, SessionUser};
pub async fn my_handler(
mut t: Tenant,
SessionUser(user): SessionUser,
) -> impl IntoResponse {
match user {
Some(u) => format!("hello, {}", u.username).into_response(),
None => StatusCode::UNAUTHORIZED.into_response(),
}
}Structs§
- Database
Tenant - Resolves the request’s tenant from the registry, then hands the handler a connection on the configured backend. The extractor owns that connection and returns it to the pool when the handler ends.
- Database
Tenant Context - What the
DatabaseTenantextractor reads from request extensions.DBis the tenant-data backend,sqlx::Sqliteorsqlx::MySql. The registry pool is the backend-erasingcrate::sql::Pool, so an app can be all SQLite, or mix a Postgres registry with SQLite tenants. - Session
Operator - Reads the
rustango_op_sessioncookie and returns the matchingOperator, orNonewhen the session is missing or has expired. It never rejects. - Session
User - Reads the
rustango_tenant_sessioncookie and returns the matchingUser, orNonewhen the session is missing or has expired. It never rejects. - Tenant
- Finds the request’s tenant and gives the handler a connection
scoped to it.
DBdefaults to Postgres, sofn handler(t: Tenant)meansTenant<sqlx::Postgres>. Borrow the connection withTenant::connfor ORM calls. - Tenant
Context - What the
Tenantextractor reads from request extensions.crate::server::Builderbuilds it once and clones theArcinto every request.
Enums§
- Database
Tenant Rejection - Why the
DatabaseTenantextractor failed. It has the same shape assuper::TenantRejection, but is a separate type so the compiler catches a mix-up. - Tenant
Rejection - Why the
Tenantextractor failed.