rustango 0.34.0

Django-shaped batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
Documentation
//! Tenancy variant of `startapp` — same Django-shape file layout as
//! `rustango::migrate::manage startapp`, with one twist: when
//! `--with-manage-bin` is passed, the generated `src/bin/manage.rs`
//! wires `crate::tenancy::manage::run` instead of the single-
//! tenant dispatcher. Useful for bootstrapping multi-tenant projects.
//!
//! Falls through to [`rustango::migrate::scaffold::startapp`] for the
//! actual file writes — this module just owns the tenancy template
//! and the argv parser.

use std::io::Write;

use crate::tenancy::error::TenancyError;
use crate::tenancy::manage::args::next_value;

/// Tenancy-aware `manage.rs` template. Wires
/// `crate::tenancy::manage::run` so the resulting binary recognizes
/// `create-tenant` / `migrate-tenants` / `run-server` / etc. on top of
/// the standard `migrate` / `makemigrations` set.
pub const TENANCY_MANAGE_BIN: &str =
    "//! Generated by `manage startapp --with-manage-bin` (tenancy-aware).
//!
//! UX: `cargo run -- create-tenant <slug>`,
//! `cargo run -- run-server`, plus everything the
//! single-tenant dispatcher offers (`migrate`, `makemigrations`, …).

use crate::sql::sqlx::PgPool;
use crate::tenancy::TenantPools;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Pull your models into this binary so `inventory` registers
    // them. For the `manage startapp <name>` shape:
    //   #[allow(unused_imports)]
    //   use super::<name>::models::*;
    // Tenancy registry models (Org / Operator / User) come along
    // automatically via the `crate::tenancy` crate import.

    let registry_url = std::env::var(\"DATABASE_URL\")?;
    let pool = PgPool::connect(&registry_url).await?;
    let pools = TenantPools::new(pool);
    let dir: &std::path::Path = \"./migrations\".as_ref();
    crate::tenancy::manage::run(&pools, &registry_url, dir, std::env::args().skip(1)).await?;
    Ok(())
}
";

pub(super) fn startapp_cmd<W: Write>(args: &[String], w: &mut W) -> Result<(), TenancyError> {
    let mut iter = args.iter();
    let app_name = iter
        .next()
        .cloned()
        .ok_or_else(|| TenancyError::Validation(usage()))?;
    let mut with_manage_bin = false;
    let mut with_bootstrap = false;
    let mut into: Option<String> = None;
    while let Some(flag) = iter.next() {
        match flag.as_str() {
            "--with-manage-bin" => with_manage_bin = true,
            "--with-bootstrap-migration" => with_bootstrap = true,
            "--into" => {
                into = Some(next_value(&mut iter, "--into")?);
            }
            "--help" | "-h" => {
                writeln!(w, "{}", usage())?;
                return Ok(());
            }
            "--app-name" => {
                // Belt-and-braces: accept --app-name <X> as an alias
                // even though the positional form is canonical.
                let _ = next_value(&mut iter, "--app-name")?;
            }
            other => {
                return Err(TenancyError::Validation(format!(
                    "startapp: unknown argument `{other}` (run --help for usage)"
                )));
            }
        }
    }
    let base_label = into.clone().unwrap_or_else(|| "src".into());
    let opts = rustango::migrate::scaffold::StartAppOptions {
        app_name: app_name.clone(),
        manage_bin: with_manage_bin.then_some(TENANCY_MANAGE_BIN),
        base_dir: into.map(std::path::PathBuf::from),
    };
    let cwd = std::env::current_dir().map_err(TenancyError::Io)?;
    let report =
        rustango::migrate::scaffold::startapp(&cwd, &opts).map_err(TenancyError::Migrate)?;

    // Optional: drop the framework's registry+tenant bootstrap
    // migrations into the new app's `migrations/` subdirectory so a
    // fresh project is `cargo run`-ready in one command — no separate
    // `init-tenancy` step.
    let bootstrap_report = if with_bootstrap {
        let migrations_dir = cwd
            .join(
                opts.base_dir
                    .clone()
                    .unwrap_or_else(|| std::path::PathBuf::from("src")),
            )
            .join(&app_name)
            .join("migrations");
        Some(crate::tenancy::init_tenancy(&migrations_dir)?)
    } else {
        None
    };

    write_report(
        w,
        &app_name,
        &base_label,
        &report,
        bootstrap_report.as_ref(),
    )
}

fn write_report<W: Write>(
    w: &mut W,
    app_name: &str,
    base_label: &str,
    report: &rustango::migrate::scaffold::StartAppReport,
    bootstrap: Option<&crate::tenancy::InitTenancyReport>,
) -> Result<(), TenancyError> {
    if report.written.is_empty()
        && report.skipped.is_empty()
        && bootstrap.is_none_or(|b| b.written.is_empty() && b.skipped.is_empty())
    {
        writeln!(w, "startapp: nothing to do")?;
        return Ok(());
    }
    writeln!(w, "startapp `{app_name}` (tenancy-aware)")?;
    for path in &report.written {
        writeln!(w, "  + wrote {path}")?;
    }
    for path in &report.skipped {
        writeln!(w, "  · {path} already exists — left untouched")?;
    }
    for path in &report.patched {
        writeln!(w, "  ~ patched {path} (auto-mounted new app)")?;
    }
    for hint in &report.manual_steps {
        writeln!(w, "  ! manual: {hint}")?;
    }
    if let Some(b) = bootstrap {
        let migrations_rel = format!("{base_label}/{app_name}/migrations");
        for name in &b.written {
            writeln!(w, "  + wrote {migrations_rel}/{name}.json")?;
        }
        for name in &b.skipped {
            writeln!(
                w,
                "  · {migrations_rel}/{name}.json already exists — left untouched"
            )?;
        }
    }
    if !report.written.is_empty() {
        writeln!(w, "next:")?;
        writeln!(
            w,
            "  add `mod {app_name};` to {base_label}/main.rs (or {base_label}/lib.rs)"
        )?;
        writeln!(
            w,
            "  so the derive macros' `inventory` registrations are pulled in."
        )?;
        if bootstrap.is_some() {
            writeln!(w)?;
            writeln!(
                w,
                "  bootstrap migrations are already in {base_label}/{app_name}/migrations/ —"
            )?;
            writeln!(
                w,
                "  point `Builder::migrate(...)` at that directory and `cargo run` is enough."
            )?;
        } else {
            writeln!(w)?;
            writeln!(
                w,
                "  for tenancy: `cargo run -- init-tenancy && cargo run -- migrate` to"
            )?;
            writeln!(
                w,
                "  materialize the registry + tenant bootstrap migrations."
            )?;
            writeln!(
                w,
                "  (or pass `--with-bootstrap-migration` next time to bundle them in.)"
            )?;
        }
    }
    Ok(())
}

fn usage() -> String {
    "startapp <name> [--into <dir>] [--with-manage-bin] [--with-bootstrap-migration]\n  \
     Scaffold a Django-shape app module under <dir>/<name>/ (mod.rs +\n  \
     models.rs + views.rs + urls.rs). Idempotent: existing files are\n  \
     left untouched. <name> must be a valid Rust identifier.\n\n  \
     --into <dir>\n  \
     Override the default `src/` directory the app lands in. Use\n  \
     for non-standard layouts — e.g. `--into examples/blog_demo`\n  \
     for an in-tree example dir, or `--into crates/web` for a\n  \
     workspace member with no `src/` parent. Default: `src`.\n\n  \
     --with-manage-bin\n  \
     Also write <dir>/bin/manage.rs with the **tenancy-aware**\n  \
     dispatcher boilerplate (crate::tenancy::manage::run). Skipped\n  \
     if the file already exists.\n\n  \
     --with-bootstrap-migration\n  \
     Also drop the framework's registry+tenant bootstrap migrations\n  \
     into <dir>/<name>/migrations/. Pair with\n  \
     `Builder::migrate(\"<dir>/<name>/migrations\")` and the project\n  \
     is `cargo run`-ready out of the gate — no separate `init-tenancy`\n  \
     step. Idempotent."
        .to_owned()
}