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 HTMX_JS_ROUTE: &str = "/assets/htmx.org-2.0.4.min.js";
10pub const HTML5_QRCODE_JS_ROUTE: &str = "/assets/html5-qrcode.min.js";
11pub const STYLE_CSS_ROUTE: &str = "/assets/style.css";
12pub const LOGO_PNG_ROUTE: &str = "/assets/logo.png";
13
14pub(crate) fn get_static_asset(content_type: &'static str, body: &'static [u8]) -> Response {
15    (
16        [(CONTENT_TYPE, content_type)],
17        [(CACHE_CONTROL, format!("public, max-age={}", 60 * 60))],
18        body,
19    )
20        .into_response()
21}
22
23pub(crate) fn get_static_css(body: &'static str) -> Response {
24    get_static_asset("text/css", body.as_bytes())
25}
26
27pub(crate) fn get_static_png(body: &'static [u8]) -> Response {
28    get_static_asset("image/png", body)
29}
30
31pub(crate) fn get_static_js(body: &'static str) -> Response {
32    get_static_asset("application/javascript", body.as_bytes())
33}
34
35pub trait WithStaticRoutesExt {
36    fn with_static_routes(self) -> Self;
37}
38
39impl<S> WithStaticRoutesExt for Router<S>
40where
41    S: Clone + Send + Sync + 'static,
42{
43    fn with_static_routes(self) -> Self {
44        self.route(
45            BOOTSTRAP_CSS_ROUTE,
46            get(|| async move { get_static_css(include_str!("../assets/bootstrap.min.css")) }),
47        )
48        .route(
49            BOOTSTRAP_JS_ROUTE,
50            get(|| async move { get_static_js(include_str!("../assets/bootstrap.bundle.min.js")) }),
51        )
52        .route(
53            HTMX_JS_ROUTE,
54            get(|| async move { get_static_js(include_str!("../assets/htmx.org-2.0.4.min.js")) }),
55        )
56        .route(
57            HTML5_QRCODE_JS_ROUTE,
58            get(|| async move { get_static_js(include_str!("../assets/html5-qrcode.min.js")) }),
59        )
60        .route(
61            STYLE_CSS_ROUTE,
62            get(|| async move { get_static_css(include_str!("../assets/style.css")) }),
63        )
64        .route(
65            LOGO_PNG_ROUTE,
66            get(|| async move { get_static_png(include_bytes!("../assets/logo.png")) }),
67        )
68    }
69}