Skip to content

Commit

Permalink
use figment for config
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyuyureka committed Nov 22, 2023
1 parent 613fab6 commit f1fb34b
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 39 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"]
3 changes: 2 additions & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ api:
bind: "[::]:3000"

collectors:
- collector_type: Bmp
my_bmp_collector:
collector_type: Bmp
bind: "[::]:11019"
peers:
"192.0.2.1": {}
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
8 changes: 4 additions & 4 deletions manual/src/deployment/nixos.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ in {
enable = true;
settings = {
api.bind = "[::1]:3000";
collectors = [
{
collectors = {
my_bmp_collector = {
collector_type = "Bmp";
bind = "[::]:${toString bmpPort}";
peers = {
"192.0.2.1" = {};
};
}
];
};
};
};
};
Expand Down
13 changes: 0 additions & 13 deletions src/config_check.rs

This file was deleted.

21 changes: 11 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ pub mod store_impl;
pub mod table_impl;

use serde::Deserialize;
use std::collections::HashMap;

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 @@ -37,6 +35,9 @@ pub enum CollectorConfig {

#[derive(Deserialize, Debug)]
pub struct Config {
pub collectors: Vec<CollectorConfig>,
pub collectors: HashMap<String, CollectorConfig>,
pub api: api::ApiServerConfig,
/// Only check config and exit
#[serde(default)]
pub check_config: bool,
}
19 changes: 16 additions & 3 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,19 @@ 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_").split("_"));

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

trace!("config: {:#?}", &cfg);

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

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

Expand All @@ -29,7 +42,7 @@ async fn main() -> anyhow::Result<()> {
shutdown_rx.clone(),
)));

futures.extend(cfg.collectors.into_iter().map(|collector| match collector {
futures.extend(cfg.collectors.into_iter().map(|(_, collector)| match collector {
CollectorConfig::Bmp(cfg) => {
tokio::task::spawn(bmp_collector::run(cfg, store.clone(), shutdown_rx.clone()))
}
Expand Down

0 comments on commit f1fb34b

Please sign in to comment.