From 8c523414f007d54e0145ad9335a8932043a19f16 Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 25 Oct 2022 22:47:35 +0300 Subject: kittybox-frontend-renderer: gzip static assets --- kittybox-rs/templates/src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'kittybox-rs/templates/src/lib.rs') 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) -> Response { - + pub async fn statics( + Path(path): Path + ) -> 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() + } } } } -- cgit 1.4.1