From effccfdbbc7b889b7eb4dc4be67c19749498a53f Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Thu, 1 Jun 2023 23:01:11 -0400 Subject: [PATCH] Add config extension module Better way to parse environment variables and pass around config vars. --- src/config.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 20 +++++++++++++------- 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..a5586be --- /dev/null +++ b/src/config.rs @@ -0,0 +1,25 @@ +use anyhow::{Context, Result}; + +#[derive(Clone, Debug)] +pub struct Config { + pub database_url: String, + pub database_max_connections: u32, + pub host: String, + pub port: u16, +} + +impl Config { + pub fn new() -> Result { + let database_url = std::env::var("DATABASE_URL").context("DATABASE_URL not set")?; + let database_max_connections = std::env::var("DATABASE_MAX_CONNECTIONS").context("DATABASE_MAX_CONNECTIONS not set")?.parse()?; + let host = std::env::var("HOST").context("HOST not set")?; + let port = std::env::var("PORT").context("PORT not set")?.parse()?; + + Ok(Config { + database_url, + database_max_connections, + host, + port, + }) + } +} diff --git a/src/lib.rs b/src/lib.rs index 484f251..6bb3fcd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod config; pub mod error; pub mod handlers; pub mod jobs; diff --git a/src/main.rs b/src/main.rs index 532913a..9b57efa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,37 @@ +use std::path::Path; + +use anyhow::Result; use axum::{ routing::{get, post}, - Router, + Router, Extension, }; use dotenvy::dotenv; use notify::Watcher; use sqlx::postgres::PgPoolOptions; -use std::{env, path::Path}; use tower::ServiceBuilder; use tower_livereload::LiveReloadLayer; use tower_http::trace::TraceLayer; use tracing::debug; +use lib::config; use lib::handlers; #[tokio::main] -async fn main() -> anyhow::Result<()> { +async fn main() -> Result<()> { dotenv().ok(); + let config = config::Config::new()?; + tracing_subscriber::fmt::init(); let pool = PgPoolOptions::new() - .max_connections(env::var("DATABASE_MAX_CONNECTIONS")?.parse()?) - .connect(&env::var("DATABASE_URL")?) + .max_connections(config.database_max_connections) + .connect(&config.database_url) .await?; sqlx::migrate!().run(&pool).await?; + let addr = format!("{}:{}", &config.host, &config.port).parse()?; let mut app = Router::new() .route("/api/v1/feeds", get(handlers::api::feeds::get)) .route("/api/v1/feed", post(handlers::api::feed::post)) @@ -36,7 +42,8 @@ async fn main() -> anyhow::Result<()> { .route("/", get(handlers::home::get)) .route("/feeds", get(handlers::feeds::get)) .with_state(pool) - .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http())); + .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http())) + .layer(Extension(config)); #[cfg(debug_assertions)] { @@ -47,7 +54,6 @@ async fn main() -> anyhow::Result<()> { app = app.layer(livereload); } - let addr = (env::var("HOST")? + ":" + &env::var("PORT")?).parse()?; debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service())