2021-03-02 19:30:43 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Serialized, Toml},
|
|
|
|
Figment,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-02 19:13:58 +00:00
|
|
|
|
2021-03-02 19:51:33 +00:00
|
|
|
use crate::infra::cli::CLIOpts;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2021-03-02 19:30:43 +00:00
|
|
|
pub struct Configuration {
|
2021-03-02 22:07:01 +00:00
|
|
|
pub ldap_port: u16,
|
|
|
|
pub ldaps_port: u16,
|
2021-03-07 11:36:12 +00:00
|
|
|
pub http_port: u16,
|
2021-03-02 19:51:33 +00:00
|
|
|
pub secret_pepper: String,
|
2021-03-12 08:33:43 +00:00
|
|
|
pub ldap_user_dn: String,
|
|
|
|
pub ldap_user_pass: String,
|
|
|
|
pub database_url: String,
|
2021-03-02 22:07:01 +00:00
|
|
|
pub verbose: bool,
|
2021-03-02 19:30:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:26 +00:00
|
|
|
impl Default for Configuration {
|
|
|
|
fn default() -> Self {
|
2021-03-02 19:30:43 +00:00
|
|
|
Configuration {
|
2021-03-02 22:07:01 +00:00
|
|
|
ldap_port: 3890,
|
|
|
|
ldaps_port: 6360,
|
2021-03-07 11:36:12 +00:00
|
|
|
http_port: 17170,
|
2021-03-02 19:30:43 +00:00
|
|
|
secret_pepper: String::from("secretsecretpepper"),
|
2021-03-12 08:33:43 +00:00
|
|
|
ldap_user_dn: String::new(),
|
|
|
|
ldap_user_pass: String::new(),
|
|
|
|
database_url: String::from("sqlite://users.db?mode=rwc"),
|
2021-03-02 22:07:01 +00:00
|
|
|
verbose: false,
|
2021-03-02 19:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:26 +00:00
|
|
|
impl Configuration {
|
2021-03-02 22:07:01 +00:00
|
|
|
fn merge_with_cli(mut self: Configuration, cli_opts: CLIOpts) -> Configuration {
|
2021-03-02 21:03:58 +00:00
|
|
|
if cli_opts.verbose {
|
2021-03-02 22:07:01 +00:00
|
|
|
self.verbose = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(port) = cli_opts.ldap_port {
|
|
|
|
self.ldap_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(port) = cli_opts.ldaps_port {
|
|
|
|
self.ldaps_port = port;
|
2021-03-02 21:03:58 +00:00
|
|
|
}
|
2021-03-02 20:43:26 +00:00
|
|
|
|
2021-03-02 22:07:01 +00:00
|
|
|
self
|
2021-03-02 20:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:51:33 +00:00
|
|
|
pub fn init(cli_opts: CLIOpts) -> Result<Configuration> {
|
2021-03-02 20:43:26 +00:00
|
|
|
let config_file = cli_opts.config_file.clone();
|
|
|
|
|
|
|
|
let config: Configuration = Figment::from(Serialized::defaults(Configuration::default()))
|
|
|
|
.merge(Toml::file(config_file))
|
|
|
|
.merge(Env::prefixed("LLDAP_"))
|
|
|
|
.extract()?;
|
2021-03-02 19:30:43 +00:00
|
|
|
|
2021-03-02 22:07:01 +00:00
|
|
|
let config = config.merge_with_cli(cli_opts);
|
2021-03-02 19:30:43 +00:00
|
|
|
Ok(config)
|
2021-03-02 19:13:58 +00:00
|
|
|
}
|