fix(web): 完善后端 404 响应并修复 /api 未匹配路由被静态兜底遮蔽

- 静态资源缺失时返回 404 HTML,移除"前端未构建"占位页
- /api 未匹配路由返回统一的 404 JSON
- 静态兜底由 /{*path} 改为 fallback_service,避免在 axum 0.8 下遮蔽嵌套 /api 的 fallback 并导致鉴权被绕过
This commit is contained in:
mofeng-git
2026-09-22 21:42:19 +08:00
parent 3d3110c3d7
commit 4cf3f4e252
2 changed files with 34 additions and 44 deletions

View File

@@ -4,9 +4,11 @@ use axum::{
routing::{delete, put},
};
use axum::{
http::StatusCode,
middleware,
response::IntoResponse,
routing::{any, get, patch, post},
Router,
Json, Router,
};
use std::sync::Arc;
use tower_http::{
@@ -363,6 +365,7 @@ pub fn create_router(state: Arc<AppState>) -> Router {
.merge(protected_routes)
.merge(stream_routes)
.merge(upload_routes)
.fallback(api_not_found)
.layer(middleware::from_fn_with_state(
state.clone(),
auth_middleware,
@@ -384,3 +387,14 @@ pub fn create_router(state: Arc<AppState>) -> Router {
None => main_router,
}
}
async fn api_not_found() -> impl IntoResponse {
(
StatusCode::NOT_FOUND,
Json(super::ErrorResponse {
success: false,
code: None,
message: "Not Found".to_string(),
}),
)
}