server app builder implemented

This commit is contained in:
Andre Heber
2024-02-26 14:43:57 +01:00
parent 518acf03b1
commit 2f4895928f
4 changed files with 61 additions and 57 deletions

View File

@ -27,3 +27,44 @@ pub fn run(
Ok(server)
}
pub struct Application {
pub port: u16,
pub server: Server,
}
impl Application {
pub async fn build(
config: crate::configuration::Settings,
) -> Result<Self, std::io::Error> {
let connection_pool: Pool<Postgres> = get_connection_pool(config.database);
let sender_email = config.email_client.sender().unwrap();
let timeout = config.email_client.timeout();
let email_client = EmailClient::new(
config.email_client.base_url,
sender_email,
config.email_client.authorization_token,
timeout,
);
let port = config.application.port;
let listener = TcpListener::bind(format!("{}:{}", config.application.host, port))?;
let server = run(listener, connection_pool, email_client)?;
Ok(Self { port, server })
}
pub fn port(&self) -> u16 {
self.port
}
pub async fn run_until_stopped(self) -> Result<(), std::io::Error> {
self.server.await
}
}
pub fn get_connection_pool(config: crate::configuration::DatabaseSettings) -> Pool<Postgres> {
Pool::connect_lazy_with(config.with_db())
}