2021-08-14 17:12:18 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
use crate::config::RuntimeConfig;
|
|
|
|
|
|
|
|
// This code is inspired by the Trunk crate (https://github.com/thedodd/trunk)
|
|
|
|
|
|
|
|
// This file parses the options that can be declared in the environment.
|
|
|
|
// runtime.rs applies business logic and builds RuntimeConfig structs.
|
|
|
|
|
|
|
|
/// Base configuration options
|
2021-08-16 09:19:16 +00:00
|
|
|
#[derive(Clone, Default, Deserialize)]
|
2021-08-14 17:12:18 +00:00
|
|
|
pub struct ConfigOptsBase {
|
2021-09-11 14:34:03 +00:00
|
|
|
/// This node's private IP address [default: None]
|
|
|
|
pub private_ip: Option<String>,
|
|
|
|
/// Expiration time for IGD rules [default: 60]
|
|
|
|
pub expiration_time: Option<u16>,
|
|
|
|
/// Refresh time for IGD and Firewall rules [default: 300]
|
|
|
|
pub refresh_time: Option<u16>,
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ACME configuration options
|
2021-08-16 09:19:16 +00:00
|
|
|
#[derive(Clone, Default, Deserialize)]
|
2021-08-14 17:12:18 +00:00
|
|
|
pub struct ConfigOptsAcme {
|
2021-09-11 14:34:03 +00:00
|
|
|
/// Whether ACME is enabled [default: false]
|
|
|
|
#[serde(default)]
|
|
|
|
pub enable: bool,
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
/// The default domain holder's e-mail [default: None]
|
|
|
|
pub email: Option<String>,
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Consul configuration options
|
2021-08-16 09:19:16 +00:00
|
|
|
#[derive(Clone, Default, Deserialize)]
|
2021-08-14 17:12:18 +00:00
|
|
|
pub struct ConfigOptsConsul {
|
2021-09-11 14:34:03 +00:00
|
|
|
/// Consul's node name [default: None]
|
|
|
|
pub node_name: Option<String>,
|
|
|
|
/// Consul's REST URL [default: "http://127.0.0.1:8500"]
|
|
|
|
pub url: Option<String>,
|
2021-12-30 19:42:56 +00:00
|
|
|
/// Consul's CA certificate [default: None]
|
|
|
|
pub ca_cert: Option<String>,
|
2022-12-01 16:47:31 +00:00
|
|
|
/// Skip TLS verification for Consul server [default: false]
|
|
|
|
#[serde(default)]
|
2022-08-24 16:22:00 +00:00
|
|
|
pub tls_skip_verify: bool,
|
2021-12-30 19:42:56 +00:00
|
|
|
/// Consul's client certificate [default: None]
|
|
|
|
pub client_cert: Option<String>,
|
|
|
|
/// Consul's client key [default: None]
|
|
|
|
pub client_key: Option<String>,
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Model of all potential configuration options
|
|
|
|
pub struct ConfigOpts {
|
2021-09-11 14:34:03 +00:00
|
|
|
pub base: ConfigOptsBase,
|
|
|
|
pub acme: ConfigOptsAcme,
|
|
|
|
pub consul: ConfigOptsConsul,
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigOpts {
|
2021-09-11 14:34:03 +00:00
|
|
|
pub fn from_env() -> Result<RuntimeConfig> {
|
|
|
|
let base: ConfigOptsBase = envy::prefixed("DIPLONAT_").from_env()?;
|
|
|
|
let consul: ConfigOptsConsul = envy::prefixed("DIPLONAT_CONSUL_").from_env()?;
|
|
|
|
let acme: ConfigOptsAcme = envy::prefixed("DIPLONAT_ACME_").from_env()?;
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
RuntimeConfig::new(Self {
|
|
|
|
base: base,
|
|
|
|
consul: consul,
|
|
|
|
acme: acme,
|
|
|
|
})
|
|
|
|
}
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
// Currently only used in tests
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn from_iter<Iter: Clone>(iter: Iter) -> Result<RuntimeConfig>
|
2021-12-25 18:19:19 +00:00
|
|
|
where
|
|
|
|
Iter: IntoIterator<Item = (String, String)>,
|
|
|
|
{
|
2021-09-11 14:34:03 +00:00
|
|
|
let base: ConfigOptsBase = envy::prefixed("DIPLONAT_").from_iter(iter.clone())?;
|
|
|
|
let consul: ConfigOptsConsul = envy::prefixed("DIPLONAT_CONSUL_").from_iter(iter.clone())?;
|
|
|
|
let acme: ConfigOptsAcme = envy::prefixed("DIPLONAT_ACME_").from_iter(iter.clone())?;
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
RuntimeConfig::new(Self {
|
|
|
|
base: base,
|
|
|
|
consul: consul,
|
|
|
|
acme: acme,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|