add ServerConfig with env variables

This commit is contained in:
2025-06-16 18:03:42 +02:00
parent 0c619fbc94
commit 10837dac35
5 changed files with 138 additions and 4 deletions

95
Cargo.lock generated
View File

@@ -21,6 +21,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
name = "archive" name = "archive"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bon",
"bytes", "bytes",
"byteyarn", "byteyarn",
"thiserror", "thiserror",
@@ -54,6 +55,31 @@ version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" 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]] [[package]]
name = "buf-trait" name = "buf-trait"
version = "0.4.1" version = "0.4.1"
@@ -90,12 +116,59 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" 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]] [[package]]
name = "gimli" name = "gimli"
version = "0.31.1" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.172" version = "0.2.172"
@@ -176,6 +249,16 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" 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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.95" version = "1.0.95"
@@ -209,6 +292,12 @@ version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f" checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f"
[[package]]
name = "rustversion"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
@@ -240,6 +329,12 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "syn" name = "syn"
version = "2.0.102" version = "2.0.102"

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
bon = "3.6.4"
bytes = "1.10.1" bytes = "1.10.1"
byteyarn = "0.5.1" byteyarn = "0.5.1"
thiserror = "2.0.12" thiserror = "2.0.12"

10
src/config.rs Normal file
View File

@@ -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<usize>,
}

View File

@@ -1,8 +1,10 @@
use config::ServerConfig;
use errors::AppError; use errors::AppError;
use server::Server; use server::Server;
pub mod client; pub mod client;
pub mod commands; pub mod commands;
pub mod config;
pub mod connection; pub mod connection;
pub mod database; pub mod database;
pub mod errors; pub mod errors;
@@ -13,7 +15,23 @@ pub type Result<T> = std::result::Result<T, AppError>;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { 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?; server.run().await?;

View File

@@ -4,6 +4,7 @@ use tokio::net::ToSocketAddrs;
use tokio::{net::TcpListener, sync::Semaphore}; use tokio::{net::TcpListener, sync::Semaphore};
use crate::Result; use crate::Result;
use crate::config::ServerConfig;
use crate::connection::Connection; use crate::connection::Connection;
use crate::database::Database; use crate::database::Database;
use crate::handler::Handler; use crate::handler::Handler;
@@ -16,14 +17,23 @@ pub struct Server {
} }
impl Server { impl Server {
const MAX_CONNECTIONS: usize = 256; pub const MAX_CONNECTIONS: usize = 256;
pub async fn new<Addr: ToSocketAddrs>(addr: Addr) -> Result<Self> { pub async fn new(config: ServerConfig) -> Result<Self> {
let addr = format!("{}:{}", config.host, config.port);
Self::_new(
addr,
config.max_connections.unwrap_or(Self::MAX_CONNECTIONS),
)
.await
}
async fn _new<Addr: ToSocketAddrs>(addr: Addr, max_connections: usize) -> Result<Self> {
let listener = TcpListener::bind(addr).await?; let listener = TcpListener::bind(addr).await?;
Ok(Self { Ok(Self {
db: Database::new(), db: Database::new(),
connection_limit: Arc::new(Semaphore::const_new(Self::MAX_CONNECTIONS)), connection_limit: Arc::new(Semaphore::const_new(max_connections)),
listener, listener,
}) })
} }