From a4e106380b8bc073dd15ca80eea13d9276fe932b Mon Sep 17 00:00:00 2001 From: LFC Date: Thu, 11 May 2023 15:08:20 +0800 Subject: [PATCH] fix: refreshing Dashboard returns 404 (#1562) * fix: refreshing Dashboard returns 404 * fix: refreshing Dashboard returns 404 --- src/servers/src/http.rs | 8 ++++++++ src/servers/src/http/dashboard.rs | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/servers/src/http.rs b/src/servers/src/http.rs index 73906d6dc0..dc48d28d0f 100644 --- a/src/servers/src/http.rs +++ b/src/servers/src/http.rs @@ -519,6 +519,14 @@ impl HttpServer { if !self.options.disable_dashboard { info!("Enable dashboard service at '/dashboard'"); router = router.nest("/dashboard", dashboard::dashboard()); + + // "/dashboard" and "/dashboard/" are two different paths in Axum. + // We cannot nest "/dashboard/", because we already mapping "/dashboard/*x" while nesting "/dashboard". + // So we explicitly route "/dashboard/" here. + router = router.route( + "/dashboard/", + routing::get(dashboard::static_handler).post(dashboard::static_handler), + ); } } router diff --git a/src/servers/src/http/dashboard.rs b/src/servers/src/http/dashboard.rs index 3b3572cda9..90b8a48aca 100644 --- a/src/servers/src/http/dashboard.rs +++ b/src/servers/src/http/dashboard.rs @@ -14,7 +14,8 @@ use axum::body::{boxed, Full}; use axum::http::{header, StatusCode, Uri}; -use axum::response::{IntoResponse, Response}; +use axum::response::Response; +use axum::routing; use axum::routing::Router; use common_telemetry::debug; use rust_embed::RustEmbed; @@ -27,11 +28,13 @@ use crate::error::{BuildHttpResponseSnafu, Result}; pub struct Assets; pub(crate) fn dashboard() -> Router { - Router::new().fallback(static_handler) + Router::new() + .route("/", routing::get(static_handler).post(static_handler)) + .route("/*x", routing::get(static_handler).post(static_handler)) } #[axum_macros::debug_handler] -async fn static_handler(uri: Uri) -> Result { +pub async fn static_handler(uri: Uri) -> Result { debug!("[dashboard] requesting: {}", uri.path()); let mut path = uri.path().trim_start_matches('/'); @@ -39,6 +42,18 @@ async fn static_handler(uri: Uri) -> Result { path = "index.html"; } + match get_assets(path) { + Ok(response) if response.status() == StatusCode::NOT_FOUND => index_page(), + Ok(response) => Ok(response), + Err(e) => Err(e), + } +} + +fn index_page() -> Result { + get_assets("index.html") +} + +fn get_assets(path: &str) -> Result { match Assets::get(path) { Some(content) => { let body = boxed(Full::from(content.data));