Skip to content

Commit

Permalink
enable br compression
Browse files Browse the repository at this point in the history
  • Loading branch information
zaknesler committed May 11, 2024
1 parent 3556137 commit d87575c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion crates/blend-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ sqlx = { workspace = true, features = ["chrono"] }
thiserror = { workspace = true }
tokio = { workspace = true }
tower-cookies = "0.10"
tower-http = { version = "0.5", features = ["trace", "cors", "fs"] }
tower-http = { version = "0.5", features = [
"trace",
"cors",
"fs",
"compression-br",
] }
tracing = { workspace = true }
uuid = { workspace = true }
validator = { version = "0.18", features = ["derive"] }
16 changes: 13 additions & 3 deletions crates/blend-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use self::error::WebResult;
use axum::http::{header, HeaderValue, Method};
use tokio::net::TcpListener;
use tower_cookies::CookieManagerLayer;
use tower_http::{cors, trace::TraceLayer};
use tower_http::{
compression::{predicate::SizeAbove, CompressionLayer},
cors,
trace::TraceLayer,
CompressionLevel,
};

mod context;
mod error;
Expand Down Expand Up @@ -38,9 +43,14 @@ pub async fn serve(ctx: context::Context) -> WebResult<()> {
}

let app = crate::router::router(ctx.clone())
.layer(TraceLayer::new_for_http())
.layer(cors)
.layer(CookieManagerLayer::new());
.layer(
CompressionLayer::new()
.quality(CompressionLevel::Precise(4))
.compress_when(SizeAbove::new(512)),
)
.layer(CookieManagerLayer::new())
.layer(TraceLayer::new_for_http());

let addr = format!(
"{}:{}",
Expand Down
34 changes: 34 additions & 0 deletions crates/blend-web/src/middleware/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use axum::{
extract::Request,
http::{
header::{ACCEPT, CONTENT_TYPE},
request, HeaderValue, Method, StatusCode,
},
middleware::{self, Next},
response::Response,
Router,
};

const MAX_AGE_HOURS: u32 = 24;
const CACHE_TYPES: [&str; 4] = [
"text/css",
"text/javascript",
"application/javascript",
"image/svg+xml",
];

pub async fn middleware(request: Request, next: Next) -> Response {
let mut response = next.run(request).await;

if let Some(content_type) = response.headers().get(CONTENT_TYPE) {
if CACHE_TYPES.iter().any(|&ct| content_type == ct) {
let value = format!("public, max-age={}", MAX_AGE_HOURS * 60 * 60);

if let Ok(value) = HeaderValue::from_str(&value) {
response.headers_mut().insert("cache-control", value);
}
}
}

response
}
3 changes: 3 additions & 0 deletions crates/blend-web/src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ pub use auth::middleware as auth;

mod guest;
pub use guest::middleware as guest;

mod cache;
pub use cache::middleware as cache;
6 changes: 4 additions & 2 deletions crates/blend-web/src/router/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::Router;
use axum::{middleware, Router};
use axum_embed::ServeEmbed;
use rust_embed::RustEmbed;

Expand All @@ -13,5 +13,7 @@ pub fn router() -> Router {
Some("index.html".into()),
);

Router::new().nest_service("/", serve_assets)
Router::new()
.nest_service("/", serve_assets)
.layer(middleware::from_fn(crate::middleware::cache))
}

0 comments on commit d87575c

Please sign in to comment.