axum-with-seaorm/src/config.rs

47 lines
1.1 KiB
Rust

use std::env;
use config::File;
use serde::Deserialize;
use crate::err::AppError;
use crate::Result;
#[derive(Deserialize)]
pub struct MysqlConfig {
pub dsn: String,
pub max_cons: u32,
}
#[derive(Deserialize)]
pub struct WebConfig {
pub addr: String,
}
#[derive(Deserialize)]
pub struct AppConfig {
pub mysql: MysqlConfig,
pub web: WebConfig,
}
impl AppConfig {
pub fn new() -> Result<Self> {
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "dev".into());
// https://github.com/mehcode/config-rs/blob/master/examples/hierarchical-env/settings.rs
config::Config::builder()
// Start off by merging in the "default" configuration file
.add_source(File::with_name("config/default"))
// Add in the current environment file
// Default to 'dev' env
// Note that this file is _optional_
.add_source(
File::with_name(&format!("config/{}", run_mode)).required(false),
)
.build()
.map_err(AppError::from)?
.try_deserialize()
.map_err(AppError::from)
}
}