| 1 | //! Request handlers. Each page is an askama template that embeds a [`Layout`]. |
| 2 | |
| 3 | pub mod archive; |
| 4 | pub mod blame; |
| 5 | pub mod blob; |
| 6 | pub mod commit; |
| 7 | pub mod error; |
| 8 | pub mod feed; |
| 9 | pub mod find; |
| 10 | pub mod index; |
| 11 | pub mod log; |
| 12 | pub mod raw; |
| 13 | pub mod refs; |
| 14 | pub mod repo; |
| 15 | pub mod summary; |
| 16 | pub mod tree; |
| 17 | |
| 18 | use std::sync::Arc; |
| 19 | |
| 20 | use askama::Template; |
| 21 | use axum::http::StatusCode; |
| 22 | use axum::response::{Html, IntoResponse, Response}; |
| 23 | |
| 24 | use crate::config::Config; |
| 25 | use crate::server::assets; |
| 26 | use crate::state::AppState; |
| 27 | |
| 28 | /// Data shared by every page: site chrome, asset URLs and customization. |
| 29 | pub struct Layout { |
| 30 | /// `server.base_path`, always ending in `/`. |
| 31 | pub base: String, |
| 32 | /// `{base}-/static/{hash}/` |
| 33 | pub static_base: String, |
| 34 | pub site_title: String, |
| 35 | /// Full `<title>` text. |
| 36 | pub page_title: String, |
| 37 | /// Server default theme: `auto`, `dark` or `light`. |
| 38 | pub theme: &'static str, |
| 39 | /// Versioned URL of `/-/site.css`, when there is anything in it. |
| 40 | pub site_css: Option<String>, |
| 41 | pub header: Option<Arc<str>>, |
| 42 | pub footer: Option<Arc<str>>, |
| 43 | /// Favicon URL and media type: `site.favicon` or the built-in icon. |
| 44 | pub favicon: (String, &'static str), |
| 45 | /// Versioned URL of `site.logo`. |
| 46 | pub logo: Option<String>, |
| 47 | pub version: &'static str, |
| 48 | /// Atom feed advertised in `<head>`. |
| 49 | pub feed: Option<String>, |
| 50 | } |
| 51 | |
| 52 | impl Layout { |
| 53 | /// `title` is the page-specific part; the site title is appended. |
| 54 | pub fn new(state: &AppState, config: &Config, title: Option<&str>) -> Self { |
| 55 | let base = config.server.base_path.clone(); |
| 56 | let site_title = config.site.title.clone(); |
| 57 | let static_base = format!("{base}-/static/{}/", state.assets.hash()); |
| 58 | Layout { |
| 59 | favicon: match &config.custom.favicon { |
| 60 | Some(icon) => ( |
| 61 | format!("{base}-/favicon?v={}", icon.version), |
| 62 | icon.content_type, |
| 63 | ), |
| 64 | None => (format!("{static_base}icons/favicon.svg"), "image/svg+xml"), |
| 65 | }, |
| 66 | logo: config |
| 67 | .custom |
| 68 | .logo |
| 69 | .as_ref() |
| 70 | .map(|logo| format!("{base}-/logo?v={}", logo.version)), |
| 71 | static_base, |
| 72 | site_css: assets::site_css_version(config).map(|v| format!("{base}-/site.css?v={v}")), |
| 73 | page_title: match title { |
| 74 | Some(t) => format!("{t} · {site_title}"), |
| 75 | None => site_title.clone(), |
| 76 | }, |
| 77 | site_title, |
| 78 | base, |
| 79 | theme: config.ui.theme.as_str(), |
| 80 | header: config.custom.header.clone(), |
| 81 | footer: config.custom.footer.clone(), |
| 82 | version: env!("CARGO_PKG_VERSION"), |
| 83 | feed: None, |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Render a template into an HTML response with the given status. |
| 89 | pub fn render(status: StatusCode, page: &impl Template) -> Response { |
| 90 | match page.render() { |
| 91 | Ok(html) => (status, Html(html)).into_response(), |
| 92 | Err(e) => { |
| 93 | tracing::error!("template error: {e}"); |
| 94 | (StatusCode::INTERNAL_SERVER_ERROR, "internal error\n").into_response() |
| 95 | } |
| 96 | } |
| 97 | } |