perf: caching on voice notes and drawings
This commit is contained in:
parent
ca6b7e2803
commit
ae293c7724
1 changed files with 17 additions and 1 deletions
18
src/web.rs
18
src/web.rs
|
|
@ -2,7 +2,8 @@ use axum::{
|
||||||
extract::DefaultBodyLimit,
|
extract::DefaultBodyLimit,
|
||||||
extract::Path as AxumPath,
|
extract::Path as AxumPath,
|
||||||
extract::State,
|
extract::State,
|
||||||
http::{header, StatusCode},
|
http::{header, HeaderValue, StatusCode},
|
||||||
|
middleware::{self, Next},
|
||||||
response::{Html, IntoResponse, Response},
|
response::{Html, IntoResponse, Response},
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Form, Router,
|
Form, Router,
|
||||||
|
|
@ -44,9 +45,24 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||||
.route("/drawings/{filename}", get(serve_drawing))
|
.route("/drawings/{filename}", get(serve_drawing))
|
||||||
.route("/voice_notes/{filename}", get(serve_voice_note))
|
.route("/voice_notes/{filename}", get(serve_voice_note))
|
||||||
.layer(DefaultBodyLimit::max(2 * 1024 * 1024))
|
.layer(DefaultBodyLimit::max(2 * 1024 * 1024))
|
||||||
|
.layer(middleware::from_fn(security_headers))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn security_headers(req: axum::extract::Request, next: Next) -> Response {
|
||||||
|
let is_static = req.uri().path().starts_with("/drawings/")
|
||||||
|
|| req.uri().path().starts_with("/voice_notes/");
|
||||||
|
let mut res = next.run(req).await;
|
||||||
|
let h = res.headers_mut();
|
||||||
|
if is_static {
|
||||||
|
h.insert(header::CACHE_CONTROL, HeaderValue::from_static("public, max-age=31536000, immutable"));
|
||||||
|
} else {
|
||||||
|
h.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
|
||||||
|
}
|
||||||
|
h.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
async fn index(State(state): State<Arc<AppState>>) -> Html<String> {
|
async fn index(State(state): State<Arc<AppState>>) -> Html<String> {
|
||||||
let entries_dir = state.config.data_dir.join("entries");
|
let entries_dir = state.config.data_dir.join("entries");
|
||||||
let entries = entries::read_approved(&entries_dir);
|
let entries = entries::read_approved(&entries_dir);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue