chyabrung-cli 0.2.2

The `chy` command line tool for Chyabrung: scaffolding, dev server with hot reload, builds, generators, migrations and one-command deploys
//! # {{app_name}}
//!
//! A JSON API built with Chyabrung. Where things live:
//!
//! | Path               | What goes there                                    | Create with                         |
//! |--------------------|----------------------------------------------------|-------------------------------------|
//! | `src/routes.rs`    | every URL of the app (start here)                  | edit by hand                        |
//! | `src/controllers/` | handlers: read the request, return a response      | `chy make:controller PostController`|
//! | (all of the above) | a whole feature: model to test, routes mounted     | `chy make:resource Comment`         |
//! | `src/models/`      | one struct per database table                      | `chy make:model Post -m`            |
//! | `src/requests/`    | incoming data and its validation rules             | add a file by hand                  |
//! | `src/middleware/`  | code that runs before or after handlers            | `chy make:middleware EnsureAdmin`   |
//! | `migrations/`      | SQL that creates and changes tables                | `chy make:migration add_bio_to_users`|
//! | `database/seeds/`  | sample data for development                        | `chy make:seeder users`             |
//! | `public/`          | static files served at `/assets/...`               | copy files in                       |
//! | `tests/`           | automated tests (`cargo test`)                     | add a file by hand                  |
//!
//! This file wires everything together: shared state, global middleware and routes.

pub mod controllers;
pub mod middleware;
pub mod models;
pub mod requests;
pub mod routes;

use chyabrung::prelude::*;
use chyabrung::auth::has_role;
use chyabrung::maintenance::Maintenance;
use chyabrung::storage::Storage;
use std::time::Duration;

/// The SQL migrations in `migrations/`. They run when the app starts, and
/// `chy migrate`, `chy migrate:status` and `chy migrate:rollback` use the same files.
pub fn migrator() -> Result<Migrator> {
    Migrator::new().dir("migrations")
}

/// Build the application.
pub fn app(db: Db, jwt: Jwt, cache: Cache, storage: Storage) -> App {
    // Who may do what: `require_permission("posts.update")` or `user.can("posts.update")`.
    let roles = Roles::new().role("admin", &["*"]).role("editor", &["posts.update"]).role("user", &[]);

    let app = App::new()
        // Shared state: any handler can ask for it with `State(db): State<Db>`.
        .state(db)
        .state(jwt.clone())
        .state(cache)
        .state(storage)
        .state(roles)
        // Global middleware: runs for every request, top to bottom.
        .middleware(request_id())
        .middleware(logger())
        .middleware(secure_headers())
        .middleware(cors())
        .middleware(compression())
        .middleware(timeout(Duration::from_secs(30)))
        .middleware(authenticate().jwt(jwt)) // `Authorization: Bearer <token>` → logged-in user
        // `chy down` shows a "back soon" page to everyone except admins and
        // the bypass URL it prints; `chy up` ends it.
        .maintenance(Maintenance::new().allow(has_role("admin")).except("/assets"))
        // Files: `public/` at /assets, uploads at /storage.
        .static_files("/assets", "public")
        .static_files("/storage", "storage/public")
        .body_limit(10 * 1024 * 1024) // allow uploads up to 10 MB
        // API docs generated from the routes and the requests/ and models/ types:
        // Swagger UI at /docs, the OpenAPI spec at /openapi.json.
        .openapi(OpenApi::new("{{app_name}}", env!("CARGO_PKG_VERSION")));

    routes::register(app)
}