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