sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / docs / archive / drakestapleton.com-old / crates / site-server / src / routes / pages.rs
| 1 | use axum::{ |
| 2 | body::Bytes, |
| 3 | extract::{OriginalUri, State}, |
| 4 | http::{header, HeaderMap, HeaderValue, StatusCode}, |
| 5 | response::{IntoResponse, Response}, |
| 6 | }; |
| 7 | use crate::state::AppState; |
| 8 | |
| 9 | pub async fn serve_page( |
| 10 | State(state): State<AppState>, |
| 11 | OriginalUri(uri): OriginalUri, |
| 12 | ) -> Response { |
| 13 | let path = uri.path(); |
| 14 | |
| 15 | // Normalize trailing slash (except for root "/") |
| 16 | let normalized = if path.len() > 1 && path.ends_with('/') { |
| 17 | path.trim_end_matches('/') |
| 18 | } else { |
| 19 | path |
| 20 | }; |
| 21 | |
| 22 | if let Some(content) = state.routes.get(normalized) { |
| 23 | let mut headers = HeaderMap::new(); |
| 24 | headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8")); |
| 25 | headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("public, max-age=300, stale-while-revalidate=86400")); |
| 26 | (StatusCode::OK, headers, content.clone()).into_response() |
| 27 | } else { |
| 28 | let not_found_bytes = state |
| 29 | .routes |
| 30 | .get("/404") |
| 31 | .cloned() |
| 32 | .unwrap_or_else(|| Bytes::from_static(b"<h1>404 - Page Not Found</h1>")); |
| 33 | let mut headers = HeaderMap::new(); |
| 34 | headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8")); |
| 35 | (StatusCode::NOT_FOUND, headers, not_found_bytes).into_response() |
| 36 | } |
| 37 | } |