diff --git a/assets/sheets/index.css b/assets/sheets/index.css new file mode 100644 index 0000000..aacbba9 --- /dev/null +++ b/assets/sheets/index.css @@ -0,0 +1 @@ +/* uwu */ diff --git a/src/bin/stranger-site-server.rs b/src/bin/stranger-site-server.rs index d512de4..67dea3b 100644 --- a/src/bin/stranger-site-server.rs +++ b/src/bin/stranger-site-server.rs @@ -2,7 +2,10 @@ use axum::{routing::get, Router}; use std::{net::SocketAddr, sync::Arc}; use color_eyre::eyre::{Context, Result}; -use stranger_site::{routes::index::index, AppState}; +use stranger_site::{ + routes::{index::index, sheets::sheet}, + AppState, +}; #[tokio::main] async fn main() -> Result<()> { @@ -15,6 +18,7 @@ async fn main() -> Result<()> { .route("/", get(index)) .route("/index", get(index)) .route("/index.html", get(index)) + .route("/sheets/:sheet", get(sheet)) .with_state(Arc::new(shared_state)); // run it diff --git a/src/lib.rs b/src/lib.rs index 0f5cf1c..dfbff5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use color_eyre::eyre::{Context, Result}; use tera::Tera; @@ -7,6 +9,7 @@ pub mod routes; pub struct AppState { pub title: String, pub tera: Tera, + pub sheets: HashMap, } impl AppState { @@ -15,6 +18,16 @@ impl AppState { let mut tera = Tera::default(); tera.add_raw_template("index.html", include_str!("../assets/templates/index.html")) .context("Failed to load built in template for index.html")?; - Ok(AppState { title, tera }) + let sheets = [( + "index.css".to_string(), + include_str!("../assets/sheets/index.css"), + )] + .into_iter() + .collect(); + Ok(AppState { + title, + tera, + sheets, + }) } } diff --git a/src/routes.rs b/src/routes.rs index 33edc95..57ed761 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1 +1,2 @@ pub mod index; +pub mod sheets; diff --git a/src/routes/sheets.rs b/src/routes/sheets.rs new file mode 100644 index 0000000..d851fcb --- /dev/null +++ b/src/routes/sheets.rs @@ -0,0 +1,40 @@ +use std::sync::Arc; + +use axum::{ + extract::{Path, State}, + http::{header::CONTENT_TYPE, HeaderValue, Response, StatusCode}, +}; +use color_eyre::eyre::ContextCompat; + +use crate::AppState; + +/// Return the given sheet +async fn sheet_inner( + State(state): State>, + Path(path): Path, +) -> color_eyre::eyre::Result> { + eprintln!("Returning sheet: {path}"); + let sheet = state + .sheets + .get(&path) + .context("No sheet with given name found")? + .to_string(); + let mut response = Response::new(sheet); + response + .headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("text/css")); + Ok(response) +} + +pub async fn sheet( + path: Path, + state: State>, +) -> Result, (StatusCode, String)> { + match sheet_inner(state, path).await { + Ok(x) => Ok(x), + Err(err) => { + let err = format!("{:?}", err); + Err((StatusCode::NOT_FOUND, err)) + } + } +}