mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
Add logging + start wiring config
This commit is contained in:
parent
acc80d21e5
commit
0b6f482e51
@ -8,7 +8,7 @@ actix-web = "3"
|
||||
anyhow = "*"
|
||||
clap = "3.0.0-beta.2"
|
||||
http = "*"
|
||||
passablewords = "*"
|
||||
log = "*"
|
||||
serde = "*"
|
||||
thiserror = "*"
|
||||
tracing = "*"
|
||||
|
@ -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()
|
||||
|
@ -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
17
src/infra/logging.rs
Normal 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(())
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
pub mod cli;
|
||||
pub mod configuration;
|
||||
pub mod logging;
|
||||
|
15
src/main.rs
15
src/main.rs
@ -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(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user