From 10837dac35fd69b9645ee9234f960d7fd41eedbe Mon Sep 17 00:00:00 2001 From: 409 <409dev@protonmail.com> Date: Mon, 16 Jun 2025 18:03:42 +0200 Subject: [PATCH] add `ServerConfig` with env variables --- Cargo.lock | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/config.rs | 10 ++++++ src/main.rs | 20 ++++++++++- src/server.rs | 16 +++++++-- 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 2ed0317..f2a3547 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" name = "archive" version = "0.1.0" dependencies = [ + "bon", "bytes", "byteyarn", "thiserror", @@ -54,6 +55,31 @@ version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" +[[package]] +name = "bon" +version = "3.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61138465baf186c63e8d9b6b613b508cd832cba4ce93cf37ce5f096f91ac1a6" +dependencies = [ + "bon-macros", + "rustversion", +] + +[[package]] +name = "bon-macros" +version = "3.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40d1dad34aa19bf02295382f08d9bc40651585bd497266831d40ee6296fb49ca" +dependencies = [ + "darling", + "ident_case", + "prettyplease", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "buf-trait" version = "0.4.1" @@ -90,12 +116,59 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" +[[package]] +name = "darling" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "gimli" version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "libc" version = "0.2.172" @@ -176,6 +249,16 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +[[package]] +name = "prettyplease" +version = "0.2.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6837b9e10d61f45f987d50808f83d1ee3d206c66acf650c3e4ae2e1f6ddedf55" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -209,6 +292,12 @@ version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f" +[[package]] +name = "rustversion" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" + [[package]] name = "scopeguard" version = "1.2.0" @@ -240,6 +329,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.102" diff --git a/Cargo.toml b/Cargo.toml index 89b5f12..8d2d0f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +bon = "3.6.4" bytes = "1.10.1" byteyarn = "0.5.1" thiserror = "2.0.12" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..2a49737 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,10 @@ +use bon::Builder; + +#[derive(Debug, Builder)] +pub struct ServerConfig { + #[builder(default = String::from("0.0.0.0"))] + pub host: String, + #[builder(default = 6171)] + pub port: u16, + pub max_connections: Option, +} diff --git a/src/main.rs b/src/main.rs index 4d5b102..190d9da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ +use config::ServerConfig; use errors::AppError; use server::Server; pub mod client; pub mod commands; +pub mod config; pub mod connection; pub mod database; pub mod errors; @@ -13,7 +15,23 @@ pub type Result = std::result::Result; #[tokio::main] async fn main() -> Result<()> { - let mut server = Server::new("127.0.0.1:6171").await?; + let config = ServerConfig::builder() + .maybe_host(std::env::var("SERVER_HOST").ok()) + .maybe_port( + std::env::var("SERVER_PORT") + .ok() + .map(|v| v.parse().ok()) + .flatten(), + ) + .maybe_max_connections( + std::env::var("MAX_CONNECTIONS") + .ok() + .map(|v| v.parse().ok()) + .flatten(), + ) + .build(); + + let mut server = Server::new(config).await?; server.run().await?; diff --git a/src/server.rs b/src/server.rs index 0ef30dc..1812757 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,6 +4,7 @@ use tokio::net::ToSocketAddrs; use tokio::{net::TcpListener, sync::Semaphore}; use crate::Result; +use crate::config::ServerConfig; use crate::connection::Connection; use crate::database::Database; use crate::handler::Handler; @@ -16,14 +17,23 @@ pub struct Server { } impl Server { - const MAX_CONNECTIONS: usize = 256; + pub const MAX_CONNECTIONS: usize = 256; - pub async fn new(addr: Addr) -> Result { + pub async fn new(config: ServerConfig) -> Result { + let addr = format!("{}:{}", config.host, config.port); + Self::_new( + addr, + config.max_connections.unwrap_or(Self::MAX_CONNECTIONS), + ) + .await + } + + async fn _new(addr: Addr, max_connections: usize) -> Result { let listener = TcpListener::bind(addr).await?; Ok(Self { db: Database::new(), - connection_limit: Arc::new(Semaphore::const_new(Self::MAX_CONNECTIONS)), + connection_limit: Arc::new(Semaphore::const_new(max_connections)), listener, }) }