From 8978730e04a52e3b2593966fc2f1d7194ca84dc8 Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Wed, 13 Mar 2024 16:31:47 +0800 Subject: [PATCH] make it compatible Signed-off-by: Bugen Zhao --- ci/scripts/release.sh | 2 + src/meta/src/dashboard/mod.rs | 10 +++- src/meta/src/dashboard/proxy.rs | 99 --------------------------------- 3 files changed, 10 insertions(+), 101 deletions(-) delete mode 100644 src/meta/src/dashboard/proxy.rs diff --git a/ci/scripts/release.sh b/ci/scripts/release.sh index e35459b71495b..35837e32fa9bf 100755 --- a/ci/scripts/release.sh +++ b/ci/scripts/release.sh @@ -39,6 +39,8 @@ source "$HOME/.cargo/env" rustup show source ci/scripts/common.sh unset RUSTC_WRAPPER # disable sccache +# TODO: uncomment this line to use embedded dashboard in release build +# unset RISINGWAVE_CI # disable ci mode, this is critical for building and embedding the dashboard assets echo "--- Install protoc3" PROTOC_ARCH=${ARCH} diff --git a/src/meta/src/dashboard/mod.rs b/src/meta/src/dashboard/mod.rs index ea33cdf750ab1..697f90e76703e 100644 --- a/src/meta/src/dashboard/mod.rs +++ b/src/meta/src/dashboard/mod.rs @@ -13,7 +13,6 @@ // limitations under the License. mod prometheus; -mod proxy; use std::collections::HashMap; use std::net::SocketAddr; @@ -416,7 +415,14 @@ impl DashboardService { .layer(cors_layer); let trace_ui_router = otlp_embedded::ui_app(srv.trace_state.clone(), "/trace/"); - let dashboard_router = risingwave_meta_dashboard::router(); + let dashboard_router = if let Some(ui_path) = ui_path { + // TODO(bugen): remove `ui_path` + get_service(ServeDir::new(ui_path)) + .handle_error(|e| async move { match e {} }) + .boxed_clone() + } else { + risingwave_meta_dashboard::router().boxed_clone() + }; let app = Router::new() .fallback_service(dashboard_router) diff --git a/src/meta/src/dashboard/proxy.rs b/src/meta/src/dashboard/proxy.rs deleted file mode 100644 index 1010a3bb274fa..0000000000000 --- a/src/meta/src/dashboard/proxy.rs +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2024 RisingWave Labs -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::collections::HashMap; -use std::sync::Arc; - -use anyhow::anyhow; -use axum::body::Body; -use axum::http::StatusCode; -use axum::response::{IntoResponse, Response}; -use bytes::Bytes; -use hyper::header::CONTENT_TYPE; -use hyper::{HeaderMap, Request}; -use parking_lot::Mutex; -use url::Url; - -#[derive(Clone)] -pub struct CachedResponse { - code: StatusCode, - body: Bytes, - headers: HeaderMap, - uri: Url, -} - -impl IntoResponse for CachedResponse { - fn into_response(self) -> Response { - let guess = mime_guess::from_path(self.uri.path()); - let mut headers = HeaderMap::new(); - if let Some(x) = self.headers.get(hyper::header::ETAG) { - headers.insert(hyper::header::ETAG, x.clone()); - } - if let Some(x) = self.headers.get(hyper::header::CACHE_CONTROL) { - headers.insert(hyper::header::CACHE_CONTROL, x.clone()); - } - if let Some(x) = self.headers.get(hyper::header::EXPIRES) { - headers.insert(hyper::header::EXPIRES, x.clone()); - } - if let Some(x) = guess.first() { - if x.type_() == "image" && x.subtype() == "svg" { - headers.insert(CONTENT_TYPE, "image/svg+xml".parse().unwrap()); - } else { - headers.insert( - CONTENT_TYPE, - format!("{}/{}", x.type_(), x.subtype()).parse().unwrap(), - ); - } - } - (self.code, headers, self.body).into_response() - } -} - -pub async fn proxy( - req: Request, - cache: Arc>>, -) -> anyhow::Result { - let mut path = req.uri().path().to_string(); - if path.ends_with('/') { - path += "index.html"; - } - - if let Some(resp) = cache.lock().get(&path) { - return Ok(resp.clone().into_response()); - } - - let url_str = format!( - "https://raw.githubusercontent.com/risingwavelabs/risingwave/dashboard-artifact{}", - path - ); - let url = Url::parse(&url_str)?; - if url.to_string() != url_str { - return Err(anyhow!("normalized URL isn't the same as the original one")); - } - - tracing::info!("dashboard service: proxying {}", url); - - let content = reqwest::get(url.clone()).await?; - - let resp = CachedResponse { - code: content.status(), - headers: content.headers().clone(), - body: content.bytes().await?, - uri: url, - }; - - cache.lock().insert(path, resp.clone()); - - Ok(resp.into_response()) -}