Tonight
This commit is contained in:
parent
28323f145e
commit
94f9c0d594
76 changed files with 3238 additions and 1230 deletions
|
|
@ -19,6 +19,7 @@ use std::sync::Arc;
|
|||
use std::time::Duration;
|
||||
|
||||
use anyhow::{bail, Context};
|
||||
use axum::http::{header, HeaderValue};
|
||||
use axum::middleware;
|
||||
use axum::routing::{any, get, patch, post};
|
||||
use axum::Router;
|
||||
|
|
@ -37,6 +38,67 @@ use tracing_subscriber::EnvFilter;
|
|||
|
||||
use state::{AppState, SharedState};
|
||||
|
||||
fn is_api_path(path: &str) -> bool {
|
||||
path.starts_with("/api/")
|
||||
|| path.starts_with("/pb/")
|
||||
|| path.starts_with("/s/")
|
||||
|| matches!(path, "/health" | "/metrics")
|
||||
}
|
||||
|
||||
fn is_fingerprinted_asset(path: &str) -> bool {
|
||||
let Some(filename) = path.rsplit('/').next() else {
|
||||
return false;
|
||||
};
|
||||
let Some((stem, extension)) = filename.rsplit_once('.') else {
|
||||
return false;
|
||||
};
|
||||
if !matches!(extension, "css" | "js") {
|
||||
return false;
|
||||
}
|
||||
let Some((_, hash)) = stem.rsplit_once('.') else {
|
||||
return false;
|
||||
};
|
||||
hash.len() >= 8 && hash.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
fn is_static_asset_path(path: &str) -> bool {
|
||||
path.rsplit('/')
|
||||
.next()
|
||||
.is_some_and(|segment| segment.contains('.'))
|
||||
}
|
||||
|
||||
async fn static_cache_headers(
|
||||
request: axum::extract::Request,
|
||||
next: middleware::Next,
|
||||
) -> axum::response::Response {
|
||||
let path = request.uri().path().to_string();
|
||||
let mut response = next.run(request).await;
|
||||
|
||||
if is_api_path(&path) || response.headers().contains_key(header::CACHE_CONTROL) {
|
||||
return response;
|
||||
}
|
||||
|
||||
let cache_control = response
|
||||
.headers()
|
||||
.get(header::CONTENT_TYPE)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.filter(|content_type| content_type.contains("text/html"))
|
||||
.map(|_| HeaderValue::from_static("no-cache, must-revalidate"))
|
||||
.or_else(|| {
|
||||
is_fingerprinted_asset(&path)
|
||||
.then(|| HeaderValue::from_static("public, max-age=31536000, immutable"))
|
||||
})
|
||||
.or_else(|| {
|
||||
is_static_asset_path(&path).then(|| HeaderValue::from_static("public, max-age=3600"))
|
||||
});
|
||||
|
||||
if let Some(value) = cache_control {
|
||||
response.headers_mut().insert(header::CACHE_CONTROL, value);
|
||||
}
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn resident_memory_kib() -> Option<u64> {
|
||||
let status = std::fs::read_to_string("/proc/self/status").ok()?;
|
||||
|
|
@ -558,6 +620,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
}
|
||||
},
|
||||
))
|
||||
.layer(middleware::from_fn(static_cache_headers))
|
||||
.layer(cors)
|
||||
.layer(CompressionLayer::new().zstd(true).gzip(true))
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue