Browse Source

env vs config #12

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
adc6744d16
  1. 1
      Makefile
  2. 1
      server/.gitignore
  3. 3
      server/config.example.toml
  4. 27
      server/src/config.rs

1
Makefile

@ -3,6 +3,7 @@ all: server client
cp server/target/x86_64-unknown-linux-musl/release/hastic release
mkdir release/public
cp -r client/dist/* release/public/
cp server/config.example.toml release/config.toml
server:

1
server/.gitignore vendored

@ -1,3 +1,4 @@
target/*
data.db
data/*
config.toml

3
server/config.example.toml

@ -0,0 +1,3 @@
port = 8000
query = "rate(go_memstats_alloc_bytes_total[5m])"
prom_url = "http://localhost:9090"

27
server/src/config.rs

@ -1,3 +1,6 @@
use std::collections::HashMap;
pub struct Config {
pub prom_url: String,
pub query: String,
@ -7,10 +10,28 @@ pub struct Config {
// TODO: use actual config and env variables
impl Config {
pub fn new() -> Config {
let mut config = config::Config::default();
if std::path::Path::new("config.toml").exists() {
config.merge(config::File::with_name("config")).unwrap();
}
config.merge(config::Environment::with_prefix("HASTIC")).unwrap();
if config.get::<u16>("port").is_err() {
config.set("port", "8000").unwrap();
}
if config.get::<String>("prom_url").is_err() {
config.set("prom_url", "http://localhost:9090").unwrap();
}
if config.get::<String>("query").is_err() {
config.set("query", "rate(go_memstats_alloc_bytes_total[5m])").unwrap();
}
Config {
port: 8000,
prom_url: "http://localhost:9090".to_owned(),
query: "rate(go_memstats_alloc_bytes_total[5m])".to_owned(),
port: config.get::<u16>("port").unwrap(),
prom_url: config.get("prom_url").unwrap(),
query: config.get("query").unwrap()
}
}
}

Loading…
Cancel
Save