Skip to main content

fedimint_ui_common/
assets.rs

1use axum::Router;
2use axum::http::header::{CACHE_CONTROL, CONTENT_TYPE};
3use axum::response::{IntoResponse, Response};
4use axum::routing::get;
5
6// Asset route constants
7pub const BOOTSTRAP_CSS_ROUTE: &str = "/assets/bootstrap.min.css";
8pub const BOOTSTRAP_JS_ROUTE: &str = "/assets/bootstrap.bundle.min.js";
9pub const BOOTSTRAP_ICONS_CSS_ROUTE: &str = "/assets/bootstrap-icons.min.css";
10pub const BOOTSTRAP_ICONS_WOFF2_ROUTE: &str = "/assets/fonts/bootstrap-icons.woff2";
11pub const BOOTSTRAP_ICONS_WOFF_ROUTE: &str = "/assets/fonts/bootstrap-icons.woff";
12pub const HTMX_JS_ROUTE: &str = "/assets/htmx.org-2.0.4.min.js";
13pub const HTML5_QRCODE_JS_ROUTE: &str = "/assets/html5-qrcode.min.js";
14pub const CHARTJS_ROUTE: &str = "/assets/chart.umd.min.js";
15pub const STYLE_CSS_ROUTE: &str = "/assets/style.css";
16pub const LOGO_PNG_ROUTE: &str = "/assets/logo.png";
17
18pub(crate) fn get_static_asset(content_type: &'static str, body: &'static [u8]) -> Response {
19    (
20        [(CONTENT_TYPE, content_type)],
21        [(CACHE_CONTROL, format!("public, max-age={}", 60 * 60))],
22        body,
23    )
24        .into_response()
25}
26
27pub(crate) fn get_static_css(body: &'static str) -> Response {
28    get_static_asset("text/css", body.as_bytes())
29}
30
31pub(crate) fn get_static_png(body: &'static [u8]) -> Response {
32    get_static_asset("image/png", body)
33}
34
35pub(crate) fn get_static_js(body: &'static str) -> Response {
36    get_static_asset("application/javascript", body.as_bytes())
37}
38
39fn get_static_font(body: &'static [u8]) -> Response {
40    get_static_asset("font/woff2", body)
41}
42
43fn get_static_woff(body: &'static [u8]) -> Response {
44    get_static_asset("font/woff", body)
45}
46
47pub trait WithStaticRoutesExt {
48    fn with_static_routes(self) -> Self;
49}
50
51impl<S> WithStaticRoutesExt for Router<S>
52where
53    S: Clone + Send + Sync + 'static,
54{
55    fn with_static_routes(self) -> Self {
56        self.route(
57            BOOTSTRAP_CSS_ROUTE,
58            get(|| async move { get_static_css(include_str!("../assets/bootstrap.min.css")) }),
59        )
60        .route(
61            BOOTSTRAP_JS_ROUTE,
62            get(|| async move { get_static_js(include_str!("../assets/bootstrap.bundle.min.js")) }),
63        )
64        .route(
65            BOOTSTRAP_ICONS_CSS_ROUTE,
66            get(
67                || async move { get_static_css(include_str!("../assets/bootstrap-icons.min.css")) },
68            ),
69        )
70        .route(
71            BOOTSTRAP_ICONS_WOFF2_ROUTE,
72            get(|| async move {
73                get_static_font(include_bytes!("../assets/fonts/bootstrap-icons.woff2"))
74            }),
75        )
76        .route(
77            BOOTSTRAP_ICONS_WOFF_ROUTE,
78            get(|| async move {
79                get_static_woff(include_bytes!("../assets/fonts/bootstrap-icons.woff"))
80            }),
81        )
82        .route(
83            HTMX_JS_ROUTE,
84            get(|| async move { get_static_js(include_str!("../assets/htmx.org-2.0.4.min.js")) }),
85        )
86        .route(
87            HTML5_QRCODE_JS_ROUTE,
88            get(|| async move { get_static_js(include_str!("../assets/html5-qrcode.min.js")) }),
89        )
90        .route(
91            CHARTJS_ROUTE,
92            get(|| async move { get_static_js(include_str!("../assets/chart.umd.min.js")) }),
93        )
94        .route(
95            STYLE_CSS_ROUTE,
96            get(|| async move { get_static_css(include_str!("../assets/style.css")) }),
97        )
98        .route(
99            LOGO_PNG_ROUTE,
100            get(|| async move { get_static_png(include_bytes!("../assets/logo.png")) }),
101        )
102    }
103}