basic logging

This commit is contained in:
2025-06-16 18:11:48 +02:00
parent 10837dac35
commit 39dd27378a
5 changed files with 223 additions and 10 deletions

View File

@@ -6,5 +6,6 @@ pub struct ServerConfig {
pub host: String,
#[builder(default = 6171)]
pub port: u16,
pub max_connections: Option<usize>,
#[builder(default = 256)]
pub max_connections: usize,
}

View File

@@ -15,6 +15,11 @@ pub type Result<T> = std::result::Result<T, AppError>;
#[tokio::main]
async fn main() -> Result<()> {
env_logger::builder()
.format_target(false)
.filter_level(log::LevelFilter::Info)
.init();
let config = ServerConfig::builder()
.maybe_host(std::env::var("SERVER_HOST").ok())
.maybe_port(
@@ -31,7 +36,13 @@ async fn main() -> Result<()> {
)
.build();
let mut server = Server::new(config).await?;
let mut server = Server::new(&config).await?;
log::info!("The server is listening on {}:{}", config.host, config.port,);
log::info!(
"The maximum amount of concurrent connections is {}",
config.max_connections
);
server.run().await?;

View File

@@ -17,15 +17,9 @@ pub struct Server {
}
impl Server {
pub const MAX_CONNECTIONS: usize = 256;
pub async fn new(config: ServerConfig) -> 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
Self::_new(addr, config.max_connections).await
}
async fn _new<Addr: ToSocketAddrs>(addr: Addr, max_connections: usize) -> Result<Self> {
@@ -46,14 +40,17 @@ impl Server {
.unwrap();
let socket = self.listener.accept().await?.0;
let addr = socket.peer_addr()?;
let connection = Connection::new(socket);
let mut handler = Handler::new(self.db.clone(), connection);
tokio::spawn(async move {
log::debug!("Spawned a new connection handler: {addr}");
if let Err(e) = handler.run().await {
println!("Handler::run error: {e:?}");
}
log::debug!("Connection handler ended: {addr}");
drop(permit);
});