1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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) 2023 metamuffin <metamuffin.org>
Copyright (C) 2023 tpart
*/
use rocket::{
get,
http::{ContentType, Header},
response::Responder,
};
macro_rules! concat_files {
([$base: expr], $($files:literal),*) => {{
#[cfg(debug_assertions)]
{
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(debug_assertions))]
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()))
}
|