app.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. //! Router assembly and middleware wiring.
  2. //!
  3. //! [`build_app`] is the single entry point used by both `main.rs` and the
  4. //! integration tests. It mounts the health probe and the public `/api`
  5. //! display endpoints, then defers every unmatched path to the static SPA
  6. //! service, and wraps the whole stack in the HTTP tracing layer.
  7. //!
  8. //! Extension points for todos 15-18: extend `api::router()` with the OAuth
  9. //! and session routes (they receive [`AppState`] via `State<AppState>` once
  10. //! `with_state` runs). Stateful routes MUST be added before the
  11. //! `fallback_service` call.
  12. use axum::Router;
  13. use axum::routing::get;
  14. use tower_http::trace::TraceLayer;
  15. use crate::state::AppState;
  16. use crate::{api, health, static_files};
  17. /// Build the application router.
  18. ///
  19. /// Returns a state-resolved `Router` ready for `axum::serve` or
  20. /// `tower::ServiceExt::oneshot`.
  21. pub fn build_app(state: AppState) -> Router {
  22. let router: Router<AppState> = Router::new()
  23. .route("/healthz", get(health::healthz))
  24. .route("/oauth/start", get(crate::oauth::start))
  25. .route("/callback", get(crate::oauth::callback))
  26. .nest("/api", api::router(state.sessions().clone()))
  27. .fallback_service(static_files::service(state.config().dist_dir()));
  28. router.layer(TraceLayer::new_for_http()).with_state(state)
  29. }