anvilsign in

collin/anvil

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
11use anvil_core::{
12 App,
13 Result,
14};
15use axum::{
16 Router,
17 routing::{
18 get,
19 post,
20 },
21};
22
23pub mod admin;
24pub mod artifacts;
25pub mod attachments;
26pub mod auth;
27pub mod git_http;
28pub mod pages;
29pub mod todomd;
30pub mod ui;
31
32/// Build the application router.
33pub fn router(app: App) -> Router {
34 let mut router = Router::new()
35 .route("/-/healthz", get(healthz))
36 .route("/-/login", get(auth::login_form).post(auth::login_submit))
37 .route("/-/logout", post(auth::logout));
38 router = ui::routes(router); // web UI, including `/`
39 router = admin::routes(router); // admin-only dashboard
40 router = pages::routes(router); // static sites from `pages` branches
41 router = artifacts::routes(router); // CI artifact downloads + sites
42 router = attachments::routes(
43 router,
44 app.config.http.attachment_max_mb.saturating_mul(1 << 20),
45 ); // uploaded image attachments
46 router = git_http::routes(router); // smart-HTTP git endpoints
47 router
48 // Derives the per-request CSRF token so the layout can attach it to
49 // htmx requests (hx-headers). Runs for all routes; cheap.
50 .layer(axum::middleware::from_fn_with_state(
51 app.clone(),
52 auth::csrf_context,
53 ))
54 .with_state(app)
55}
56
57/// Bind to the configured HTTP address and serve until shutdown.
58pub async fn serve(app: App) -> Result<()> {
59 let listen = app.config.http.listen.clone();
60 let router = router(app);
61
62 let listener = tokio::net::TcpListener::bind(&listen).await?;
63 tracing::info!("anvil web server listening on http://{listen}");
64 axum::serve(listener, router).await?;
65 Ok(())
66}
67
68async fn healthz() -> &'static str {
69 "ok"
70}