A minimal self-hosted git browser on Rust axum: bare repos rendered as a website with commits, diffs, syntax-highlighted blobs, atom feeds, and clone over HTTPS.
axumdockergitgit-browsergitoxidegixrustself-hosted
1use axum::{
2 extract::{Request, State},
3 http::StatusCode,
4 middleware::Next,
5 response::{IntoResponse, Response},
6};
7use chrono::Local;
8use std::time::Instant;
9
10use crate::AppState;
11
12pub async fn log_requests(req: Request, next: Next) -> Response {
13 let method = req.method().clone();
14 let path = req
15 .uri()
16 .path_and_query()
17 .map(|p| p.as_str().to_string())
18 .unwrap_or_else(|| req.uri().path().to_string());
19 let start = Instant::now();
20 let response = next.run(req).await;
21 let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0;
22 let status = response.status().as_u16();
23 let now = Local::now().format("%H:%M:%S");
24 let color = match status {
25 200..=299 => "\x1b[32m",
26 300..=399 => "\x1b[36m",
27 400..=499 => "\x1b[33m",
28 _ => "\x1b[31m",
29 };
30 eprintln!("{now} {method:<5} {color}{status}\x1b[0m {elapsed_ms:>7.2}ms {path}");
31 response
32}
33
34/// Router fallback: render the themed 404 shell so unmatched URLs match the
35/// look of a missing-repo page instead of axum's default plain-text body.
36pub async fn not_found(State(state): State<AppState>, req: Request) -> Response {
37 let path = req.uri().path().to_string();
38 let body = crate::render::render(
39 &state,
40 "not_found.html",
41 &path,
42 minijinja::context! { name => path.trim_start_matches('/') },
43 );
44 (StatusCode::NOT_FOUND, body).into_response()
45}