From d5ec8a28e7b1c0a525917605d67e0f69845f8c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Wed, 22 Jul 2026 14:25:18 +0200 Subject: [PATCH] fix in error codes --- docker-compose.yml | 1 - src/app.rs | 21 +++++++++++++++++++-- src/config.rs | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4ff8ebf..c7ef348 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,6 @@ services: STATIC_DIR: ${STATIC_DIR:-/app/static} FILES_DIR: ${FILES_DIR:-/data/files} UPLOAD_MAX_SIZE_MB: ${UPLOAD_MAX_SIZE_MB:-20} - ASSET_VERSION: ${ASSET_VERSION:-dev} REGISTRATION_ENABLED: ${REGISTRATION_ENABLED:-false} RUST_LOG: ${RUST_LOG:-rustpad=info,tower_http=warn} FRONTEND_LOG_LEVEL: ${FRONTEND_LOG_LEVEL:-warn} diff --git a/src/app.rs b/src/app.rs index 2f9311e..d60cb50 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,12 +5,29 @@ use axum::{ routing::{get, post}, Router, }; -use tower::ServiceBuilder; +use tower::{service_fn, ServiceBuilder}; use tower_http::{services::ServeDir, set_header::SetResponseHeaderLayer, trace::TraceLayer}; use crate::{api, auth, db, state::SharedState, websocket}; +use std::convert::Infallible; pub fn router(state: SharedState, static_dir: &str, upload_max_size_bytes: usize) -> Router { + let asset_version = state.asset_version.clone(); + let asset_not_found = service_fn(move |_request| { + let asset_version = asset_version.clone(); + async move { + Ok::<_, Infallible>(error_response( + StatusCode::NOT_FOUND, + "404", + "File not found", + "The requested application asset does not exist.", + "/", + "Home page", + &asset_version, + )) + } + }); + Router::new() .route("/", get(home)) .route("/p/{slug}", get(pad)) @@ -78,7 +95,7 @@ pub fn router(state: SharedState, static_dir: &str, upload_max_size_bytes: usize header::CACHE_CONTROL, HeaderValue::from_static("private, must-revalidate"), )) - .service(ServeDir::new(static_dir)), + .service(ServeDir::new(static_dir).not_found_service(asset_not_found)), ) .fallback(not_found) .method_not_allowed_fallback(method_not_allowed) diff --git a/src/config.rs b/src/config.rs index d2e116a..e072bcb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -54,7 +54,7 @@ impl Config { upload_max_size_bytes: upload_max_size_mb .checked_mul(1024 * 1024) .ok_or("UPLOAD_MAX_SIZE_MB is too large")?, - asset_version: env_var("ASSET_VERSION", env!("CARGO_PKG_VERSION")), + asset_version: env!("CARGO_PKG_VERSION").to_owned(), smtp, registration_enabled: env_bool("REGISTRATION_ENABLED", false)?, frontend_log_level: env_log_level("FRONTEND_LOG_LEVEL", "warn")?,