diff options
author | Vika <vika@fireburn.ru> | 2022-10-25 22:47:35 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2022-11-06 17:38:03 +0300 |
commit | 8c523414f007d54e0145ad9335a8932043a19f16 (patch) | |
tree | fe5cc306820740b330d1e1150549d077e52bbe61 /kittybox-rs/templates/src | |
parent | 4b917f257a41e6bdfd32e91d7a38bdc975c8f678 (diff) | |
download | kittybox-8c523414f007d54e0145ad9335a8932043a19f16.tar.zst |
kittybox-frontend-renderer: gzip static assets
Diffstat (limited to 'kittybox-rs/templates/src')
-rw-r--r-- | kittybox-rs/templates/src/lib.rs | 21 |
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() + } } } } |