From 9f7b903901acb0cd6ec9cb2146406a92ebf79cab Mon Sep 17 00:00:00 2001 From: Vika Date: Fri, 7 Oct 2022 19:53:04 +0300 Subject: templates: move static assets to the templates crate It makes more sense to keep CSS near the templates, and the client-side JavaScript code too, since it depends on the DOM structure to work. Additionally, the overhead of `include_dir!()` is almost completely mitigated by the fact that this is a separate crate that isn't recompiled often. The linking stage, however, is still expected to take a little bit long. But I doubt it'd be longer than what it was before, since it's the same exact files that get linked into the app. --- kittybox-rs/src/frontend/mod.rs | 47 ++++++----------------------------------- 1 file changed, 6 insertions(+), 41 deletions(-) (limited to 'kittybox-rs/src/frontend/mod.rs') diff --git a/kittybox-rs/src/frontend/mod.rs b/kittybox-rs/src/frontend/mod.rs index 58de39d..f0f4e5a 100644 --- a/kittybox-rs/src/frontend/mod.rs +++ b/kittybox-rs/src/frontend/mod.rs @@ -12,7 +12,12 @@ use tracing::{debug, error}; //pub mod login; pub mod onboarding; -use kittybox_templates::{Entry, ErrorPage, Feed, MainPage, Template, VCard, POSTS_PER_PAGE}; +use kittybox_frontend_renderer::{ + Entry, Feed, VCard, + ErrorPage, Template, MainPage, + POSTS_PER_PAGE +}; +pub use kittybox_frontend_renderer::assets::statics; #[derive(Debug, Deserialize)] pub struct QueryParams { @@ -266,43 +271,3 @@ pub async fn catchall( } } } - -const STYLE_CSS: &[u8] = include_bytes!("./style.css"); -// XXX const path handling is ugly, and concat!() doesn't take -// constants, only literals... how annoying! -// -// This might break compiling on inferior operating systems that use -// backslashes as their path separator -const ONBOARDING_JS: &[u8] = include_bytes!(concat!( - env!("OUT_DIR"), "/", "kittybox_js", "/", "onboarding.js" -)); -const ONBOARDING_CSS: &[u8] = include_bytes!("./onboarding.css"); -const INDIEAUTH_JS: &[u8] = include_bytes!(concat!( - env!("OUT_DIR"), "/", "kittybox_js", "/", "indieauth.js" -)); -const LIB_JS: &[u8] = include_bytes!(concat!( - env!("OUT_DIR"), "/", "kittybox_js", "/", "lib.js" -)); -const JSLABELS_HTML: &[u8] = include_bytes!("../../javascript/jslicense.html"); -const MIME_JS: &str = "application/javascript"; -const MIME_CSS: &str = "text/css"; -const MIME_PLAIN: &str = "text/plain"; -const MIME_HTML: &str = "text/html; charset=utf-8"; - -pub async fn statics(Path(name): Path) -> impl IntoResponse { - use axum::http::header::CONTENT_TYPE; - - match name.as_str() { - "style.css" => (StatusCode::OK, [(CONTENT_TYPE, MIME_CSS)], STYLE_CSS), - "onboarding.js" => (StatusCode::OK, [(CONTENT_TYPE, MIME_JS)], ONBOARDING_JS), - "onboarding.css" => (StatusCode::OK, [(CONTENT_TYPE, MIME_CSS)], ONBOARDING_CSS), - "indieauth.js" => (StatusCode::OK, [(CONTENT_TYPE, MIME_JS)], INDIEAUTH_JS), - "lib.js" => (StatusCode::OK, [(CONTENT_TYPE, MIME_JS)], LIB_JS), - "jslicense.html" => (StatusCode::OK, [(CONTENT_TYPE, MIME_HTML)], JSLABELS_HTML), - _ => ( - StatusCode::NOT_FOUND, - [(CONTENT_TYPE, MIME_PLAIN)], - "not found".as_bytes(), - ), - } -} -- cgit 1.4.1