backend opti

This commit is contained in:
2026-05-14 18:34:07 +02:00
parent 8f4556b968
commit 2a6a4e6483
6 changed files with 301 additions and 36 deletions
+21 -4
View File
@@ -12,20 +12,27 @@ use axum::{
use std::{collections::HashMap, env, path::PathBuf, sync::Arc, time::Duration};
use tokio::sync::{Mutex, RwLock};
use tower_http::{
compression::CompressionLayer,
cors::{AllowOrigin, CorsLayer},
services::ServeDir,
};
use tracing::{error, info, warn};
use crate::handlers::contact::RATE_LIMIT_WINDOW_MS;
use crate::models::PostInfo;
use crate::models::{ImageDim, PostInfo};
pub struct CachedPost {
pub info: PostInfo,
pub body: String,
}
pub struct AppState {
pub admin_token: String,
pub data_dir: PathBuf,
pub cookie_secure: bool,
pub post_lock: Mutex<()>,
pub posts_cache: RwLock<Vec<PostInfo>>,
pub posts_cache: RwLock<Vec<CachedPost>>,
pub image_dims_cache: RwLock<HashMap<String, ImageDim>>,
pub contact_rate_limit: Mutex<HashMap<String, Vec<i64>>>,
}
@@ -67,6 +74,7 @@ async fn main() {
cookie_secure,
post_lock: Mutex::new(()),
posts_cache: RwLock::new(Vec::new()),
image_dims_cache: RwLock::new(HashMap::new()),
contact_rate_limit: Mutex::new(HashMap::new()),
});
@@ -97,6 +105,10 @@ async fn main() {
None => CorsLayer::new(),
};
// JSON routes get a tight 1 MB cap; the upload route keeps 50 MB.
const JSON_BODY_LIMIT: usize = 1024 * 1024;
const UPLOAD_BODY_LIMIT: usize = 50 * 1024 * 1024;
let app = Router::new()
.route("/api/auth/login", post(handlers::auth::login))
.route("/api/auth/logout", post(handlers::auth::logout))
@@ -118,7 +130,11 @@ async fn main() {
"/api/uploads/{filename}",
delete(handlers::upload::delete_upload),
)
.route("/api/upload", post(handlers::upload::upload_file))
.route(
"/api/upload",
post(handlers::upload::upload_file)
.layer(DefaultBodyLimit::max(UPLOAD_BODY_LIMIT)),
)
.route("/api/contact", post(handlers::contact::submit_contact))
.route("/api/messages", get(handlers::contact::list_messages))
.route(
@@ -127,7 +143,8 @@ async fn main() {
)
.route("/healthz", get(|| async { "ok" }))
.nest_service("/uploads", ServeDir::new(uploads_dir))
.layer(DefaultBodyLimit::max(50 * 1024 * 1024))
.layer(DefaultBodyLimit::max(JSON_BODY_LIMIT))
.layer(CompressionLayer::new().br(true).gzip(true))
.layer(cors)
.with_state(state);