mod api; mod pages; use axum::Router; use axum::http::header; use axum::response::IntoResponse; use axum::routing::get; use sqlx::SqlitePool; use tower_http::trace::TraceLayer; pub fn app(pool: SqlitePool) -> Router { Router::new() .merge(pages::router()) .nest("/api", api::router()) .route("/health", get(|| async { "ok" })) .route( "/static/htmx.min.js", get(|| asset("text/javascript", include_str!("../../static/htmx.min.js"))), ) .route( "/static/flatpickr/flatpickr.min.js", get(|| { asset( "text/javascript", include_str!("../../static/flatpickr/flatpickr.min.js"), ) }), ) .route( "/static/flatpickr/fr.min.js", get(|| { asset( "text/javascript", include_str!("../../static/flatpickr/fr.min.js"), ) }), ) .route( "/static/flatpickr/flatpickr.min.css", get(|| { asset( "text/css", include_str!("../../static/flatpickr/flatpickr.min.css"), ) }), ) .route("/static/app.js", get(|| asset("text/javascript", APP_JS))) .route("/static/app.css", get(|| asset("text/css", APP_CSS))) .route( "/static/favicon.svg", get(|| asset("image/svg+xml", include_str!("../../static/favicon.svg"))), ) .layer(TraceLayer::new_for_http()) .with_state(pool) } const APP_CSS: &str = include_str!("../../static/app.css"); const APP_JS: &str = include_str!("../../static/app.js"); /// Hash of app.css + app.js, appended as `?v=` to their URLs so a deploy /// bypasses the browser cache. pub fn asset_version() -> &'static str { static VERSION: std::sync::LazyLock = std::sync::LazyLock::new(|| { use std::hash::{Hash, Hasher}; let mut h = std::collections::hash_map::DefaultHasher::new(); (APP_CSS, APP_JS).hash(&mut h); format!("{:x}", h.finish()) }); &VERSION } // En livereload, le CSS/JS doit être relu à chaque rechargement. const CACHE_CONTROL: &str = if cfg!(feature = "livereload") { "no-cache" } else { "public, max-age=3600" }; async fn asset(content_type: &'static str, body: &'static str) -> impl IntoResponse { ( [ (header::CONTENT_TYPE, content_type), (header::CACHE_CONTROL, CACHE_CONTROL), ], body, ) }