diff options
author | LUXEY Adrien <adrien.luxey@inria.fr> | 2021-08-14 19:12:18 +0200 |
---|---|---|
committer | LUXEY Adrien <adrien.luxey@inria.fr> | 2021-08-16 10:26:06 +0200 |
commit | ae9550ce23bbc85b05669fe5ec4406c8a67417ec (patch) | |
tree | bab55a23444b06ded2374dd0380b53a304426b33 /src/config/options.rs | |
parent | ade0090cdb4a021830439dc0d9a04b2748601ea5 (diff) | |
download | diplonat-ae9550ce23bbc85b05669fe5ec4406c8a67417ec.tar.gz diplonat-ae9550ce23bbc85b05669fe5ec4406c8a67417ec.zip |
New configuration parsing using envy. Added minimal functionnality for
the future ACME parameters. Tests written and passing.
WIP: added envy dependncy and ConfigOpts structs that will constitute Diplonat's configuration
WIP: ConfigOpts from_env() and validate() methods written. No API change (the env names remain unchanged)! Now need to use our new ConfigOpts struct instead of Environment, and update references to the environment variables in the code.
WIP: RuntimeConfig with business logic done. Tests written, but they are all running from the same process - setting environment variables in each test produces incoherent results. Another solution for testing is needed.
WIP: tests are fully written using 'from_iter' and all passing
Diffstat (limited to 'src/config/options.rs')
-rw-r--r-- | src/config/options.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/config/options.rs b/src/config/options.rs new file mode 100644 index 0000000..a76cf57 --- /dev/null +++ b/src/config/options.rs @@ -0,0 +1,75 @@ +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 +#[derive(Clone, Debug, Default, Deserialize)] +pub struct ConfigOptsBase { + /// 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>, +} + +/// ACME configuration options +#[derive(Clone, Debug, Default, Deserialize)] +pub struct ConfigOptsAcme { + /// Whether ACME is enabled [default: false] + #[serde(default)] + pub enable: bool, + + /// The default domain holder's e-mail [default: None] + pub email: Option<String>, +} + +/// Consul configuration options +#[derive(Clone, Debug, Default, Deserialize)] +pub struct ConfigOptsConsul { + /// 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>, +} + +/// Model of all potential configuration options +#[derive(Debug)] +pub struct ConfigOpts { + pub base: ConfigOptsBase, + pub acme: ConfigOptsAcme, + pub consul: ConfigOptsConsul, +} + +impl ConfigOpts { + 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()?; + + RuntimeConfig::new(Self { + base: base, + consul: consul, + acme: acme, + }) + } + + pub fn from_iter<Iter: Clone>(iter: Iter) -> Result<RuntimeConfig> + where Iter: IntoIterator<Item = (String, String)> { + 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())?; + + RuntimeConfig::new(Self { + base: base, + consul: consul, + acme: acme, + }) + } +}
\ No newline at end of file |