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