Add config extension module

Better way to parse environment variables and pass around config vars.
This commit is contained in:
Tyler Hallada 2023-06-01 23:01:11 -04:00
parent a67ffbbbed
commit effccfdbbc
3 changed files with 39 additions and 7 deletions

25
src/config.rs Normal file
View File

@ -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<Config> {
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,
})
}
}

View File

@ -1,3 +1,4 @@
pub mod config;
pub mod error;
pub mod handlers;
pub mod jobs;

View File

@ -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())