Skip to content

Service providers & container ​

A provider is a unit of setup, as in Laravel: it binds services and adds routes, commands, jobs and schedules while the application is assembled, then optionally checks or prepares things once the database is connected. The framework's own features are providers too. The container holds one shared instance per type, which handlers receive with Inject<T>.

Quick start ​

src/bootstrap/mod.rs assembles the application from the providers listed, in order, in src/bootstrap/providers.rs (like Laravel's bootstrap/providers.php). A new application's list:

rust
// src/bootstrap/providers.rs
pub fn register(app: &mut AppBuilder, api: &ApiRoutes) {
    app.provider(providers::auth_service_provider::provider(api));
    app.provider(providers::admin_panel_provider::provider());
    app.provider(providers::app_service_provider::AppServiceProvider);
}

Each comes from a file of src/providers/:

FileReturnsRegisters
auth_service_provider.rsAuthProviderauthentication, and the permissions beyond each resource's own (those of your API routes included)
admin_panel_provider.rsAdminProviderthe panel: resources, pages, extensions, translations
app_service_provider.rsyour ServiceProviderjobs, seeders, console commands, your services

Your own providers go in the same list: app.provider(BillingProvider);.

Framework providers ​

ProviderAddsNeeded for
AuthProvider (rustcms::auth)the admin session and API guards, sign-in routes, permissions, make:user, permission:sync, …authentication and the admin panel; before every provider that uses its services
rustcms::storage::StorageProviderthe configured disk ([storage])uploads, the media library (AdminProvider::media())
rustcms::mail::MailProviderthe mailer ([mail]) and its jobsending mail: invitations, password resets, your own mail
AdminProvider (rustcms::admin)the admin panelthe panel

Order matters during register: a provider can only use what earlier providers bound (in boot, every binding is available). AdminProvider reads the services of AuthProvider, and of StorageProvider when the panel has a media library, in its register: register them before it. A new application has neither storage nor mail; add them when you need them.

A full list: the demo ​

The demo application registers storage and mail, and passes its admin routes to AuthProvider so that their permissions are declared too:

rs
pub fn register(app: &mut AppBuilder, api: &ApiRoutes, admin: &AdminRoutes) {
    app.provider(providers::auth_service_provider::provider(api, admin));
    // Uploads under `storage/app` (see `[storage]`); the panel's media library uses them.
    app.provider(rustcms::storage::StorageProvider);
    app.provider(rustcms::mail::MailProvider);
    app.provider(providers::admin_panel_provider::provider());
    app.provider(providers::app_service_provider::AppServiceProvider);
}
rs
pub fn provider(api: &ApiRoutes, admin: &AdminRoutes) -> AuthProvider {
    AuthProvider::new()
        // Editors edit their own posts (see `PostPolicy`).
        .permission(ADMIN_GUARD, "posts.update_own", "posts")
        // The API's permissions come from its routes, the panel pages' from theirs.
        .api_permissions(api)
        .admin_permissions(admin)
        // How the Roles page shows them: an area, labels, and a starting selection.
        .permission_area("live", PermissionArea::new("Live").sort(10))
        .permission_meta(
            ADMIN_GUARD,
            "demo.live",
            PermissionMeta::new().label("Use live demos").description("Ticks, echo socket and hello page").area("live"),
        )
        .permission_meta(
            ADMIN_GUARD,
            "posts.update_own",
            PermissionMeta::new().label("Edit own posts").description("Edit the posts you wrote"),
        )
        .permission_preset(
            "viewer",
            PermissionPreset::new("Viewer").description("Reads posts and categories").permissions([
                "posts.view_any",
                "posts.view",
                "categories.view_any",
                "categories.view",
            ]),
        )
}

AuthProvider builders:

MethodDoes
AuthProvider::new()the built-in permissions only (users, roles, permissions)
.permission(guard, name, group)declares a permission for permission:sync
.api_permissions(&api)declares every permission the API routes require (api guard)
.admin_permissions(&admin)declares every permission the admin routes require (admin guard)
.permission_area(id, area)a section of the Roles page
.permission_meta(guard, name, meta)the label, description and area of a permission on the Roles page
.permission_preset(id, preset)a starting selection on the Roles page (it grants nothing by itself)
.define(ability, check)a gate ability (Authorization)

See Users & roles for the Roles page.

Writing a provider ​

A provider has two phases:

  • register(&self, app: &mut AppBuilder) runs while the application is assembled, right when app.provider(…) adds it: read configuration, bind services, add routes, commands, jobs, schedules. It cannot reach the database. A configuration error here is a panic with a clear message.
  • async fn boot(&self, state: &AppState) -> anyhow::Result<()> runs once the database is connected and every provider is registered, in the same order. An error stops the command.

boot runs for serve, db:seed, migrate:fresh --seed and the console commands of providers and the application. It does not run for migrate, migrate:rollback, migrate:refresh, migrate:status, route:list or key:generate (What each command loads). Both methods have empty defaults.

A sketch (Stripe, StripeConfig and SyncInvoices stand for your own types):

rust
// Cargo.toml: anyhow = "1"
use rustcms::async_trait;
use rustcms::prelude::*;

struct BillingProvider;

#[async_trait]
impl ServiceProvider for BillingProvider {
    fn register(&self, app: &mut AppBuilder) {
        let config: StripeConfig = app.config().section("stripe").expect("[stripe] settings");
        app.bind(Stripe::new(config));
        app.command(SyncInvoices);
    }

    async fn boot(&self, state: &AppState) -> anyhow::Result<()> {
        state.service::<Stripe>().expect("bound in register").verify().await
    }
}

The demo's application provider binds a service, registers seeders and keeps the generator anchors:

rs
pub struct AppServiceProvider;

#[async_trait]
impl ServiceProvider for AppServiceProvider {
    fn register(&self, app: &mut AppBuilder) {
        // The live page's counter (one producer for every viewer).
        let ticker = crate::services::ticker::Ticker::new(app.shutdown());
        app.bind(ticker);
        // `rustcms db:seed` runs `database` (it calls the others).
        app.seeder(seeders::DatabaseSeeder);
        app.seeder(seeders::user_seeder::UserSeeder);
        app.seeder(seeders::category_seeder::CategorySeeder);
        app.seeder(seeders::post_seeder::PostSeeder);
        // rustcms:seeders
        // rustcms:jobs
        // rustcms:commands
    }
}

Background work

Do not start loops with tokio::spawn in register or boot: they would run in every command that boots, on every instance. Use app.schedule(…) for periodic work (run automatically only by serve, each slot once across instances; by hand with schedule:run <name>) and jobs for work triggered by a request. A long-lived task tied to requests (a stream producer) takes app.shutdown(), which is cancelled when the server stops, as the demo's Ticker does.

The container ​

app.bind(value) stores one shared instance per type; binding the same type again replaces it. Read it back:

rs
/// `POST /admin/api/x/demo/ticks/reset`.
pub async fn reset(Inject(ticker): Inject<Ticker>) -> StatusCode {
    ticker.reset();
    StatusCode::NO_CONTENT
}
rust
// While assembling, after the provider that bound it:
let ticker = app.service::<Ticker>();       // Option<Arc<Ticker>>
// Anywhere with the state (boot, jobs, commands, seeders):
let ticker = state.service::<Ticker>();     // Option<Arc<Ticker>>

A missing binding in a handler is a 500 (logged), never a panic. Bind a type of your own (a struct, or a newtype around a client) rather than a common type such as String, which another provider could bind too.

AppState also gives every handler and job config(), db() (the pool) and keyring() (APP_KEY and the previous keys, for encrypted values). Handlers take it as State(state): State<AppState>.

The builder ​

MethodDoes
AppBuilder::new(config)an empty application (with the queue, schedule and key:reencrypt commands)
app.provider(p)adds a provider (its register runs now)
app.bind(service) / app.service::<T>()the container
app.config()the configuration
app.routes(|router| …)adds Axum routes (prefer ApiRoutes and AdminRoutes, which declare access)
app.middleware(|router, state| …)wraps the application's routes (Adding middleware)
app.describe_routes(source, routes)lists routes added with routes for route:list: routes yields (Method, String) pairs
app.described_routes()every described route, sorted
app.body_limit(method, path, bytes)the body limit of requests with exactly this method and path (larger or smaller than http.body_limit)
app.command(cmd) / app.commands()a console command, an AppCommand (Console)
app.job::<J>()a job the queue's workers run (Queues)
app.schedule(schedule)periodic work, run automatically only by serve (Scheduling)
app.seeder(seeder) / app.seeders()a seeder for db:seed (Seeding)
app.encrypted_column::<E>(column, binding)an encrypted column for key:reencrypt (Encrypted columns)
app.add_encrypted_column(column) / app.encrypted_columns()the same, for a column built elsewhere; the columns so far
app.mail_template(template)a mail template, mail feature (Mail)
app.shutdown()the signal long-lived tasks stop on when the server stops
app.build().awaitconnects and boots: an App (the console does this)
app.build_with_db(db).awaitthe same on an existing connection (tests, tools)

These registrations panic on a mistake, so it shows at the first start: job on an invalid kind or a kind another job type already uses (registering the same type again does nothing); schedule and seeder on an invalid or repeated name; mail_template on an invalid template or one added twice for the same name and locale; encrypted_column on a column registered twice with another binding.

Pitfalls ​

  • A service is None in register: the provider that binds it comes later in the list. Move it up, or read it in boot.
  • AdminProvider::media() panics at start: StorageProvider is missing or registered after AdminProvider.
  • A permission is missing on the Roles page: declare it on AuthProvider (or through api_permissions / admin_permissions), then run permission:sync.

RustCMS 0.17.0 · source cfb82fa33fab · MIT OR Apache-2.0