about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-09-19 17:31:57 +0300
committerVika <vika@fireburn.ru>2022-09-19 17:31:57 +0300
commitfe116a8117bf398a83bb25efbd5c866fc33c3d4f (patch)
tree3df00dd52d599c6e887d401298017a5cd061673e
parent66049566ae865e1a4bd049257d6afc0abded16e9 (diff)
downloadkittybox-fe116a8117bf398a83bb25efbd5c866fc33c3d4f.tar.zst
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.
-rw-r--r--kittybox-rs/src/frontend/mod.rs13
-rw-r--r--kittybox-rs/src/lib.rs52
2 files changed, 38 insertions, 27 deletions
diff --git a/kittybox-rs/src/frontend/mod.rs b/kittybox-rs/src/frontend/mod.rs
index 0797ba6..e2187c9 100644
--- a/kittybox-rs/src/frontend/mod.rs
+++ b/kittybox-rs/src/frontend/mod.rs
@@ -267,13 +267,14 @@ pub async fn catchall<D: Storage>(
     }
 }
 
-static STYLE_CSS: &[u8] = include_bytes!("./style.css");
-static ONBOARDING_JS: &[u8] = include_bytes!("./onboarding.js");
-static ONBOARDING_CSS: &[u8] = include_bytes!("./onboarding.css");
+const STYLE_CSS: &[u8] = include_bytes!("./style.css");
+const ONBOARDING_JS: &[u8] = include_bytes!("./onboarding.js");
+const ONBOARDING_CSS: &[u8] = include_bytes!("./onboarding.css");
+const INDIEAUTH_JS: &[u8] = include_bytes!("./indieauth.js");
 
-static MIME_JS: &str = "application/javascript";
-static MIME_CSS: &str = "text/css";
-static MIME_PLAIN: &str = "text/plain";
+const MIME_JS: &str = "application/javascript";
+const MIME_CSS: &str = "text/css";
+const MIME_PLAIN: &str = "text/plain";
 
 pub async fn statics(Path(name): Path<String>) -> impl IntoResponse {
     use axum::http::header::CONTENT_TYPE;
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<std::collections::HashMap<&'static str, Resource>>;
+    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<HashMap<&'static str, Resource>>;
 
     #[tracing::instrument]
     async fn map_to_static(
         Path(name): Path<String>,
         Extension(resources): Extension<ResourceTable>
-    ) -> 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()