| 1 | //! HTTP server for anvil: the web UI and the smart-HTTP git endpoints |
| 2 | //! (`/<owner>/<repo>.git/info/refs`, `/git-upload-pack`, `/git-receive-pack`), |
| 3 | //! plus cookie-session auth. |
| 4 | |
| 5 | // Handlers return `Result<_, Response>` — the idiomatic axum pattern where the |
| 6 | // error arm is a ready-made HTTP response (404/500/redirect). `Response` is |
| 7 | // intrinsically large, so `result_large_err` fires across the crate; the |
| 8 | // pattern is intentional and the responses are never hot-path allocated en masse. |
| 9 | #![allow(clippy::result_large_err)] |
| 10 | |
| 11 | use anvil_core::{ |
| 12 | App, |
| 13 | Result, |
| 14 | }; |
| 15 | use axum::{ |
| 16 | Router, |
| 17 | routing::{ |
| 18 | get, |
| 19 | post, |
| 20 | }, |
| 21 | }; |
| 22 | |
| 23 | pub mod artifacts; |
| 24 | pub mod auth; |
| 25 | pub mod git_http; |
| 26 | pub mod issues; |
| 27 | pub mod pages; |
| 28 | pub mod ui; |
| 29 | |
| 30 | /// Build the application router. |
| 31 | pub fn router(app: App) -> Router { |
| 32 | let mut router = Router::new() |
| 33 | .route("/-/healthz", get(healthz)) |
| 34 | .route("/-/login", get(auth::login_form).post(auth::login_submit)) |
| 35 | .route("/-/logout", post(auth::logout)); |
| 36 | router = ui::routes(router); // web UI, including `/` |
| 37 | router = pages::routes(router); // static sites from `pages` branches |
| 38 | router = artifacts::routes(router); // CI artifact downloads + sites |
| 39 | router = issues::routes(router); // per-repo issue tracker |
| 40 | router = git_http::routes(router); // smart-HTTP git endpoints |
| 41 | router |
| 42 | // Derives the per-request CSRF token so the layout can attach it to |
| 43 | // htmx requests (hx-headers). Runs for all routes; cheap. |
| 44 | .layer(axum::middleware::from_fn_with_state( |
| 45 | app.clone(), |
| 46 | auth::csrf_context, |
| 47 | )) |
| 48 | .with_state(app) |
| 49 | } |
| 50 | |
| 51 | /// Bind to the configured HTTP address and serve until shutdown. |
| 52 | pub async fn serve(app: App) -> Result<()> { |
| 53 | let listen = app.config.http.listen.clone(); |
| 54 | let router = router(app); |
| 55 | |
| 56 | let listener = tokio::net::TcpListener::bind(&listen).await?; |
| 57 | tracing::info!("anvil web server listening on http://{listen}"); |
| 58 | axum::serve(listener, router).await?; |
| 59 | Ok(()) |
| 60 | } |
| 61 | |
| 62 | async fn healthz() -> &'static str { |
| 63 | "ok" |
| 64 | } |