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