From b3288627d171fff9a289a56a4ae27307985f9f96 Mon Sep 17 00:00:00 2001
From: Vika <vika@fireburn.ru>
Date: Sun, 20 Apr 2025 09:07:49 +0300
Subject: kittybox-frontend-renderer: factor out the `assets` module into a
 file

Change-Id: I6138cfe8479ba8df9a1580049675c1dd84abdad1
---
 templates/src/assets.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 templates/src/assets.rs

(limited to 'templates/src/assets.rs')

diff --git a/templates/src/assets.rs b/templates/src/assets.rs
new file mode 100644
index 0000000..493c14d
--- /dev/null
+++ b/templates/src/assets.rs
@@ -0,0 +1,47 @@
+use axum::extract::Path;
+use axum::http::header::{CACHE_CONTROL, CONTENT_ENCODING, CONTENT_TYPE, X_CONTENT_TYPE_OPTIONS};
+use axum::http::StatusCode;
+use axum::response::{IntoResponse, Response};
+
+const ASSETS: include_dir::Dir<'static> = include_dir::include_dir!("$OUT_DIR/");
+const CACHE_FOR_A_DAY: &str = "max-age=86400";
+const GZIP: &str = "gzip";
+
+pub async fn statics(Path(path): Path<String>) -> Response {
+    let content_type: &'static str = if path.ends_with(".js") {
+        "application/javascript"
+    } else if path.ends_with(".css") {
+        "text/css"
+    } else if path.ends_with(".html") {
+        "text/html; charset=\"utf-8\""
+    } else {
+        "application/octet-stream"
+    };
+
+    match ASSETS.get_file(path.clone() + ".gz") {
+        Some(file) => (
+            StatusCode::OK,
+            [
+                (CONTENT_TYPE, content_type),
+                (CONTENT_ENCODING, GZIP),
+                (CACHE_CONTROL, CACHE_FOR_A_DAY),
+                (X_CONTENT_TYPE_OPTIONS, "nosniff"),
+            ],
+            file.contents(),
+        )
+            .into_response(),
+        None => match ASSETS.get_file(path) {
+            Some(file) => (
+                StatusCode::OK,
+                [
+                    (CONTENT_TYPE, content_type),
+                    (CACHE_CONTROL, CACHE_FOR_A_DAY),
+                    (X_CONTENT_TYPE_OPTIONS, "nosniff"),
+                ],
+                file.contents(),
+            )
+                .into_response(),
+            None => StatusCode::NOT_FOUND.into_response(),
+        },
+    }
+}
-- 
cgit 1.4.1