2023-07-07 19:22:30 +00:00
|
|
|
use axum::headers::{AccessControlMaxAge, HeaderMapExt};
|
|
|
|
use axum::http::header::CONTENT_SECURITY_POLICY;
|
|
|
|
use axum::http::{Request, StatusCode};
|
2023-07-22 21:36:46 +00:00
|
|
|
use axum::middleware::{from_fn, Next};
|
2023-07-07 19:22:30 +00:00
|
|
|
use axum::Router;
|
|
|
|
use hyper::Response;
|
|
|
|
use std::time::Duration;
|
|
|
|
use tower::util::MapResponseLayer;
|
|
|
|
use tower_http::services::{ServeDir, ServeFile};
|
|
|
|
|
|
|
|
pub fn static_serve_sw(file: &str) -> Router {
|
|
|
|
Router::new()
|
|
|
|
.nest_service("", ServeFile::new(file))
|
|
|
|
.layer(MapResponseLayer::new(|mut res: Response<_>| {
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(AccessControlMaxAge::from(Duration::from_secs(60 * 10)));
|
|
|
|
|
|
|
|
res
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn static_serve(dir: &str) -> Router {
|
|
|
|
let serve_dir = ServeDir::new(dir);
|
|
|
|
Router::new()
|
|
|
|
.nest_service("", serve_dir)
|
|
|
|
.layer(MapResponseLayer::new(|mut res: Response<_>| {
|
|
|
|
res.headers_mut()
|
|
|
|
.typed_insert(AccessControlMaxAge::from(Duration::from_secs(
|
|
|
|
60 * 60 * 24 * 7,
|
|
|
|
)));
|
|
|
|
|
|
|
|
res
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn static_serve_svg(dir: &str) -> Router {
|
|
|
|
let serve_dir = ServeDir::new(dir);
|
|
|
|
Router::new()
|
|
|
|
.nest_service("", serve_dir)
|
|
|
|
.layer(from_fn(|req: Request<_>, next: Next<_>| async {
|
|
|
|
if !req.uri().path().ends_with(".svg") {
|
|
|
|
return Err(StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(next.run(req).await)
|
|
|
|
}))
|
|
|
|
.layer(MapResponseLayer::new(|mut res: Response<_>| {
|
|
|
|
let headers = res.headers_mut();
|
|
|
|
headers.typed_insert(AccessControlMaxAge::from(Duration::from_secs(
|
|
|
|
60 * 60 * 24 * 7,
|
|
|
|
)));
|
|
|
|
headers.insert(
|
|
|
|
CONTENT_SECURITY_POLICY,
|
|
|
|
"default-src 'none'; style-src 'unsafe-inline'"
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
res
|
|
|
|
}))
|
|
|
|
}
|