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