From c00547c8437b992aa073378ab165aa40b073e1b4 Mon Sep 17 00:00:00 2001 From: Vika Date: Fri, 15 Jul 2022 02:18:14 +0300 Subject: PoC for modularity and WIP built-in Micropub client rework This is the living, breathing proof that Kittybox can be split into independent components without sacrificing any functionality. Just make sure all neccesary backing storage components are available to the modules that need them. Also the Micropub client was split into several files, because it's about to get much bigger and more full-featured. Yes, I am going to write it in vanilla JavaScript. I don't trust anything from NPM to run on my computer. Not anymore. Not after the node-ipc malware fiasco. And I am definitely not going to spin up a VM or a Docker container (who uses Docker containers as a security measure?) to hack on my own code. Cargo can at least be sandboxed inside Nix, where it can't do much harm. NPM basically requires unrestricted network access to download dependencies, and it runs arbitrary code upon **downloading** them. Cargo and rust-analyzer, on the other hand, can be configured to not trust the source code and its dependencies (for example, Cargo doesn't execute code on fetching dependencies - only on building, and rust-analyzer's proc-macro expansion support can be sacrificed for more security). --- kittybox-rs/src/lib.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'kittybox-rs/src/lib.rs') 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>; + + #[tracing::instrument] + async fn map_to_static( + Path(name): Path, + Extension(resources): Extension + ) -> 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)) + ) + } +} -- cgit 1.4.1