Add logging + start wiring config

This commit is contained in:
Thomas Wickham 2021-03-02 20:51:33 +01:00
parent acc80d21e5
commit 0b6f482e51
6 changed files with 58 additions and 16 deletions

View File

@ -8,7 +8,7 @@ actix-web = "3"
anyhow = "*" anyhow = "*"
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
http = "*" http = "*"
passablewords = "*" log = "*"
serde = "*" serde = "*"
thiserror = "*" thiserror = "*"
tracing = "*" tracing = "*"

View File

@ -1,9 +1,17 @@
use clap::Clap; use clap::Clap;
/// lldap is a lightweight LDAP server /// lldap is a lightweight LDAP server
#[derive(Debug, Clap)] #[derive(Debug, Clap, Clone)]
#[clap(version = "0.1", author = "The LLDAP team")] #[clap(version = "0.1", author = "The LLDAP team")]
pub struct CLIOpts; pub struct CLIOpts {
/// Change config file name
#[clap(short, long, default_value = "lldap_config.toml")]
pub config_file: String,
/// Set verbose logging
#[clap(short, long)]
pub verbose: bool,
}
pub fn init() -> CLIOpts { pub fn init() -> CLIOpts {
CLIOpts::parse() CLIOpts::parse()

View File

@ -5,26 +5,33 @@ use figment::{
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)] use crate::infra::cli::CLIOpts;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Configuration { pub struct Configuration {
secret_pepper: String, pub log_level_verbose: bool,
some_text: String, pub secret_pepper: String,
pub some_text: String,
} }
impl Default for Configuration { impl Configuration {
fn default() -> Self { fn from_cli(cli_opts: CLIOpts) -> Self {
Configuration { Configuration {
log_level_verbose: cli_opts.verbose,
secret_pepper: String::from("secretsecretpepper"), secret_pepper: String::from("secretsecretpepper"),
some_text: String::new(), some_text: String::new(),
} }
} }
} }
pub fn init() -> Result<Configuration> { pub fn init(cli_opts: CLIOpts) -> Result<Configuration> {
let config: Configuration = Figment::from(Serialized::defaults(Configuration::default())) // FIXME cli arguments are less prioritary than toml config file or env... Not good.
.merge(Toml::file("lldap_config.toml")) let config: Configuration = Figment::from(Serialized::defaults(Configuration::from_cli(
.merge(Env::prefixed("LLDAP_")) cli_opts.clone(),
.extract()?; )))
.merge(Toml::file(cli_opts.config_file))
.merge(Env::prefixed("LLDAP_"))
.extract()?;
Ok(config) Ok(config)
} }

17
src/infra/logging.rs Normal file
View File

@ -0,0 +1,17 @@
use crate::infra::configuration::Configuration;
use anyhow::Context;
use tracing::subscriber::set_global_default;
use tracing_log::LogTracer;
pub fn init(_config: Configuration) -> anyhow::Result<()> {
// TODO: use config.log_level_verbose to set level
let subscriber = tracing_subscriber::fmt()
.with_timer(tracing_subscriber::fmt::time::time())
.with_target(false)
.with_level(true)
.finish();
LogTracer::init().context("Failed to set logger")?;
set_global_default(subscriber).context("Failed to set subscriber")?;
Ok(())
}

View File

@ -1,2 +1,3 @@
pub mod cli; pub mod cli;
pub mod configuration; pub mod configuration;
pub mod logging;

View File

@ -1,7 +1,16 @@
use anyhow::Result;
use log::*;
mod infra; mod infra;
fn main() { fn main() -> Result<()> {
let config = infra::configuration::init();
let cli_opts = infra::cli::init(); let cli_opts = infra::cli::init();
println!("Hello, world! Config: {:?}, CLI: {:?}", config, cli_opts); let config = infra::configuration::init(cli_opts.clone())?;
infra::logging::init(config.clone())?;
info!("Starting....");
debug!("Config: {:?}", config);
debug!("CLI: {:?}", cli_opts);
info!("End.");
Ok(())
} }