chyabrung-cli 0.2.3

The `chy` command line tool for Chyabrung: scaffolding, dev server with hot reload, builds, generators, migrations and one-command deploys
//! # API routes: the JSON API under `/api`
//!
//! Everything a mobile app, a single-page app or another service calls:
//! JSON endpoints, plus the WebSocket at `/ws`. Browser pages belong in `web.rs`.
//!
//! `chy make:resource Comment` mounts its controller here, at the
//! `chy:resources` line.

use crate::controllers::post_controller::PostController;
use crate::controllers::{auth_controller, realtime_controller, stats_controller, upload_controller};
use chyabrung::prelude::*;
use std::time::Duration;

/// Add the API routes to the app.
pub fn register(app: App) -> App {
    app.get("/ws", realtime_controller::socket)
        .group("/api", |r| {
            r.group("/auth", |r| {
                // Slow down password guessing: 20 tries a minute per IP. Behind a
                // reverse proxy (Nginx, a load balancer) add `.trust_proxy(true)`,
                // or every visitor shares the proxy's IP and one limit.
                r.middleware(rate_limit(20, Duration::from_secs(60)))
                    .post("/register", auth_controller::register)
                    .post("/login", auth_controller::login)
            })
            .get("/me", auth_controller::me)
            .mount(PostController) // GET/POST /api/posts, GET/PUT/DELETE /api/posts/:id
            .get("/stats", stats_controller::index)
            .post("/upload", upload_controller::store)
            .get("/events", realtime_controller::events)
            // chy:resources (`chy make:resource` mounts new controllers above this line)
        })
}