From 38994a86aeb25c2f783461069a57887dd37f2124 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 3 Mar 2025 19:36:32 +0100 Subject: [PATCH] Serve new UI from axum server --- showbits-assets/src/lib.rs | 6 +- .../public}/photo.html | 0 showbits-thermal-printer/dist | 1 + showbits-thermal-printer/src/server.rs | 10 ++- showbits-thermal-printer/src/server/static.rs | 87 +++++++++---------- showbits-thermal-printer/static/index.html | 16 ---- 6 files changed, 51 insertions(+), 69 deletions(-) rename {showbits-thermal-printer/static => showbits-thermal-printer-ui/public}/photo.html (100%) create mode 120000 showbits-thermal-printer/dist delete mode 100644 showbits-thermal-printer/static/index.html diff --git a/showbits-assets/src/lib.rs b/showbits-assets/src/lib.rs index 9469e8c..acb7b98 100644 --- a/showbits-assets/src/lib.rs +++ b/showbits-assets/src/lib.rs @@ -1,8 +1,10 @@ pub const UNIFONT: &[u8] = include_bytes!("../data/unifont-15.1.05.otf"); pub const UNIFONT_JP: &[u8] = include_bytes!("../data/unifont_jp-15.1.05.otf"); pub const UNIFONT_UPPER: &[u8] = include_bytes!("../data/unifont_upper-15.1.05.otf"); -pub const UNIFONT_NAME: &str = "Unifont"; -pub const UNIFONT_SIZE: f32 = 16.0; + +pub const UNIFONT_NAME: &str = "unifont-15.1.05.otf"; +pub const UNIFONT_JP_NAME: &str = "unifont_jp-15.1.05.otf"; +pub const UNIFONT_UPPER_NAME: &str = "unifont_upper-15.1.05.otf"; pub const EGG_BAD_COVERS: &[&[u8]] = &[include_bytes!("../data/egg_bad/cover_00.png")]; pub const EGG_BAD_PATTERNS: &[&[u8]] = &[ diff --git a/showbits-thermal-printer/static/photo.html b/showbits-thermal-printer-ui/public/photo.html similarity index 100% rename from showbits-thermal-printer/static/photo.html rename to showbits-thermal-printer-ui/public/photo.html diff --git a/showbits-thermal-printer/dist b/showbits-thermal-printer/dist new file mode 120000 index 0000000..9abc991 --- /dev/null +++ b/showbits-thermal-printer/dist @@ -0,0 +1 @@ +../showbits-thermal-printer-ui/dist \ No newline at end of file diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 6181041..45203f0 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -12,8 +12,6 @@ use tokio::{net::TcpListener, sync::mpsc}; use crate::{documents, drawer::Command}; -use self::r#static::get_static_file; - #[derive(Clone)] pub struct Server { tx: mpsc::Sender, @@ -27,6 +25,12 @@ impl Server { pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> { let app = Router::new() + // Files + .route("/", get(r#static::get_index)) + .route("/assets/{*path}", get(r#static::get_asset)) + .route("/fonts/{*path}", get(r#static::get_font)) + .route("/photo.html", get(r#static::get_photo)) + // API .route("/api/calendar", post(documents::calendar::post)) .route("/api/cells", post(documents::cells::post)) .route("/api/chat", post(documents::chat::post)) @@ -34,7 +38,7 @@ pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> .route("/api/image", post(documents::image::post)) .route("/api/text", post(documents::text::post)) .route("/api/tictactoe", post(documents::tictactoe::post)) - .fallback(get(get_static_file)) + // Rest .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .with_state(Server { tx }); diff --git a/showbits-thermal-printer/src/server/static.rs b/showbits-thermal-printer/src/server/static.rs index 449b62f..d02d383 100644 --- a/showbits-thermal-printer/src/server/static.rs +++ b/showbits-thermal-printer/src/server/static.rs @@ -1,62 +1,53 @@ use axum::{ - http::{StatusCode, Uri, header}, + extract::Path, + http::{StatusCode, header}, response::{IntoResponse, Response}, }; use rust_embed::RustEmbed; +use showbits_assets::{ + UNIFONT, UNIFONT_JP, UNIFONT_JP_NAME, UNIFONT_NAME, UNIFONT_UPPER, UNIFONT_UPPER_NAME, +}; use super::statuscode::status_code; #[derive(RustEmbed)] -#[folder = "static"] -struct StaticFiles; +#[folder = "dist/assets"] +struct Assets; -struct StaticFile(String); - -fn look_up_path(path: &str) -> Option { - let path = path.trim_start_matches('/'); - let file = StaticFiles::get(path)?; - let mime = mime_guess::from_path(path).first_or_octet_stream(); - let response = ([(header::CONTENT_TYPE, mime.as_ref())], file.data).into_response(); - Some(response) -} - -impl IntoResponse for StaticFile { - fn into_response(self) -> Response { - let mut path = self.0; - if path.is_empty() { - path.push('/') - }; - - if path.ends_with(".html") { - // A file `/foo/bar.html` should not be accessible directly, only - // indirectly at `/foo/bar`. - return status_code(StatusCode::NOT_FOUND); +pub async fn get_asset(Path(path): Path) -> impl IntoResponse { + match Assets::get(&path) { + None => status_code(StatusCode::NOT_FOUND), + Some(content) => { + let mime = mime_guess::from_path(&path).first_or_octet_stream(); + ([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response() } - - if path.ends_with("/index") { - // A file `/foo/index.html` should not be accessible directly, only - // indirectly at `/foo/`. - return status_code(StatusCode::NOT_FOUND); - } - - if path.ends_with('/') { - path.push_str("index"); - } - - if let Some(response) = look_up_path(&path) { - return response; - } - - path.push_str(".html"); - - if let Some(response) = look_up_path(&path) { - return response; - } - - status_code(StatusCode::NOT_FOUND) } } -pub async fn get_static_file(uri: Uri) -> impl IntoResponse { - StaticFile(uri.path().to_string()) +pub async fn get_font(Path(path): Path) -> Response { + let font = if path == UNIFONT_NAME { + UNIFONT + } else if path == UNIFONT_JP_NAME { + UNIFONT_JP + } else if path == UNIFONT_UPPER_NAME { + UNIFONT_UPPER + } else { + return status_code(StatusCode::NOT_FOUND); + }; + + ([(header::CONTENT_TYPE, "font/otf")], font).into_response() +} + +pub async fn get_index() -> impl IntoResponse { + ( + [(header::CONTENT_TYPE, "text/html; charset=utf-8")], + include_str!("../../dist/index.html"), + ) +} + +pub async fn get_photo() -> impl IntoResponse { + ( + [(header::CONTENT_TYPE, "text/html; charset=utf-8")], + include_str!("../../dist/photo.html"), + ) } diff --git a/showbits-thermal-printer/static/index.html b/showbits-thermal-printer/static/index.html deleted file mode 100644 index b7f0887..0000000 --- a/showbits-thermal-printer/static/index.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - TP: Index - - -

Thermal Printer Control

- - -