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 = "*"
clap = "3.0.0-beta.2"
http = "*"
passablewords = "*"
log = "*"
serde = "*"
thiserror = "*"
tracing = "*"

View File

@ -1,9 +1,17 @@
use clap::Clap;
/// lldap is a lightweight LDAP server
#[derive(Debug, Clap)]
#[derive(Debug, Clap, Clone)]
#[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 {
CLIOpts::parse()

View File

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

View File

@ -1,7 +1,16 @@
use anyhow::Result;
use log::*;
mod infra;
fn main() {
let config = infra::configuration::init();
fn main() -> Result<()> {
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(())
}