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