mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-10-28 08:20:49 +08:00
fix(web): 完善后端 404 响应并修复 /api 未匹配路由被静态兜底遮蔽
- 静态资源缺失时返回 404 HTML,移除"前端未构建"占位页
- /api 未匹配路由返回统一的 404 JSON
- 静态兜底由 /{*path} 改为 fallback_service,避免在 axum 0.8 下遮蔽嵌套 /api 的 fallback 并导致鉴权被绕过
This commit is contained in:
@@ -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(),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user