about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2023-07-29 21:59:56 +0300
committerVika <vika@fireburn.ru>2023-07-29 21:59:56 +0300
commit0617663b249f9ca488e5de652108b17d67fbaf45 (patch)
tree11564b6c8fa37bf9203a0a4cc1c4e9cc088cb1a5 /src/lib.rs
parent26c2b79f6a6380ae3224e9309b9f3352f5717bd7 (diff)
Moved the entire Kittybox tree into the root
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..c1bd965
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,93 @@
+#![forbid(unsafe_code)]
+#![warn(clippy::todo)]
+
+/// Database abstraction layer for Kittybox, allowing the CMS to work with any kind of database.
+pub mod database;
+pub mod frontend;
+pub mod media;
+pub mod micropub;
+pub mod indieauth;
+pub mod webmentions;
+
+pub mod companion {
+    use std::{collections::HashMap, sync::Arc};
+    use axum::{
+        extract::{Extension, Path},
+        response::{IntoResponse, Response}
+    };
+
+    #[derive(Debug, Clone, Copy)]
+    struct Resource {
+        data: &'static [u8],
+        mime: &'static str
+    }
+
+    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>
+    ) -> Response {
+        tracing::debug!("Searching for {} in the resource table...", name);
+        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()
+            }
+        }
+    }
+
+    #[must_use]
+    pub fn router() -> axum::Router {
+        let resources: ResourceTable = {
+            let mut map = HashMap::new();
+
+            macro_rules! register_resource {
+                ($map:ident, $prefix:expr, ($filename:literal, $mime:literal)) => {{
+                    $map.insert($filename, Resource {
+                        data: include_bytes!(concat!($prefix, $filename)),
+                        mime: $mime
+                    })
+                }};
+                ($map:ident, $prefix:expr, ($filename:literal, $mime:literal), $( ($f:literal, $m:literal) ),+) => {{
+                    register_resource!($map, $prefix, ($filename, $mime));
+                    register_resource!($map, $prefix, $(($f, $m)),+);
+                }};
+            }
+
+            register_resource! {
+                map,
+                concat!(env!("OUT_DIR"), "/", "companion", "/"),
+                ("index.html", "text/html; charset=\"utf-8\""),
+                ("main.js", "text/javascript"),
+                ("micropub_api.js", "text/javascript"),
+                ("indieauth.js", "text/javascript"),
+                ("base64.js", "text/javascript"),
+                ("style.css", "text/css")
+            };
+
+            Arc::new(map)
+        };
+
+        axum::Router::new()
+            .route(
+                "/:filename",
+                axum::routing::get(map_to_static)
+                    .layer(Extension(resources))
+            )
+    }
+}