aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/style.rs
blob: b2a21896f94f78cf3a6370543762cd19d987f75b (plain)
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
/*
    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) 2026 metamuffin <metamuffin.org>
    Copyright (C) 2023 tpart
*/
use jellyui::{css_bundle, js_bundle, js_bundle_map};
use rocket::{
    get,
    http::{ContentType, Header},
    response::Responder,
};
use std::borrow::Cow;

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)
    }
}

#[get("/assets/bundle.css")]
pub fn r_assets_css() -> CachedAsset<(ContentType, Cow<'static, str>)> {
    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, Cow<'static, str>)> {
    CachedAsset((ContentType::JavaScript, js_bundle()))
}
#[get("/assets/bundle.js.map")]
pub fn r_assets_js_map() -> CachedAsset<(ContentType, Cow<'static, str>)> {
    CachedAsset((ContentType::JSON, js_bundle_map()))
}