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 modelGET /<table>— list rowsGET /__admin/<table>/new— create formPOST /<table>— submit createGET /<table>/<pk>— detail viewGET /<table>/<pk>/edit— edit form (PK readonly)POST /<table>/<pk>— submit editPOST /<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] —AdminErrorand itsIntoResponseimpl. forms,render,auth— value parsing, HTML rendering primitives, HTTP Basic auth middleware.
Structs§
- Builder
- Configurable admin builder.
Enums§
- Admin
Error - Error returned by admin handlers — including user-defined bulk
action handlers registered via
super::Builder::register_action. Variants are non-exhaustive only forInternal; user code should almost always returnAdminError::Internalfrom custom actions.
Functions§
- protect_
with_ basic_ auth - Wrap
routerso 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§
- Admin
Action Fn - 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. ReturnOk(())on success;AdminError::Internal(...)for failure (renders as 500). Built-indelete_selecteduses this signature. - Admin
Action Future - Future returned by an [
AdminAction] handler.