Skip to main content

solverforge_ui/
lib.rs

1use axum::{
2    extract::Path,
3    http::{header, StatusCode},
4    response::{IntoResponse, Response},
5    routing::get,
6    Router,
7};
8use include_dir::{include_dir, Dir};
9
10static ASSETS: Dir = include_dir!("$CARGO_MANIFEST_DIR/static/sf");
11
12pub fn routes() -> Router {
13    Router::new().route("/sf/{*path}", get(serve_asset))
14}
15
16async fn serve_asset(Path(path): Path<String>) -> Response {
17    let Some(file) = ASSETS.get_file(&path) else {
18        return StatusCode::NOT_FOUND.into_response();
19    };
20
21    let mime = mime_from_path(&path);
22    let cache = if is_immutable(&path) {
23        "public, max-age=31536000, immutable"
24    } else {
25        "public, max-age=3600"
26    };
27
28    (
29        StatusCode::OK,
30        [(header::CONTENT_TYPE, mime), (header::CACHE_CONTROL, cache)],
31        file.contents(),
32    )
33        .into_response()
34}
35
36fn mime_from_path(path: &str) -> &'static str {
37    match path.rsplit('.').next() {
38        Some("css") => "text/css; charset=utf-8",
39        Some("js") => "application/javascript; charset=utf-8",
40        Some("svg") => "image/svg+xml",
41        Some("woff2") => "font/woff2",
42        Some("woff") => "font/woff",
43        Some("ttf") => "font/ttf",
44        Some("eot") => "application/vnd.ms-fontobject",
45        Some("png") => "image/png",
46        Some("jpg" | "jpeg") => "image/jpeg",
47        Some("ico") => "image/x-icon",
48        Some("json") => "application/json",
49        Some("html") => "text/html; charset=utf-8",
50        Some("map") => "application/json",
51        _ => "application/octet-stream",
52    }
53}
54
55fn is_immutable(path: &str) -> bool {
56    path.starts_with("fonts/")
57        || path.starts_with("vendor/")
58        || path.starts_with("img/")
59        || is_versioned_bundle(path)
60}
61
62fn is_versioned_bundle(path: &str) -> bool {
63    path.strip_prefix("sf.")
64        .and_then(|rest| rest.rsplit_once('.'))
65        .map(|(version, ext)| {
66            !version.is_empty()
67                && version.chars().all(|ch| {
68                    ch.is_ascii_digit()
69                        || ch == '.'
70                        || ch == '-'
71                        || ch == '+'
72                        || ch.is_ascii_alphabetic()
73                })
74                && matches!(ext, "css" | "js")
75        })
76        .unwrap_or(false)
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use axum::{
83        body::{to_bytes, Body},
84        http::{Method, Request, StatusCode},
85    };
86    use tower::util::ServiceExt;
87
88    #[test]
89    fn versioned_bundles_are_detected() {
90        assert!(is_versioned_bundle("sf.0.2.0.css"));
91        assert!(is_versioned_bundle("sf.0.2.0.js"));
92        assert!(is_versioned_bundle("sf.0.2.0-beta.1.js"));
93        assert!(is_versioned_bundle("sf.0.2.0+build.7.css"));
94        assert!(!is_versioned_bundle("sf.css"));
95        assert!(!is_versioned_bundle("sf.js"));
96        assert!(!is_versioned_bundle("vendor/sf.0.2.0.js"));
97    }
98
99    #[test]
100    fn caches_paths_are_predicted_correctly() {
101        assert_eq!(mime_from_path("styles/sf.css"), "text/css; charset=utf-8");
102        assert_eq!(
103            mime_from_path("scripts/sf.js"),
104            "application/javascript; charset=utf-8"
105        );
106        assert_eq!(mime_from_path("img/logo.svg"), "image/svg+xml");
107        assert_eq!(mime_from_path("font.woff2"), "font/woff2");
108
109        assert!(is_immutable("fonts/jetbrains-mono.woff2"));
110        assert!(is_immutable("vendor/leaflet/leaflet.js"));
111        assert!(is_immutable("img/solverforge-logo.svg"));
112        assert!(is_immutable("sf.0.2.0.css"));
113        assert!(is_immutable("sf.0.2.0+build.7.js"));
114        assert!(!is_immutable("sf.css"));
115    }
116
117    #[test]
118    fn mime_detection_still_works_for_versioned_assets() {
119        assert_eq!(mime_from_path("sf.0.2.0.css"), "text/css; charset=utf-8");
120        assert_eq!(
121            mime_from_path("sf.0.2.0+build.7.js"),
122            "application/javascript; charset=utf-8"
123        );
124    }
125
126    #[tokio::test]
127    async fn serves_assets_with_expected_headers() {
128        let app = routes();
129
130        let immutable_resp = app
131            .clone()
132            .oneshot(
133                Request::builder()
134                    .method(Method::GET)
135                    .uri("/sf/fonts/jetbrains-mono.woff2")
136                    .body(Body::empty())
137                    .unwrap(),
138            )
139            .await
140            .unwrap();
141
142        assert_eq!(immutable_resp.status(), StatusCode::OK);
143        assert_eq!(
144            immutable_resp.headers().get("cache-control").unwrap(),
145            "public, max-age=31536000, immutable"
146        );
147        assert_eq!(
148            immutable_resp.headers().get("content-type").unwrap(),
149            "font/woff2"
150        );
151        assert!(!to_bytes(immutable_resp.into_body(), 16_000_000)
152            .await
153            .unwrap()
154            .is_empty());
155
156        let mutable_resp = app
157            .clone()
158            .oneshot(
159                Request::builder()
160                    .method(Method::GET)
161                    .uri("/sf/sf.css")
162                    .body(Body::empty())
163                    .unwrap(),
164            )
165            .await
166            .unwrap();
167
168        assert_eq!(mutable_resp.status(), StatusCode::OK);
169        assert_eq!(
170            mutable_resp.headers().get("cache-control").unwrap(),
171            "public, max-age=3600"
172        );
173        assert_eq!(
174            mutable_resp.headers().get("content-type").unwrap(),
175            "text/css; charset=utf-8"
176        );
177        assert!(!to_bytes(mutable_resp.into_body(), 16_000_000)
178            .await
179            .unwrap()
180            .is_empty());
181
182        let missing_resp = app
183            .oneshot(
184                Request::builder()
185                    .method(Method::GET)
186                    .uri("/sf/does-not-exist")
187                    .body(Body::empty())
188                    .unwrap(),
189            )
190            .await
191            .unwrap();
192
193        assert_eq!(missing_resp.status(), StatusCode::NOT_FOUND);
194    }
195}