diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-04-02 17:23:05 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-04-02 17:32:38 -0500 |
commit | d3a09372b5b945a609cce5e28c4d4233e3b134e8 (patch) | |
tree | 9f888aa393948e1c90d76d37349ffe9738b95f24 /src/main.rs | |
parent | b263c6637ce8b7c83e4d01d1ef2e90e195a155fb (diff) |
feat: impl toml file and env config layering
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 50 |
1 files changed, 16 insertions, 34 deletions
diff --git a/src/main.rs b/src/main.rs index a926916..353c7fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ -use std::sync::Arc; - -use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; -use tokio::net::TcpListener; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; -use unnamed_server::{state::AppState, Error}; + +use crate::config::Config; + +mod config; #[tokio::main] #[tracing::instrument] -async fn main() -> Result<(), Error> { +async fn main() -> Result<(), main_error::MainError> { + let _ = dotenvy::dotenv(); + tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() @@ -16,36 +17,17 @@ async fn main() -> Result<(), Error> { .with(tracing_subscriber::fmt::layer()) .init(); - // TODO: Migrate all of these into a struct parsed from env, cli, and file. - let _ = dotenvy::dotenv(); - let listen_addr = std::env::var("ADDRESS").unwrap_or("127.0.0.1:30000".to_string()); - let jwt_max_age: time::Duration = time::Duration::HOUR; - // serde_json::from_str(&std::env::var("JWT_MAX_AGE").unwrap_or_else(|_| "1h".to_string()))?; - let jwt_secret = std::env::var("JWT_SECRET").expect("JWT_SECRET is not set"); - let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL is not set"); - - let pool = init_db(&database_url).await?; - let state = Arc::new(AppState { - pool, - jwt_secret, - jwt_max_age, - }); - let app = unnamed_server::init_router(state); - - let listener = TcpListener::bind(listen_addr).await?; + let config_file = std::env::args() + .nth(1) + .unwrap_or("/etc/unnamed_app.toml".to_string()); - tracing::info!("Server listening on http://{}", listener.local_addr()?); - - axum::serve(listener, app).await.map_err(From::from) -} - -async fn init_db(uri: &str) -> Result<Pool<Postgres>, sqlx::Error> { - let pool = PgPoolOptions::new() - .max_connections(10) - .connect(uri) + let (listener, router) = Config::new() + .file(config_file)? + .env("UNNAMED_")? + .build() .await?; - sqlx::migrate!().run(&pool).await?; + tracing::info!("Server listening on http://{}", listener.local_addr()?); - Ok(pool) + axum::serve(listener, router).await.map_err(From::from) } |