From fe116a8117bf398a83bb25efbd5c866fc33c3d4f Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 19 Sep 2022 17:31:57 +0300 Subject: Small optimizations to frontend code There is a possibility of refactoring some of the companion code to act as a generic embedded asset framework and put it in the `util` crate. --- kittybox-rs/src/lib.rs | 52 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'kittybox-rs/src/lib.rs') diff --git a/kittybox-rs/src/lib.rs b/kittybox-rs/src/lib.rs index 3f25689..a75c3ea 100644 --- a/kittybox-rs/src/lib.rs +++ b/kittybox-rs/src/lib.rs @@ -10,9 +10,10 @@ pub mod micropub; pub mod indieauth; pub mod companion { + use std::{collections::HashMap, sync::Arc}; use axum::{ extract::{Extension, Path}, - response::IntoResponse + response::{IntoResponse, Response} }; #[derive(Debug, Clone, Copy)] @@ -21,45 +22,54 @@ pub mod companion { mime: &'static str } - type ResourceTable = std::sync::Arc>; + impl IntoResponse for &Resource { + fn into_response(self) -> Response { + (axum::http::StatusCode::OK, + [("Content-Type", self.mime)], + self.data).into_response() + } + } + + // TODO replace with the "phf" crate someday + type ResourceTable = Arc>; #[tracing::instrument] async fn map_to_static( Path(name): Path, Extension(resources): Extension - ) -> impl IntoResponse { + ) -> Response { tracing::debug!("Searching for {} in the resource table...", name); - if let Some(res) = resources.get(name.as_str()) { - (axum::http::StatusCode::OK, - [("Content-Type", res.mime)], - res.data) - } else { - #[cfg(debug_assertions)] - tracing::error!("Not found"); - (axum::http::StatusCode::NOT_FOUND, - [("Content-Type", "text/plain")], - "Not found. Sorry.".as_bytes()) + match resources.get(name.as_str()) { + Some(res) => res.into_response(), + None => { + #[cfg(debug_assertions)] tracing::error!("Not found"); + + (axum::http::StatusCode::NOT_FOUND, + [("Content-Type", "text/plain")], + "Not found. Sorry.".as_bytes()).into_response() + } } } pub fn router() -> axum::Router { - let resources = { - let mut map = std::collections::HashMap::new(); + let resources: ResourceTable = { + let mut map = HashMap::new(); macro_rules! register_resource { - ($prefix:literal, ($filename:literal, $mime:literal)) => {{ - map.insert($filename, Resource { + ($map:ident, $prefix:literal, ($filename:literal, $mime:literal)) => {{ + $map.insert($filename, Resource { data: include_bytes!(concat!($prefix, $filename)), mime: $mime }) }}; - ($prefix:literal, ($filename:literal, $mime:literal), $( ($f:literal, $m:literal) ),+) => {{ - register_resource!($prefix, ($filename, $mime)); - register_resource!($prefix, $(($f, $m)),+); + ($map:ident, $prefix:literal, ($filename:literal, $mime:literal), $( ($f:literal, $m:literal) ),+) => {{ + register_resource!($map, $prefix, ($filename, $mime)); + register_resource!($map, $prefix, $(($f, $m)),+); }}; } register_resource! { + map, "../companion-lite/", ("index.html", "text/html; charset=\"utf-8\""), ("main.js", "text/javascript"), @@ -67,7 +77,7 @@ pub mod companion { ("style.css", "text/css") }; - std::sync::Arc::new(map) + Arc::new(map) }; axum::Router::new() -- cgit 1.4.1