Skip to content

Commit

Permalink
use figment for config
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-re-ka committed Nov 22, 2023
1 parent 613fab6 commit 536c255
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 32 deletions.
88 changes: 87 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ rayon = "1.6.1"
regex = "1.7.1"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
serde_yaml = "0.9.16"
tokio = { version = "1.23.0", features = ["rt", "macros", "time", "rt-multi-thread", "io-util", "fs", "signal"] }
tokio-stream = "0.1.11"
tokio-util = { version = "0.7.4", features = ["codec"] }
Expand All @@ -35,10 +34,7 @@ zettabgp = "0.3.4"
hickory-resolver = "0.24.0"
include_dir = { version = "0.7.3", optional = true }
mime_guess = { version = "2.0.4", optional = true }

[[bin]]
name = "fernglas-configcheck"
path = "src/config_check.rs"
figment = { version = "0.10.12", features = ["yaml", "env"] }

[features]
embed-static = ["include_dir", "mime_guess"]
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
name = "config.yaml";
text = builtins.toJSON cfg.settings;
checkPhase = ''
${fernglasPkgs.fernglas}/bin/fernglas-configcheck $out
FERNGLAS_CONFIG_CHECK=1 ${fernglasPkgs.fernglas}/bin/fernglas $out
'';
};
in {
Expand Down
2 changes: 1 addition & 1 deletion frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
static: dist,
proxy: {
    "/api/": {
  target: 'http://localhost:3000'
  target: 'https://lg.staging.service.wobcom.de'
}
  },
},
Expand Down
13 changes: 0 additions & 13 deletions src/config_check.rs

This file was deleted.

18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ pub mod table_impl;

use serde::Deserialize;

pub fn config_path_from_args() -> String {
let mut args = std::env::args();
let program = args.next().unwrap();
let config_path = match args.next() {
Some(v) => v,
_ => usage(&program),
};
pub fn config_path_from_args() -> Option<String> {
let mut args = std::env::args().skip(1);
let config_path = args.next();
if args.next().is_some() {
usage(&program);
usage();
}

config_path
}

fn usage(program: &str) -> ! {
pub fn usage() -> ! {
let program = std::env::args().next().unwrap();
eprintln!("usage: {} <CONFIG>", program);
std::process::exit(1)
}
Expand All @@ -39,4 +36,7 @@ pub enum CollectorConfig {
pub struct Config {
pub collectors: Vec<CollectorConfig>,
pub api: api::ApiServerConfig,
/// Only check config and exit
#[serde(default)]
pub check_config: bool,
}
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use fernglas::*;
use futures_util::future::{join_all, select_all};
use log::*;
use tokio::signal::unix::{signal, SignalKind};
use figment::Figment;
use figment::providers::{Yaml, Env, Format};

#[cfg(feature = "mimalloc")]
#[global_allocator]
Expand All @@ -11,8 +13,21 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
async fn main() -> anyhow::Result<()> {
env_logger::init();

let config_path = config_path_from_args();
let cfg: Config = serde_yaml::from_slice(&tokio::fs::read(&config_path).await?)?;
let mut figment = Figment::new();
if let Some(config_path) = config_path_from_args() {
figment = figment.merge(Yaml::file(config_path));
}
figment = figment.merge(Env::prefixed("FERNGLAS_"));

let cfg: Config = figment.extract()?;

if cfg.collectors.is_empty() {
fernglas::usage();
}

if cfg.check_config {
std::process::exit(0);
}

let store: store_impl::InMemoryStore = Default::default();

Expand Down

0 comments on commit 536c255

Please sign in to comment.