add ServerConfig with env variables
This commit is contained in:
@@ -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: 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?;
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user