aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/style.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/ui/style.rs')
-rw-r--r--server/src/ui/style.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/server/src/ui/style.rs b/server/src/ui/style.rs
new file mode 100644
index 0000000..77f0fe1
--- /dev/null
+++ b/server/src/ui/style.rs
@@ -0,0 +1,90 @@
+/*
+ This file is part of jellything (https://codeberg.org/metamuffin/jellything)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2025 metamuffin <metamuffin.org>
+ Copyright (C) 2023 tpart
+*/
+use rocket::{
+ get,
+ http::{ContentType, Header},
+ response::Responder,
+};
+
+macro_rules! concat_files {
+ ([$base: expr], $($files:literal),*) => {{
+ #[cfg(any(debug_assertions, feature = "hot-css"))]
+ {
+ use std::{fs::read_to_string, path::PathBuf, str::FromStr};
+ [ $($files),* ]
+ .into_iter()
+ .map(|n| {
+ read_to_string({
+ let p = PathBuf::from_str(file!()).unwrap().parent().unwrap().join($base).join(n);
+ log::info!("load {p:?}");
+ p
+ })
+ .unwrap()
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+ }
+ #[cfg(not(any(debug_assertions, feature = "hot-css")))]
+ concat!($(include_str!(concat!($base, "/", $files))),*).to_string()
+ }};
+}
+
+fn css_bundle() -> String {
+ concat_files!(
+ ["../../../web/style"],
+ "layout.css",
+ "player.css",
+ "nodepage.css",
+ "nodecard.css",
+ "js-player.css",
+ "js-transition.css",
+ "forms.css",
+ "props.css",
+ "themes.css",
+ "navbar.css"
+ )
+}
+
+pub struct CachedAsset<T>(pub T);
+impl<'r, 'o: 'r, T: Responder<'r, 'o>> Responder<'r, 'o> for CachedAsset<T> {
+ fn respond_to(self, request: &'r rocket::Request<'_>) -> rocket::response::Result<'o> {
+ let mut res = self.0.respond_to(request)?;
+ if cfg!(not(debug_assertions)) {
+ res.set_header(Header::new("cache-control", "max-age=86400"));
+ }
+ Ok(res)
+ }
+}
+
+fn js_bundle() -> String {
+ concat_files!([env!("OUT_DIR")], "bundle.js")
+}
+fn js_bundle_map() -> String {
+ concat_files!([env!("OUT_DIR")], "bundle.js.map")
+}
+
+#[get("/assets/style.css")]
+pub fn r_assets_style() -> CachedAsset<(ContentType, String)> {
+ CachedAsset((ContentType::CSS, css_bundle()))
+}
+
+#[get("/assets/cantarell.woff2")]
+pub fn r_assets_font() -> CachedAsset<(ContentType, &'static [u8])> {
+ CachedAsset((
+ ContentType::WOFF2,
+ include_bytes!("../../../web/cantarell.woff2"),
+ ))
+}
+
+#[get("/assets/bundle.js")]
+pub fn r_assets_js() -> CachedAsset<(ContentType, String)> {
+ CachedAsset((ContentType::JavaScript, js_bundle()))
+}
+#[get("/assets/bundle.js.map")]
+pub fn r_assets_js_map() -> CachedAsset<(ContentType, String)> {
+ CachedAsset((ContentType::JSON, js_bundle_map()))
+}