From 53193d84b8697786172e1bb5a24049982080faf0 Mon Sep 17 00:00:00 2001 From: Fries Date: Tue, 11 Jul 2023 01:04:53 -0700 Subject: [PATCH] add cache busting filenames and split off assets i abstraced the file handling out to a global static struct called "Files" that is only written to once, in main inside a OnceLock. i also split out asset handling into its own module folder called assets which has all the asset handling code. i also have a new crate called "proc_macros" which provides a attribute macro that adds the base_template field to each struct i decorate with it using the syn and quote crates. --- Cargo.lock | 23 ++++++++++ Cargo.toml | 9 +++- proc-macros/Cargo.toml | 13 ++++++ proc-macros/src/lib.rs | 35 +++++++++++++++ src/assets.rs | 92 --------------------------------------- src/assets/files.rs | 96 +++++++++++++++++++++++++++++++++++++++++ src/assets/mod.rs | 7 +++ src/assets/routes.rs | 62 ++++++++++++++++++++++++++ src/assets/templates.rs | 25 +++++++++++ src/main.rs | 2 + src/routes.rs | 57 ++++++++++++++++++------ templates/base.html | 48 ++++++++++++++++++++- 12 files changed, 361 insertions(+), 108 deletions(-) create mode 100644 proc-macros/Cargo.toml create mode 100644 proc-macros/src/lib.rs delete mode 100644 src/assets.rs create mode 100644 src/assets/files.rs create mode 100644 src/assets/mod.rs create mode 100644 src/assets/routes.rs create mode 100644 src/assets/templates.rs diff --git a/Cargo.lock b/Cargo.lock index 2194d7b..183d28a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,6 +614,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "http" version = "0.2.9" @@ -844,6 +850,12 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" + [[package]] name = "memchr" version = "2.5.0" @@ -856,8 +868,11 @@ version = "0.2.0" dependencies = [ "askama", "askama_rocket", + "hex", "log", + "md5", "notify", + "proc_macros", "rocket", "rocket_cors", "rust-embed", @@ -1096,6 +1111,14 @@ dependencies = [ "yansi", ] +[[package]] +name = "proc_macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "quote" version = "1.0.28" diff --git a/Cargo.toml b/Cargo.toml index 3ed4ca8..a0b6478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["cli", "shared"] +members = ["cli", "shared", "proc-macros"] [package] name = "meowy-webring" @@ -15,11 +15,13 @@ lto = "thin" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +hex = "0.4" log = "0.4" +md5 = "0.7" [dependencies.rocket] version = "=0.5.0-rc.3" -default_features = false +default-features = false features = ["json"] [dependencies.rust-embed] @@ -60,3 +62,6 @@ features = ["macos_fsevent"] [dependencies.rocket_cors] version = "=0.6.0-alpha2" default_features = false + +[dependencies.proc_macros] +path = "./proc-macros" diff --git a/proc-macros/Cargo.toml b/proc-macros/Cargo.toml new file mode 100644 index 0000000..27444fd --- /dev/null +++ b/proc-macros/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "proc_macros" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +syn = "2.0" +quote = "1.0" diff --git a/proc-macros/src/lib.rs b/proc-macros/src/lib.rs new file mode 100644 index 0000000..b6dcb4e --- /dev/null +++ b/proc-macros/src/lib.rs @@ -0,0 +1,35 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse::Parser, parse_macro_input, DeriveInput}; + +#[proc_macro_attribute] +pub fn uses_base_template(_attr: TokenStream, item: TokenStream) -> TokenStream { + let mut input = parse_macro_input!(item as DeriveInput); + + let base_template_field = syn::Field::parse_named + .parse2( + quote! { + pub base_template: BaseTemplate + } + .into(), + ) + .unwrap(); + + match &mut input.data { + syn::Data::Struct(ref mut struct_data) => { + match &mut struct_data.fields { + syn::Fields::Named(fields) => { + fields.named.push(base_template_field); + } + _ => (), + } + return quote! { + #input + } + .into(); + } + _ => { + panic!("bad") + } + }; +} diff --git a/src/assets.rs b/src/assets.rs deleted file mode 100644 index 683cd94..0000000 --- a/src/assets.rs +++ /dev/null @@ -1,92 +0,0 @@ -use askama_rocket::Template; -use rocket::{http::Status, response::content::RawCss}; -use rust_embed::{EmbeddedFile, RustEmbed}; -use shared::names::Site; -use std::borrow::Cow; - -#[derive(RustEmbed)] -#[folder = "public/"] -pub struct PublicAssets; - -impl PublicAssets { - fn get_asset(file_path: &str) -> Result { - match PublicAssets::get(file_path) { - Some(asset) => Ok(asset), - None => Err(Status::NotFound), - } - } - fn get_string(file_path: &str) -> Result { - let asset = PublicAssets::get_asset(file_path)?; - match std::str::from_utf8(&asset.data) { - Ok(string) => Ok(string.to_string()), - Err(_) => Err(Status::InternalServerError), - } - } -} - -#[derive(Responder)] -#[response(status = 200, content_type = "font/woff2")] -pub struct RawWoff2Font(pub Cow<'static, [u8]>); - -#[derive(Responder)] -#[response(status = 200, content_type = "font/woff")] -pub struct RawWoffFont(pub Cow<'static, [u8]>); - -#[derive(Template)] -#[template(path = "error.html")] -pub struct ErrorTemplate<'a> { - pub error: &'a str, - pub error_description: &'a str, -} - -#[derive(Template)] -#[template(path = "index.html")] -pub struct IndexTemplate { - pub sites: Vec, -} - -#[derive(Responder)] -pub struct ErrorTemplateResponder<'a> { - template: ErrorTemplate<'a>, -} - -#[get("/css/ + {% endblock %}