mirror of
https://github.com/rustfs/rustfs.git
synced 2026-01-17 01:30:33 +00:00
fix: 新增服务启动流程
This commit is contained in:
1103
Cargo.lock
generated
1103
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -18,3 +18,4 @@ http = "1.1.0"
|
||||
thiserror = "1.0.61"
|
||||
time = "0.3.36"
|
||||
async-trait = "0.1.80"
|
||||
tokio = "1.38.0"
|
||||
|
||||
@@ -10,5 +10,21 @@ rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
store = { path = "../store" }
|
||||
tracing-subscriber = { version = "0.3.18" }
|
||||
clap = { version = "4.5.7", features = ["derive"] }
|
||||
s3s = { version = "0.10.0" }
|
||||
anyhow = { version = "1.0.86" }
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "time"] }
|
||||
time = { workspace = true, features = ["parsing", "formatting"] }
|
||||
async-trait = { workspace = true }
|
||||
tokio = { workspace = true, features = [
|
||||
"rt-multi-thread",
|
||||
"macros",
|
||||
"net",
|
||||
"signal",
|
||||
] }
|
||||
hyper-util = { version = "0.1.5", features = [
|
||||
"tokio",
|
||||
"server-auto",
|
||||
"server-graceful",
|
||||
] }
|
||||
|
||||
30
rustfs/src/config/mod.rs
Normal file
30
rustfs/src/config/mod.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use clap::Parser;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Default port that a rustfs server listens on.
|
||||
///
|
||||
/// Used if no port is specified.
|
||||
pub const DEFAULT_PORT: u16 = 9000;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct Opt {
|
||||
/// DIR points to a directory on a filesystem.
|
||||
#[arg(required = true)]
|
||||
pub volumes: Vec<PathBuf>,
|
||||
|
||||
/// bind to a specific ADDRESS:PORT, ADDRESS can be an IP or hostname
|
||||
#[arg(long, default_value_t = format!(":{}", DEFAULT_PORT))]
|
||||
pub address: String,
|
||||
|
||||
/// Access key used for authentication.
|
||||
#[arg(long)]
|
||||
pub access_key: Option<String>,
|
||||
|
||||
/// Secret key used for authentication.
|
||||
#[arg(long)]
|
||||
pub secret_key: Option<String>,
|
||||
|
||||
/// Domain name used for virtual-hosted-style requests.
|
||||
#[arg(long)]
|
||||
pub domain_name: Option<String>,
|
||||
}
|
||||
@@ -1,3 +1,103 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod config;
|
||||
mod storage;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use hyper_util::{
|
||||
rt::{TokioExecutor, TokioIo},
|
||||
server::conn::auto::Builder as ConnBuilder,
|
||||
};
|
||||
use s3s::{auth::SimpleAuth, service::S3ServiceBuilder};
|
||||
use std::io::IsTerminal;
|
||||
use tokio::net::TcpListener;
|
||||
use tracing::info;
|
||||
|
||||
fn setup_tracing() {
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
let env_filter = EnvFilter::from_default_env();
|
||||
let enable_color = std::io::stdout().is_terminal();
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.pretty()
|
||||
.with_env_filter(env_filter)
|
||||
.with_ansi(enable_color)
|
||||
.init();
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let opt = config::Opt::parse();
|
||||
|
||||
setup_tracing();
|
||||
|
||||
run(opt)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn run(opt: config::Opt) -> Result<()> {
|
||||
// Setup S3 service
|
||||
let service = {
|
||||
let mut b = S3ServiceBuilder::new(storage::simple_fs::SimpleFS {});
|
||||
|
||||
// Enable authentication
|
||||
if let (Some(ak), Some(sk)) = (opt.access_key, opt.secret_key) {
|
||||
b.set_auth(SimpleAuth::from_single(ak, sk));
|
||||
info!("authentication is enabled");
|
||||
}
|
||||
|
||||
// Enable parsing virtual-hosted-style requests
|
||||
if let Some(domain_name) = opt.domain_name {
|
||||
b.set_base_domain(domain_name);
|
||||
info!("virtual-hosted-style requests are enabled");
|
||||
}
|
||||
|
||||
b.build()
|
||||
};
|
||||
|
||||
let listener = TcpListener::bind(opt.address).await?;
|
||||
let local_addr = listener.local_addr()?;
|
||||
|
||||
let hyper_service = service.into_shared();
|
||||
|
||||
let http_server = ConnBuilder::new(TokioExecutor::new());
|
||||
let graceful = hyper_util::server::graceful::GracefulShutdown::new();
|
||||
|
||||
let mut ctrl_c = std::pin::pin!(tokio::signal::ctrl_c());
|
||||
|
||||
info!("server is running at http://{local_addr}");
|
||||
|
||||
loop {
|
||||
let (socket, _) = tokio::select! {
|
||||
res = listener.accept() => {
|
||||
match res {
|
||||
Ok(conn) => conn,
|
||||
Err(err) => {
|
||||
tracing::error!("error accepting connection: {err}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = ctrl_c.as_mut() => {
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
let conn = http_server.serve_connection(TokioIo::new(socket), hyper_service.clone());
|
||||
let conn = graceful.watch(conn.into_owned());
|
||||
tokio::spawn(async move {
|
||||
let _ = conn.await;
|
||||
});
|
||||
}
|
||||
|
||||
tokio::select! {
|
||||
() = graceful.shutdown() => {
|
||||
tracing::debug!("Gracefully shutdown!");
|
||||
},
|
||||
() = tokio::time::sleep(std::time::Duration::from_secs(10)) => {
|
||||
tracing::debug!("Waited 10 seconds for graceful shutdown, aborting...");
|
||||
}
|
||||
}
|
||||
|
||||
info!("server is stopped");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
1
rustfs/src/storage/mod.rs
Normal file
1
rustfs/src/storage/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod simple_fs;
|
||||
6
rustfs/src/storage/simple_fs.rs
Normal file
6
rustfs/src/storage/simple_fs.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use s3s::S3;
|
||||
|
||||
pub struct SimpleFS {}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl S3 for SimpleFS {}
|
||||
Reference in New Issue
Block a user