Add config extension module
Better way to parse environment variables and pass around config vars.
This commit is contained in:
parent
a67ffbbbed
commit
effccfdbbc
25
src/config.rs
Normal file
25
src/config.rs
Normal 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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
pub mod config;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod handlers;
|
pub mod handlers;
|
||||||
pub mod jobs;
|
pub mod jobs;
|
||||||
|
20
src/main.rs
20
src/main.rs
@ -1,31 +1,37 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use axum::{
|
use axum::{
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router,
|
Router, Extension,
|
||||||
};
|
};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use notify::Watcher;
|
use notify::Watcher;
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use std::{env, path::Path};
|
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_livereload::LiveReloadLayer;
|
use tower_livereload::LiveReloadLayer;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
use lib::config;
|
||||||
use lib::handlers;
|
use lib::handlers;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> Result<()> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
|
let config = config::Config::new()?;
|
||||||
|
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.max_connections(env::var("DATABASE_MAX_CONNECTIONS")?.parse()?)
|
.max_connections(config.database_max_connections)
|
||||||
.connect(&env::var("DATABASE_URL")?)
|
.connect(&config.database_url)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
sqlx::migrate!().run(&pool).await?;
|
sqlx::migrate!().run(&pool).await?;
|
||||||
|
|
||||||
|
let addr = format!("{}:{}", &config.host, &config.port).parse()?;
|
||||||
let mut app = Router::new()
|
let mut app = Router::new()
|
||||||
.route("/api/v1/feeds", get(handlers::api::feeds::get))
|
.route("/api/v1/feeds", get(handlers::api::feeds::get))
|
||||||
.route("/api/v1/feed", post(handlers::api::feed::post))
|
.route("/api/v1/feed", post(handlers::api::feed::post))
|
||||||
@ -36,7 +42,8 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.route("/", get(handlers::home::get))
|
.route("/", get(handlers::home::get))
|
||||||
.route("/feeds", get(handlers::feeds::get))
|
.route("/feeds", get(handlers::feeds::get))
|
||||||
.with_state(pool)
|
.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)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
@ -47,7 +54,6 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
app = app.layer(livereload);
|
app = app.layer(livereload);
|
||||||
}
|
}
|
||||||
|
|
||||||
let addr = (env::var("HOST")? + ":" + &env::var("PORT")?).parse()?;
|
|
||||||
debug!("listening on {}", addr);
|
debug!("listening on {}", addr);
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
|
Loading…
Reference in New Issue
Block a user