feat: add basic configuration support

This commit is contained in:
Alexander Navarro 2025-02-26 11:53:07 -03:00
parent 8e1e366964
commit 3ff1f26cc4
6 changed files with 253 additions and 20 deletions

View file

@ -2,7 +2,8 @@
#![allow(dead_code)]
use axum_htmx::AutoVaryLayer;
use compendium::{router, AppState, Error, Link, Result, Tx};
use compendium::config::Config;
use compendium::{config, router, AppState, Error, Link, Result, Tx};
use minijinja::{Environment, Value};
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
@ -41,25 +42,10 @@ fn load_templates() -> Result<Environment<'static>> {
Ok(tmpl_env)
}
async fn init_db() -> Result<PgPool> {
let db_string = format!(
"postgres://{}:{}@{}/{}",
env::var("CPD_DB_USER")?,
env::var("CPD_DB_PASSWORD")?,
env::var("CPD_DB_HOST")?,
env::var("CPD_DB_NAME")?
);
info!("Connecting to database {}", db_string);
Ok(PgPoolOptions::new()
.max_connections(5)
.connect(&db_string)
.await?)
}
#[tokio::main]
async fn main() -> Result<()> {
let config = Config::new("./config.toml".into())?;
// Logs
tracing_subscriber::registry()
.with(
@ -72,7 +58,12 @@ async fn main() -> Result<()> {
let mut tmpl_env = load_templates()?;
let pool = init_db().await?;
info!("Connecting to database {}", config.db);
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&config.db.generate_db_string(false))
.await?;
let (tx_state, tx_layer) = Tx::setup(pool);
@ -86,7 +77,7 @@ async fn main() -> Result<()> {
#[cfg(debug_assertions)]
let app = app.layer(tower_livereload::LiveReloadLayer::new());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
let listener = tokio::net::TcpListener::bind(config.addr).await?;
info!("listening on {}", listener.local_addr()?);
axum::serve(listener, app).await?;