server app builder implemented
This commit is contained in:
@ -4,20 +4,20 @@ use sqlx::{postgres::{PgConnectOptions, PgSslMode}, ConnectOptions};
|
||||
|
||||
use crate::domain::SubscriberEmail;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[derive(serde::Deserialize,Clone)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
pub application: ApplicationSettings,
|
||||
pub email_client: EmailClientSettings,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[derive(serde::Deserialize,Clone)]
|
||||
pub struct ApplicationSettings {
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[derive(serde::Deserialize,Clone)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: Secret<String>,
|
||||
@ -48,7 +48,7 @@ impl DatabaseSettings {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[derive(serde::Deserialize,Clone)]
|
||||
pub struct EmailClientSettings {
|
||||
pub base_url: String,
|
||||
pub sender_email: String,
|
||||
|
||||
24
src/main.rs
24
src/main.rs
@ -1,8 +1,5 @@
|
||||
use std::net::TcpListener;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use zero2prod::configuration::get_configuration;
|
||||
use zero2prod::email_client;
|
||||
use zero2prod::startup::run;
|
||||
use zero2prod::startup::Application;
|
||||
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
||||
|
||||
#[tokio::main]
|
||||
@ -11,20 +8,7 @@ async fn main() -> std::io::Result<()> {
|
||||
init_subscriber(subscriber);
|
||||
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
|
||||
let connection_pool = PgPoolOptions::new().acquire_timeout(std::time::Duration::from_secs(2))
|
||||
.max_connections(10).connect_lazy_with(config.database.with_db());
|
||||
|
||||
let sender_email = config.email_client.sender().unwrap();
|
||||
let timeout = config.email_client.timeout();
|
||||
let email_client = email_client::EmailClient::new(
|
||||
config.email_client.base_url,
|
||||
sender_email,
|
||||
config.email_client.authorization_token,
|
||||
timeout,
|
||||
);
|
||||
|
||||
let address = format!("{}:{}", config.application.host, config.application.port);
|
||||
let listener = TcpListener::bind(address).expect("Failed to bind random port");
|
||||
run(listener, connection_pool ,email_client)?.await
|
||||
let app = Application::build(config).await?;
|
||||
app.run_until_stopped().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -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())
|
||||
}
|
||||
Reference in New Issue
Block a user