添加config默认值

This commit is contained in:
weisd
2024-08-09 21:27:10 +08:00
parent 5411691fb2
commit 71ff5aa253
5 changed files with 49 additions and 14 deletions

1
Cargo.lock generated
View File

@@ -1156,6 +1156,7 @@ dependencies = [
"http",
"hyper-util",
"mime",
"netif",
"s3s",
"time",
"tokio",

View File

@@ -35,4 +35,5 @@ hyper-util = { version = "0.1.5", features = [
] }
mime = "0.3.17"
transform-stream = "0.3.0"
netif = "0.1.6"
# pin-utils = "0.1.0"

View File

@@ -4,6 +4,8 @@ use clap::Parser;
///
/// Used if no port is specified.
pub const DEFAULT_PORT: u16 = 9000;
pub const DEFAULT_ACCESS_KEY: &str = "rustfsadmin";
pub const DEFAULT_SECRET_KEY: &str = "rustfsadmin";
#[derive(Debug, Parser)]
pub struct Opt {

View File

@@ -8,7 +8,7 @@ use hyper_util::{
server::conn::auto::Builder as ConnBuilder,
};
use s3s::{auth::SimpleAuth, service::S3ServiceBuilder};
use std::io::IsTerminal;
use std::{io::IsTerminal, net::SocketAddr, str::FromStr};
use tokio::net::TcpListener;
use tracing::{debug, info};
use tracing_error::ErrorLayer;
@@ -41,28 +41,57 @@ fn main() -> Result<()> {
#[tokio::main]
async fn run(opt: config::Opt) -> Result<()> {
debug!("opt: {:?}", &opt);
let listener = TcpListener::bind(opt.address.clone()).await?;
let local_addr: SocketAddr = listener.local_addr()?;
let mut domain_name = {
netif::up()?
.map(|x| x.address().to_owned())
// .filter(|v| v.is_ipv4() && !v.is_loopback() && !v.is_unspecified())
.map(|v| format!("{}", v))
.next()
.and_then(|ip| {
if let SocketAddr::V4(ipv4) = local_addr {
Some(format!("{}:{}", ip, ipv4.port()))
} else {
None
}
})
};
// Setup S3 service
let service = {
let mut b = S3ServiceBuilder::new(storage::ecfs::FS::new(opt.address.clone(), opt.volumes.clone()).await?);
let mut access_key = String::from_str(config::DEFAULT_ACCESS_KEY).unwrap();
let mut secret_key = String::from_str(config::DEFAULT_SECRET_KEY).unwrap();
// 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");
access_key = ak;
secret_key = sk;
}
info!("authentication is enabled {}, {}", &access_key, &secret_key);
b.set_auth(SimpleAuth::from_single(access_key, secret_key));
// 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");
if let Some(dm) = opt.domain_name {
domain_name = Some(dm)
}
if domain_name.is_some() {
info!(
"virtual-hosted-style requests are enabled use domain_name {}",
domain_name.as_ref().unwrap()
);
b.set_base_domain(domain_name.unwrap());
}
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());

View File

@@ -14,9 +14,11 @@ if [ -z "$RUST_LOG" ]; then
export RUST_LOG="rustfs=debug,ecstore=debug,s3s=debug"
fi
cargo run \
-- --access-key AKEXAMPLERUSTFS \
--secret-key SKEXAMPLERUSTFS \
--address 0.0.0.0:9010 \
--domain-name 127.0.0.1:9010 \
cargo run "$DATA_DIR"
# -- --access-key AKEXAMPLERUSTFS \
# --secret-key SKEXAMPLERUSTFS \
# --address 0.0.0.0:9010 \
# --domain-name 127.0.0.1:9010 \
"$DATA_DIR"
# cargo run "$DATA_DIR"