Skip to main content

Module extractors

Module extractors 

Source
Expand description

Per-request extractors for handlers — tenancy-aware DI. Ships extractors::Tenant, extractors::SessionUser, and extractors::SessionOperator. Tenancy-aware extractors for handlers.

ExtractorWhat it gives you
TenantTenant-scoped DB connection (org + pool)
SessionUserBrowser-session tenant user (None = anonymous)
SessionOperatorBrowser-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§

DatabaseTenant
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.
DatabaseTenantContext
What the DatabaseTenant extractor reads from request extensions. DB is the tenant-data backend, sqlx::Sqlite or sqlx::MySql. The registry pool is the backend-erasing crate::sql::Pool, so an app can be all SQLite, or mix a Postgres registry with SQLite tenants.
SessionOperator
Reads the rustango_op_session cookie and returns the matching Operator, or None when the session is missing or has expired. It never rejects.
SessionUser
Reads the rustango_tenant_session cookie and returns the matching User, or None when 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. DB defaults to Postgres, so fn handler(t: Tenant) means Tenant<sqlx::Postgres>. Borrow the connection with Tenant::conn for ORM calls.
TenantContext
What the Tenant extractor reads from request extensions. crate::server::Builder builds it once and clones the Arc into every request.

Enums§

DatabaseTenantRejection
Why the DatabaseTenant extractor failed. It has the same shape as super::TenantRejection, but is a separate type so the compiler catches a mix-up.
TenantRejection
Why the Tenant extractor failed.