about summary refs log tree commit diff
path: root/kittybox-rs/templates/src/lib.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-10-25 22:47:35 +0300
committerVika <vika@fireburn.ru>2022-11-06 17:38:03 +0300
commit8c523414f007d54e0145ad9335a8932043a19f16 (patch)
treefe5cc306820740b330d1e1150549d077e52bbe61 /kittybox-rs/templates/src/lib.rs
parent4b917f257a41e6bdfd32e91d7a38bdc975c8f678 (diff)
kittybox-frontend-renderer: gzip static assets
Diffstat (limited to 'kittybox-rs/templates/src/lib.rs')
-rw-r--r--kittybox-rs/templates/src/lib.rs21
1 files changed, 15 insertions, 6 deletions
diff --git a/kittybox-rs/templates/src/lib.rs b/kittybox-rs/templates/src/lib.rs
index 5b3a8df..8d5d5fa 100644
--- a/kittybox-rs/templates/src/lib.rs
+++ b/kittybox-rs/templates/src/lib.rs
@@ -13,13 +13,15 @@ pub mod assets {
     use axum::response::{IntoResponse, Response};
     use axum::extract::Path;
     use axum::http::StatusCode;
-    use axum::http::header::{CONTENT_TYPE, CACHE_CONTROL};
+    use axum::http::header::{CONTENT_TYPE, CONTENT_ENCODING, CACHE_CONTROL};
 
-    const ASSETS: include_dir::Dir<'static> = include_dir::include_dir!("$OUT_DIR");
+    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 {
-
+    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") {
@@ -30,12 +32,19 @@ pub mod assets {
             "application/octet-stream"
         };
 
-        match ASSETS.get_file(path) {
+        match ASSETS.get_file(path.clone() + ".gz") {
             Some(file) => (StatusCode::OK,
                            [(CONTENT_TYPE, content_type),
+                            (CONTENT_ENCODING, GZIP),
                             (CACHE_CONTROL, CACHE_FOR_A_DAY)],
                            file.contents()).into_response(),
-            None => StatusCode::NOT_FOUND.into_response()
+            None => match ASSETS.get_file(path) {
+                Some(file) => (StatusCode::OK,
+                               [(CONTENT_TYPE, content_type),
+                                (CACHE_CONTROL, CACHE_FOR_A_DAY)],
+                               file.contents()).into_response(),
+                None => StatusCode::NOT_FOUND.into_response()
+            }
         }
     }
 }