Set opts as most important in config

This commit is contained in:
eraden 2022-05-01 17:27:56 +02:00
parent 79b67a93a3
commit ad2b08f22b
3 changed files with 20 additions and 20 deletions

View File

@ -4,6 +4,8 @@ use parking_lot::Mutex;
use password_hash::SaltString; use password_hash::SaltString;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::UpdateConfig;
trait Example: Sized { trait Example: Sized {
fn example() -> Self; fn example() -> Self;
} }
@ -321,9 +323,17 @@ impl Default for AppConfig {
} }
} }
pub fn load(config_path: &str) -> SharedAppConfig { pub fn default_load(opts: &impl UpdateConfig) -> SharedAppConfig {
load("./bazzar.toml", opts)
}
fn load(config_path: &str, opts: &impl UpdateConfig) -> SharedAppConfig {
match std::fs::read_to_string(config_path) { match std::fs::read_to_string(config_path) {
Ok(c) => SharedAppConfig::new(toml::from_str(&c).unwrap()), Ok(c) => {
let mut c = toml::from_str(&c).unwrap();
opts.update_config(&mut c);
SharedAppConfig::new(c)
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => { Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
let config = AppConfig::example(); let config = AppConfig::example();
std::fs::write(config_path, toml::to_string_pretty(&config).unwrap()).unwrap(); std::fs::write(config_path, toml::to_string_pretty(&config).unwrap()).unwrap();

View File

@ -53,10 +53,7 @@ async fn server(opts: ServerOpts) -> Result<()> {
}; };
let redis_connection_string = "127.0.0.1:6379"; let redis_connection_string = "127.0.0.1:6379";
let app_config = config::load("./bazzar.toml"); let app_config = config::default_load(&opts);
{
opts.update_config(&mut *app_config.lock());
}
let db = database::Database::build(app_config.clone()).await?.start(); let db = database::Database::build(app_config.clone()).await?.start();
let token_manager = token_manager::TokenManager::new(app_config.clone(), db.clone()).start(); let token_manager = token_manager::TokenManager::new(app_config.clone(), db.clone()).start();
@ -98,8 +95,7 @@ async fn server(opts: ServerOpts) -> Result<()> {
async fn migrate(opts: MigrateOpts) -> Result<()> { async fn migrate(opts: MigrateOpts) -> Result<()> {
use sqlx::migrate::MigrateError; use sqlx::migrate::MigrateError;
let config = config::load("./bazzar.toml"); let config = config::default_load(&opts);
opts.update_config(&mut *config.lock());
let db = database::Database::build(config).await?; let db = database::Database::build(config).await?;
let res: std::result::Result<(), MigrateError> = let res: std::result::Result<(), MigrateError> =
sqlx::migrate!("../db/migrate").run(db.pool()).await; sqlx::migrate!("../db/migrate").run(db.pool()).await;
@ -130,8 +126,7 @@ async fn create_account(opts: CreateAccountOpts) -> Result<()> {
if !validate_length(&opts.login, Some(4), Some(100), None) { if !validate_length(&opts.login, Some(4), Some(100), None) {
panic!("Login must have at least 4 characters and no more than 100"); panic!("Login must have at least 4 characters and no more than 100");
} }
let config = config::load("./bazzar.toml"); let config = config::default_load(&opts);
opts.update_config(&mut *config.lock());
let db = database::Database::build(config.clone()).await?.start(); let db = database::Database::build(config.clone()).await?.start();
let pass = match opts.pass_file { let pass = match opts.pass_file {
Some(path) => std::fs::read_to_string(path).map_err(Error::PassFile)?, Some(path) => std::fs::read_to_string(path).map_err(Error::PassFile)?,
@ -172,7 +167,7 @@ async fn create_account(opts: CreateAccountOpts) -> Result<()> {
} }
async fn test_mailer(opts: TestMailerOpts) -> Result<()> { async fn test_mailer(opts: TestMailerOpts) -> Result<()> {
let config = config::load("./bazzar.toml"); let config = config::default_load(&opts);
opts.update_config(&mut *config.lock()); opts.update_config(&mut *config.lock());
let manager = email_manager::EmailManager::build(config) let manager = email_manager::EmailManager::build(config)

View File

@ -109,13 +109,9 @@ impl UpdateConfig for ServerOpts {
fn update_config(&self, config: &mut AppConfig) { fn update_config(&self, config: &mut AppConfig) {
{ {
let web = config.web_mut(); let web = config.web_mut();
if web.bind().is_none() {
web.set_bind(&self.bind); web.set_bind(&self.bind);
}
if web.port().is_none() {
web.set_port(self.port); web.set_port(self.port);
} }
}
if let Some(url) = self.db_url.as_ref() { if let Some(url) = self.db_url.as_ref() {
config.database_mut().set_url(url); config.database_mut().set_url(url);
} }
@ -168,9 +164,8 @@ pub struct CreateAccountOpts {
impl UpdateConfig for CreateAccountOpts { impl UpdateConfig for CreateAccountOpts {
fn update_config(&self, config: &mut AppConfig) { fn update_config(&self, config: &mut AppConfig) {
match &self.cmd { if let Some(opts) = &self.cmd {
None => {} opts.update_config(config)
Some(opts) => opts.update_config(config),
}; };
} }
} }