diff options
author | adrien <adrien@luxeylab.net> | 2021-08-25 17:20:31 +0200 |
---|---|---|
committer | adrien <adrien@luxeylab.net> | 2021-08-26 16:05:39 +0200 |
commit | 76fe63791baea5577bde3f83243f85ba781f46fe (patch) | |
tree | 1f7ffed5671f30cbd13093db02d7462183bf1952 /src/config/runtime.rs | |
parent | fa25c54e47decf9f323ba0c614f4d9de106626d5 (diff) | |
download | diplonat-76fe63791baea5577bde3f83243f85ba781f46fe.tar.gz diplonat-76fe63791baea5577bde3f83243f85ba781f46fe.zip |
Rewrote the configuration options to make Diplonat modular: IGD and Firewall modules will only be enabled if correponding `DIPLONAT_*_ENABLE` is set. (Breaking changes!)
Also rearranged imports throughout the project, because I couldn't resist, sorry.
Diffstat (limited to 'src/config/runtime.rs')
-rw-r--r-- | src/config/runtime.rs | 75 |
1 files changed, 42 insertions, 33 deletions
diff --git a/src/config/runtime.rs b/src/config/runtime.rs index 58c86b9..f83a6b5 100644 --- a/src/config/runtime.rs +++ b/src/config/runtime.rs @@ -2,17 +2,14 @@ use std::time::Duration; use anyhow::{Result, anyhow}; -use crate::config::{ConfigOpts, ConfigOptsAcme, ConfigOptsBase, ConfigOptsConsul}; +use crate::config::{ConfigOpts, ConfigOptsConsul, ConfigOptsAcme, ConfigOptsFirewall, ConfigOptsIgd}; // This code is inspired by the Trunk crate (https://github.com/thedodd/trunk) // In this file, we take ConfigOpts and transform them into ready-to-use RuntimeConfig. // We apply default values and business logic. -#[derive(Debug)] -pub struct RuntimeConfigAcme { - pub email: String, -} +// Consul config is mandatory, all the others are optional. #[derive(Debug)] pub struct RuntimeConfigConsul { @@ -21,6 +18,11 @@ pub struct RuntimeConfigConsul { } #[derive(Debug)] +pub struct RuntimeConfigAcme { + pub email: String, +} + +#[derive(Debug)] pub struct RuntimeConfigFirewall { pub refresh_time: Duration, } @@ -34,18 +36,18 @@ pub struct RuntimeConfigIgd { #[derive(Debug)] pub struct RuntimeConfig { - pub acme: Option<RuntimeConfigAcme>, pub consul: RuntimeConfigConsul, - pub firewall: RuntimeConfigFirewall, - pub igd: RuntimeConfigIgd, + pub acme: Option<RuntimeConfigAcme>, + pub firewall: Option<RuntimeConfigFirewall>, + pub igd: Option<RuntimeConfigIgd>, } impl RuntimeConfig { pub fn new(opts: ConfigOpts) -> Result<Self> { - let acme = RuntimeConfigAcme::new(opts.acme.clone())?; let consul = RuntimeConfigConsul::new(opts.consul.clone())?; - let firewall = RuntimeConfigFirewall::new(opts.base.clone())?; - let igd = RuntimeConfigIgd::new(opts.base.clone())?; + let acme = RuntimeConfigAcme::new(opts.acme.clone())?; + let firewall = RuntimeConfigFirewall::new(opts.firewall.clone())?; + let igd = RuntimeConfigIgd::new(opts.igd.clone())?; Ok(Self { acme, @@ -56,6 +58,19 @@ impl RuntimeConfig { } } +impl RuntimeConfigConsul { + pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> { + let node_name = opts.node_name.expect( + "'DIPLONAT_CONSUL_NODE_NAME' is required"); + let url = opts.url.unwrap_or(super::CONSUL_URL.to_string()); + + Ok(Self { + node_name, + url, + }) + } +} + impl RuntimeConfigAcme { pub fn new(opts: ConfigOptsAcme) -> Result<Option<Self>> { if !opts.enable { @@ -63,8 +78,7 @@ impl RuntimeConfigAcme { } let email = opts.email.expect( - "'DIPLONAT_ACME_EMAIL' environment variable is required \ - if 'DIPLONAT_ACME_ENABLE' == 'true'"); + "'DIPLONAT_ACME_EMAIL' is required if ACME is enabled"); Ok(Some(Self { email, @@ -72,34 +86,29 @@ impl RuntimeConfigAcme { } } -impl RuntimeConfigConsul { - pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> { - let node_name = opts.node_name.expect( - "'DIPLONAT_CONSUL_NODE_NAME' environment variable is required"); - let url = opts.url.unwrap_or(super::CONSUL_URL.to_string()); - - Ok(Self { - node_name, - url, - }) - } -} - impl RuntimeConfigFirewall { - pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> { + pub(super) fn new(opts: ConfigOptsFirewall) -> Result<Option<Self>> { + if !opts.enable { + return Ok(None); + } + let refresh_time = Duration::from_secs( opts.refresh_time.unwrap_or(super::REFRESH_TIME).into()); - Ok(Self { + Ok(Some(Self { refresh_time, - }) + })) } } impl RuntimeConfigIgd { - pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> { + pub(super) fn new(opts: ConfigOptsIgd) -> Result<Option<Self>> { + if !opts.enable { + return Ok(None); + } + let private_ip = opts.private_ip.expect( - "'DIPLONAT_PRIVATE_IP' environment variable is required"); + "'DIPLONAT_IGD_PRIVATE_IP' is required if IGD is enabled"); let expiration_time = Duration::from_secs( opts.expiration_time.unwrap_or(super::EXPIRATION_TIME).into()); let refresh_time = Duration::from_secs( @@ -112,10 +121,10 @@ impl RuntimeConfigIgd { refresh_time.as_secs())); } - Ok(Self { + Ok(Some(Self { private_ip, expiration_time, refresh_time, - }) + })) } }
\ No newline at end of file |