From fbd3fcbab6c5b7ee7374a12f5f3a0969b11e6788 Mon Sep 17 00:00:00 2001 From: Kieran Moy Date: Mon, 22 Jul 2024 22:29:53 +0800 Subject: [PATCH] update default config --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 3 ++- config.yaml | 4 ++-- src/config.rs | 2 +- src/main.rs | 19 +++++++++++++------ 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64dd8f4..fa40a27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -726,7 +726,7 @@ dependencies = [ [[package]] name = "tsr" -version = "0.1.7" +version = "0.1.8" dependencies = [ "async-mutex", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 4825516..42b2ed2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tsr" -version = "0.1.7" +version = "0.1.8" edition = "2021" build = "build.rs" diff --git a/README.md b/README.md index d13fea3..df48731 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ Usage: tsr [OPTIONS] Options: - -c, --config [default: ] + -c, --config set config file path + -p, --port set the listening port -h, --help Print help -V, --version Print version ``` diff --git a/config.yaml b/config.yaml index 28d1776..42bf596 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,6 @@ bind: - addr: 127.0.0.1 - listen: 8080 + addr: 0.0.0.0 + listen: 80 server: info: "Powered by Rust" diff --git a/src/config.rs b/src/config.rs index 1e0031e..90f0c34 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,7 +35,7 @@ fn init_config() -> Config { let default_config = Config { bind: BindConfig { addr: "0.0.0.0".to_owned(), - listen: 8080, + listen: 80, }, server: ServerConfig { info: "Powered by Rust".to_owned(), diff --git a/src/main.rs b/src/main.rs index ac41701..106e3ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -210,8 +210,11 @@ async fn handle_connection(mut stream: TcpStream) -> io::Result<(i32, String)> { #[derive(Parser)] #[command(version, about, long_about = None)] struct Args { - #[arg(short, long, default_value = "")] - config: String, + #[arg(short, long, default_value = None, help = "set config file path")] + config: Option, + + #[arg(short, long, default_value = None, help = "set the listening port")] + port: Option, } #[tokio::main] @@ -225,10 +228,14 @@ async fn main() -> Result<(), Box> { } let arg = Args::parse(); - *CONFIG_PATH.lock()? = arg.config; - - let listener = - TcpListener::bind(format!("{}:{}", CONFIG.bind.addr, CONFIG.bind.listen)).await?; + *CONFIG_PATH.lock()? = arg.config.unwrap_or(String::new()); + + let listener = TcpListener::bind(format!( + "{}:{}", + CONFIG.bind.addr, + arg.port.unwrap_or(CONFIG.bind.listen) + )) + .await?; loop { let (mut stream, addr) = listener.accept().await?;