diff options
Diffstat (limited to 'kittybox-rs/templates/src/lib.rs')
-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() + } } } } |