mail-accounts/src/main.rs

70 lines
1.5 KiB
Rust
Raw Normal View History

2023-08-14 18:26:38 +02:00
use std::{net::SocketAddr, sync::Arc};
2023-08-14 17:59:37 +02:00
use axum::Router;
use serde::Deserialize;
2023-08-14 18:26:38 +02:00
use sqlx::{postgres::PgPoolOptions, PgPool};
2023-08-14 17:59:37 +02:00
use tracing_subscriber::EnvFilter;
fn default_port() -> u16 {
8080
}
fn default_address() -> String {
"127.0.0.1".into()
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
struct Settings {
#[serde(default = "default_port")]
port: u16,
#[serde(default = "default_address")]
address: String,
2023-08-14 18:26:38 +02:00
database_url: String,
2023-08-14 17:59:37 +02:00
}
impl Settings {
fn new() -> color_eyre::Result<Self> {
envious::Config::default()
.with_prefix("MAIL_ADMIN_")
.case_sensitive(true)
.build_from_env()
.map_err(Into::into)
}
}
2023-08-14 18:26:38 +02:00
struct AppState {
db: PgPool,
}
2023-08-14 17:59:37 +02:00
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.with_target(true)
.with_env_filter(EnvFilter::from_default_env())
.init();
let config = Settings::new()?;
tracing::info!("Settings: {config:#?}");
let addr: SocketAddr = format!("{}:{}", config.address, config.port).parse()?;
2023-08-14 18:26:38 +02:00
let db = PgPoolOptions::new()
.max_connections(5)
.connect(&config.database_url)
.await?;
sqlx::migrate!().run(&db).await?;
2023-08-14 17:59:37 +02:00
tracing::info!("Listening on {addr}");
2023-08-14 18:26:38 +02:00
let router = Router::new().with_state(Arc::new(AppState { db }));
2023-08-14 17:59:37 +02:00
Ok(axum::Server::bind(&addr)
.serve(router.into_make_service())
.await?)
}