Replace config with envious

This commit is contained in:
traxys 2023-07-25 22:37:42 +02:00
parent 4ecc06e01b
commit f556fec3bb
3 changed files with 39 additions and 160 deletions

View file

@ -2,7 +2,6 @@ use std::{net::SocketAddr, path::PathBuf, sync::Arc};
use axum::Router;
use base64::{engine::general_purpose, Engine};
use config::{Config, ConfigError};
use jwt_simple::prelude::HS256Key;
use migration::{Migrator, MigratorTrait};
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
@ -66,29 +65,37 @@ impl<'de> Deserialize<'de> for Base64 {
}
}
fn default_host() -> String {
"0.0.0.0".into()
}
fn default_port() -> u16 {
8085
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
struct Settings {
jwt_secret: Base64,
#[serde(default = "default_host")]
host: String,
#[serde(default = "default_port")]
port: u16,
#[serde(default)]
api_allowed: Option<String>,
#[serde(default)]
serve_app: Option<PathBuf>,
database_url: String,
#[serde(default)]
sqlx_logging: bool,
}
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let cfg = Config::builder()
.add_source(config::Environment::with_prefix("REGALADE"))
.set_default("host", "0.0.0.0")?
.set_default("port", "8085")?
.set_default("api_allowed", None::<String>)?
.set_default("serve_app", None::<String>)?
.set_default("sqlx_logging", false)?
.build()?;
cfg.try_deserialize()
pub fn new() -> Result<Self, envious::EnvDeserializationError> {
envious::Config::default()
.with_prefix("REGALADE_")
.case_sensitive(true)
.build_from_env()
}
}