Commit 9e6b4882 authored by Robert Czechowski's avatar Robert Czechowski

Move all remaining config related code from main.rs into config.rs

parent ebf97354
......@@ -51,11 +51,14 @@ pub struct Config {
pub allow_sex_other: Option<bool>,
pub dbstatus_secret: Option<String>,
pub template_params: Option<::std::collections::BTreeMap<String, serde_json::Value>>,
pub only_contest_scan: Option<bool>,
pub reset_admin_pw: Option<bool>,
}
#[derive(StructOpt, Debug)]
#[structopt()]
pub struct Opt {
struct Opt {
/// Config file to use (default: 'config.json')
#[structopt(short = "c", long = "config", default_value = "config.json", parse(from_os_str))]
pub configfile: PathBuf,
......@@ -152,3 +155,41 @@ pub fn read_config_from_file(file: &Path) -> Config {
config
}
fn merge_value<T>(into: &mut Option<T>, from: Option<T>) { from.map(|x| *into = Some(x)); }
fn merge_flag(into: &mut Option<bool>, from: bool) {
if from {
*into = Some(true);
}
}
pub fn get_config() -> Config {
let opt = Opt::from_args();
#[cfg(feature = "debug")]
println!("Options: {:#?}", opt);
let mut config = read_config_from_file(&opt.configfile);
#[cfg(feature = "debug")]
println!("Config: {:#?}", config);
// Let options override config values
merge_value(&mut config.database_file, opt.databasefile);
merge_value(&mut config.database_url, opt.databaseurl);
merge_value(&mut config.port, opt.port);
merge_value(&mut config.template, opt.template);
merge_flag(&mut config.no_contest_scan, opt.nocontestscan);
merge_flag(&mut config.open_browser, opt.openbrowser);
merge_flag(&mut config.disable_results_page, opt.disableresultspage);
merge_flag(&mut config.enable_password_login, opt.enablepasswordlogin);
merge_flag(&mut config.only_contest_scan, opt.onlycontestscan);
merge_flag(&mut config.reset_admin_pw, opt.resetadminpw);
// Use default database file if none set
config.database_file.get_or_insert(Path::new("medal.db").to_owned());
config
}
......@@ -62,7 +62,6 @@ use helpers::SetPassword;
use webfw_iron::start_server;
use config::Config;
use structopt::StructOpt;
use std::path::Path;
......@@ -158,20 +157,20 @@ fn add_admin_user<C>(conn: &mut C, resetpw: bool)
}
}
fn prepare_and_start_server<C>(mut conn: C, config: Config, onlycontestscan: bool, resetadminpw: bool)
fn prepare_and_start_server<C>(mut conn: C, config: Config)
where C: MedalConnection + std::marker::Send + 'static,
db_objects::Contest: db_conn::MedalObject<C>
{
db_apply_migrations::test(&mut conn);
if onlycontestscan || config.no_contest_scan == Some(false) {
if config.only_contest_scan == Some(true) || config.no_contest_scan != Some(true) {
print!("Scanning for contests …");
refresh_all_contests(&mut conn);
println!(" Done")
}
if !onlycontestscan {
add_admin_user(&mut conn, resetadminpw);
if config.only_contest_scan != Some(true) {
add_admin_user(&mut conn, config.reset_admin_pw.unwrap_or(false));
#[cfg(feature = "webbrowser")]
let self_url = config.self_url.clone();
......@@ -205,28 +204,7 @@ fn open_browser_window(self_url: &str) {
}
fn main() {
let opt = config::Opt::from_args();
#[cfg(feature = "debug")]
println!("Options: {:#?}", opt);
let mut config = config::read_config_from_file(&opt.configfile);
#[cfg(feature = "debug")]
println!("Config: {:#?}", config);
// Let options override config values
opt.databasefile.map(|x| config.database_file = Some(x));
opt.databaseurl.map(|x| config.database_url = Some(x));
opt.port.map(|x| config.port = Some(x));
opt.template.map(|x| config.template = Some(x));
config.no_contest_scan = if opt.nocontestscan { Some(true) } else { config.no_contest_scan };
config.open_browser = if opt.openbrowser { Some(true) } else { config.open_browser };
config.disable_results_page = if opt.disableresultspage { Some(true) } else { config.disable_results_page };
config.enable_password_login = if opt.enablepasswordlogin { Some(true) } else { config.enable_password_login };
// Use default database file if none set
config.database_file.get_or_insert(Path::new("medal.db").to_owned());
let config = config::get_config();
#[cfg(feature = "debug")]
println!("Using config: {:#?}", config);
......@@ -245,7 +223,7 @@ fn main() {
let conn = postgres::Connection::connect(url, postgres::TlsMode::None).unwrap();
println!("Connected");
prepare_and_start_server(conn, config, opt.onlycontestscan, opt.resetadminpw);
prepare_and_start_server(conn, config);
return;
}
}
......@@ -257,7 +235,7 @@ fn main() {
let conn = rusqlite::Connection::open(path).unwrap();
println!("Connected");
prepare_and_start_server(conn, config, opt.onlycontestscan, opt.resetadminpw);
prepare_and_start_server(conn, config);
return;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment