diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..24a8e87 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.png filter=lfs diff=lfs merge=lfs -text diff --git a/assets/images/logo.png b/assets/images/logo.png new file mode 100644 index 0000000..74007e6 --- /dev/null +++ b/assets/images/logo.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:70bf47d91b3c06ae4cac7a4653ae1609ce8e875286776a48187617754428ad0f +size 330825 diff --git a/src/bin/stranger-site-server.rs b/src/bin/stranger-site-server.rs index 67dea3b..3f1602a 100644 --- a/src/bin/stranger-site-server.rs +++ b/src/bin/stranger-site-server.rs @@ -3,7 +3,7 @@ use std::{net::SocketAddr, sync::Arc}; use color_eyre::eyre::{Context, Result}; use stranger_site::{ - routes::{index::index, sheets::sheet}, + routes::{images::image, index::index, sheets::sheet}, AppState, }; @@ -19,6 +19,7 @@ async fn main() -> Result<()> { .route("/index", get(index)) .route("/index.html", get(index)) .route("/sheets/:sheet", get(sheet)) + .route("/images/:image", get(image)) .with_state(Arc::new(shared_state)); // run it diff --git a/src/lib.rs b/src/lib.rs index dfbff5e..b0207c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ pub struct AppState { pub title: String, pub tera: Tera, pub sheets: HashMap, + pub images: HashMap, } impl AppState { @@ -24,10 +25,17 @@ impl AppState { )] .into_iter() .collect(); + let images = [( + "logo.png".to_string(), + include_bytes!("../assets/images/logo.png").as_slice(), + )] + .into_iter() + .collect(); Ok(AppState { title, tera, sheets, + images, }) } } diff --git a/src/routes.rs b/src/routes.rs index 57ed761..7ea4a07 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,2 +1,3 @@ +pub mod images; pub mod index; pub mod sheets; diff --git a/src/routes/images.rs b/src/routes/images.rs new file mode 100644 index 0000000..1568127 --- /dev/null +++ b/src/routes/images.rs @@ -0,0 +1,41 @@ +use std::sync::Arc; + +use axum::{ + body::{Bytes, Full}, + extract::{Path, State}, + http::{header::CONTENT_TYPE, HeaderValue, Response, StatusCode}, +}; +use color_eyre::eyre::ContextCompat; + +use crate::AppState; + +/// Return the given image +async fn image_inner( + State(state): State>, + Path(path): Path, +) -> color_eyre::eyre::Result>> { + eprintln!("Returning image: {path}"); + let image = state + .images + .get(&path) + .context("No image with given name found")? + .to_vec(); + let mut response = Response::new(Full::new(Bytes::from(image))); + response + .headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("image/png")); + Ok(response) +} + +pub async fn image( + path: Path, + state: State>, +) -> Result>, (StatusCode, String)> { + match image_inner(state, path).await { + Ok(x) => Ok(x), + Err(err) => { + let err = format!("{:?}", err); + Err((StatusCode::NOT_FOUND, err)) + } + } +}