add console static server

This commit is contained in:
weisd
2025-01-22 13:42:40 +08:00
parent 5c339f0757
commit 375834ea46
7 changed files with 72 additions and 5 deletions
Generated
+20
View File
@@ -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",
+1
View File
@@ -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
+3
View File
@@ -53,4 +53,7 @@ pub struct Opt {
/// Domain name used for virtual-hosted-style requests.
#[arg(long)]
pub domain_name: Option<String>,
#[arg(long, default_value_t = format!("0.0.0.0:{}", 0))]
pub console_address: String,
}
+37
View File
@@ -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();
}
+6 -1
View File
@@ -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() => {
+1
View File
@@ -0,0 +1 @@
static index
+4 -4
View File
@@ -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}"