Skip to main content

Module admin

Module admin 

Source
Expand description

Auto-generated CRUD admin for rustango models.

Walks the inventory registry every #[derive(Model)] populates and serves an axum axum::Router over it — no per-model code required.

use rustango::{migrate, admin};
use rustango::sql::sqlx::PgPool;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pool = PgPool::connect(&std::env::var("DATABASE_URL")?).await?;
    migrate::apply_all(&pool).await?;

    let app = admin::router(pool);
    let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await?;
    axum::serve(listener, app).await?;
    Ok(())
}

Routes:

  • GET / — list every registered model
  • GET /<table> — list rows
  • GET /__admin/<table>/new — create form
  • POST /<table> — submit create
  • GET /<table>/<pk> — detail view
  • GET /<table>/<pk>/edit — edit form (PK readonly)
  • POST /<table>/<pk> — submit edit
  • POST /<table>/<pk>/delete — submit delete

§Crate layout (Django-shape)

  • [urls] — router(pool), Builder, Config, AppState (route table).
  • [views] — one async fn per route; consumes the inventory registry.
  • [helpers] — model lookup, FK joins, render_cell, render_form, pager.
  • [templates] — bundled Tera registry + render entry-point.
  • [errors] — AdminError and its IntoResponse impl.
  • forms, render, auth — value parsing, HTML rendering primitives, HTTP Basic auth middleware.

Structs§

Builder
Configurable admin builder.

Enums§

AdminError
Error returned by admin handlers — including user-defined bulk action handlers registered via super::Builder::register_action. Variants are non-exhaustive only for Internal; user code should almost always return AdminError::Internal from custom actions.

Functions§

protect_with_basic_auth
Wrap router so every request requires HTTP Basic Auth with the given credentials. The browser shows a native login dialog on the first request.
router
Mount the admin under any prefix using axum’s nesting: Router::new().nest("/admin", crate::admin::router(pool)).

Type Aliases§

AdminActionFn
Bulk action handler. Receives the model’s &PgPool (not a tenant connection — the admin runs with the connection the request lives on, so search_path is already correct) and the parsed PK list of the rows the operator selected. Return Ok(()) on success; AdminError::Internal(...) for failure (renders as 500). Built-in delete_selected uses this signature.
AdminActionFuture
Future returned by an [AdminAction] handler.