about summary refs log tree commit diff
path: root/kittybox-rs/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/src/lib.rs')
-rw-r--r--kittybox-rs/src/lib.rs71
1 files changed, 70 insertions, 1 deletions
diff --git a/kittybox-rs/src/lib.rs b/kittybox-rs/src/lib.rs
index c1683d9..3f25689 100644
--- a/kittybox-rs/src/lib.rs
+++ b/kittybox-rs/src/lib.rs
@@ -7,5 +7,74 @@ pub mod frontend;
 pub mod tokenauth;
 pub mod media;
 pub mod micropub;
+pub mod indieauth;
 
-pub static MICROPUB_CLIENT: &[u8] = include_bytes!("./index.html");
+pub mod companion {
+    use axum::{
+        extract::{Extension, Path},
+        response::IntoResponse
+    };
+
+    #[derive(Debug, Clone, Copy)]
+    struct Resource {
+        data: &'static [u8],
+        mime: &'static str
+    }
+
+    type ResourceTable = std::sync::Arc<std::collections::HashMap<&'static str, Resource>>;
+
+    #[tracing::instrument]
+    async fn map_to_static(
+        Path(name): Path<String>,
+        Extension(resources): Extension<ResourceTable>
+    ) -> impl IntoResponse {
+        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())
+        }
+    }
+
+    pub fn router() -> axum::Router {
+        let resources = {
+            let mut map = std::collections::HashMap::new();
+
+            macro_rules! register_resource {
+                ($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)),+);
+                }};
+            }
+
+            register_resource! {
+                "../companion-lite/",
+                ("index.html", "text/html; charset=\"utf-8\""),
+                ("main.js", "text/javascript"),
+                ("micropub_api.js", "text/javascript"),
+                ("style.css", "text/css")
+            };
+
+            std::sync::Arc::new(map)
+        };
+
+        axum::Router::new()
+            .route(
+                "/:filename",
+                axum::routing::get(map_to_static)
+                    .layer(Extension(resources))
+            )
+    }
+}