From 375834ea464abb14aec627eaa3b7b42cc7e3df01 Mon Sep 17 00:00:00 2001 From: weisd Date: Wed, 22 Jan 2025 13:42:40 +0800 Subject: [PATCH] add console static server --- Cargo.lock | 20 ++++++++++++++++++++ rustfs/Cargo.toml | 1 + rustfs/src/config/mod.rs | 3 +++ rustfs/src/console.rs | 37 +++++++++++++++++++++++++++++++++++++ rustfs/src/main.rs | 7 ++++++- rustfs/static/index.html | 1 + scripts/run.sh | 8 ++++---- 7 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 rustfs/src/console.rs create mode 100644 rustfs/static/index.html diff --git a/Cargo.lock b/Cargo.lock index d74aa9384..f99605546 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1419,6 +1419,25 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "include_dir" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -2406,6 +2425,7 @@ dependencies = [ "hyper", "hyper-util", "iam", + "include_dir", "jsonwebtoken", "lazy_static", "lock", diff --git a/rustfs/Cargo.toml b/rustfs/Cargo.toml index 1127fced3..3b0de2948 100644 --- a/rustfs/Cargo.toml +++ b/rustfs/Cargo.toml @@ -69,6 +69,7 @@ crypto = { path = "../crypto" } iam = { path = "../iam" } jsonwebtoken = "9.3.0" tower-http = { version = "0.6.2", features = ["cors"] } +include_dir = "0.7.4" [build-dependencies] prost-build.workspace = true diff --git a/rustfs/src/config/mod.rs b/rustfs/src/config/mod.rs index c87957cdb..f14f303eb 100644 --- a/rustfs/src/config/mod.rs +++ b/rustfs/src/config/mod.rs @@ -53,4 +53,7 @@ pub struct Opt { /// Domain name used for virtual-hosted-style requests. #[arg(long)] pub domain_name: Option, + + #[arg(long, default_value_t = format!("0.0.0.0:{}", 0))] + pub console_address: String, } diff --git a/rustfs/src/console.rs b/rustfs/src/console.rs new file mode 100644 index 000000000..07aa81199 --- /dev/null +++ b/rustfs/src/console.rs @@ -0,0 +1,37 @@ +use axum::{ + body::Body, + http::{Response, StatusCode}, + response::IntoResponse, + routing::get, + Router, +}; + +use include_dir::{include_dir, Dir}; + +static STATIC_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/static"); + +async fn static_handler(uri: axum::http::Uri) -> impl IntoResponse { + let path = uri.path().trim_start_matches('/'); + if let Some(file) = STATIC_DIR.get_file(path) { + Response::builder() + .status(StatusCode::OK) + .body(Body::from(file.contents())) + .unwrap() + } else { + Response::builder() + .status(StatusCode::NOT_FOUND) + .body(Body::from("404 Not Found")) + .unwrap() + } +} + +pub async fn start_static_file_server(addrs: &str) { + // 创建路由 + let app = Router::new().route("/*file", get(static_handler)); + + let listener = tokio::net::TcpListener::bind(addrs).await.unwrap(); + + println!("console listening on: {}", listener.local_addr().unwrap()); + + axum::serve(listener, app).await.unwrap(); +} diff --git a/rustfs/src/main.rs b/rustfs/src/main.rs index f9f09b22e..e57efa0b5 100644 --- a/rustfs/src/main.rs +++ b/rustfs/src/main.rs @@ -1,6 +1,7 @@ mod admin; mod auth; mod config; +mod console; mod grpc; mod service; mod storage; @@ -176,7 +177,7 @@ async fn run(opt: config::Opt) -> Result<()> { let http_server = ConnBuilder::new(TokioExecutor::new()); let mut ctrl_c = std::pin::pin!(tokio::signal::ctrl_c()); let graceful = hyper_util::server::graceful::GracefulShutdown::new(); - info!("server is running at http://{local_addr}"); + println!("server is running at http://{local_addr}"); loop { let (socket, _) = tokio::select! { @@ -239,6 +240,10 @@ async fn run(opt: config::Opt) -> Result<()> { info!("server was started"); + tokio::spawn(async move { + console::start_static_file_server(&opt.console_address).await; + }); + tokio::select! { _ = tokio::signal::ctrl_c() => { diff --git a/rustfs/static/index.html b/rustfs/static/index.html new file mode 100644 index 000000000..612c5bbb3 --- /dev/null +++ b/rustfs/static/index.html @@ -0,0 +1 @@ +static index \ No newline at end of file diff --git a/scripts/run.sh b/scripts/run.sh index 27ca7caa3..ad55a8512 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -10,13 +10,13 @@ mkdir -p ./target/volume/test mkdir -p ./target/volume/test{0..4} -if [ -z "$RUST_LOG" ]; then - export RUST_LOG="rustfs=debug,ecstore=debug,s3s=debug,iam=debug" -fi +# if [ -z "$RUST_LOG" ]; then +# export RUST_LOG="rustfs=debug,ecstore=debug,s3s=debug,iam=debug" +# fi # export RUSTFS_ERASURE_SET_DRIVE_COUNT=5 -export RUSTFS_STORAGE_CLASS_INLINE_BLOCK="512 KB" +# export RUSTFS_STORAGE_CLASS_INLINE_BLOCK="512 KB" # DATA_DIR_ARG="./target/volume/test{0...4}"