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