SolarEnergy/central_backend/src/app_config.rs

121 lines
3.1 KiB
Rust
Raw Normal View History

2024-06-27 16:55:09 +00:00
use clap::Parser;
2024-06-27 23:05:02 +00:00
use std::path::{Path, PathBuf};
2024-06-27 16:55:09 +00:00
/// Solar system central backend
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct AppConfig {
/// The port the server will listen to (using HTTPS)
#[arg(short, long, env, default_value = "0.0.0.0:8443")]
listen_address: String,
2024-06-27 23:05:02 +00:00
/// The port the server will listen to (using HTTP, for unsecure connections)
#[arg(short, long, env, default_value = "0.0.0.0:8080")]
unsecure_listen_address: String,
/// Public server hostname (assuming that the ports used are the same for listen address)
#[arg(short('H'), long, env, default_value = "localhost")]
hostname: String,
2024-06-27 16:55:09 +00:00
/// Server storage path
#[arg(short, long, env, default_value = "storage")]
storage: String,
}
lazy_static::lazy_static! {
static ref ARGS: AppConfig = {
AppConfig::parse()
};
}
impl AppConfig {
/// Get parsed command line arguments
pub fn get() -> &'static AppConfig {
&ARGS
}
2024-06-27 23:05:02 +00:00
/// URL for unsecure connections
pub fn unsecure_origin(&self) -> String {
format!(
"http://{}:{}",
self.hostname,
self.unsecure_listen_address.split_once(':').unwrap().1
)
}
/// URL for secure connections
pub fn secure_origin(&self) -> String {
format!(
"https://{}:{}",
self.hostname,
self.listen_address.split_once(':').unwrap().1
)
}
2024-06-27 16:55:09 +00:00
/// Get storage path
pub fn storage_path(&self) -> PathBuf {
Path::new(&self.storage).to_path_buf()
}
/// Get PKI storage path
pub fn pki_path(&self) -> PathBuf {
self.storage_path().join("pki")
}
/// Get PKI root CA cert path
pub fn root_ca_cert_path(&self) -> PathBuf {
self.pki_path().join("root_ca.pem")
}
2024-06-28 15:21:40 +00:00
/// Get PKI root CA CRL path
pub fn root_ca_crl_path(&self) -> PathBuf {
self.pki_path().join("root_ca.crl")
}
2024-06-27 16:55:09 +00:00
/// Get PKI root CA private key path
pub fn root_ca_priv_key_path(&self) -> PathBuf {
self.pki_path().join("root_ca.key")
}
2024-06-27 23:05:02 +00:00
/// Get PKI web CA cert path
pub fn web_ca_cert_path(&self) -> PathBuf {
self.pki_path().join("web_ca.pem")
}
2024-06-28 15:21:40 +00:00
/// Get PKI web CA CRL path
pub fn web_ca_crl_path(&self) -> PathBuf {
self.pki_path().join("web_ca.crl")
}
2024-06-27 23:05:02 +00:00
/// Get PKI web CA private key path
pub fn web_ca_priv_key_path(&self) -> PathBuf {
self.pki_path().join("web_ca.key")
}
/// Get PKI devices CA cert path
pub fn devices_ca_cert_path(&self) -> PathBuf {
self.pki_path().join("devices_ca.pem")
}
2024-06-28 15:21:40 +00:00
/// Get PKI devices CA CRL path
pub fn devices_ca_crl_path(&self) -> PathBuf {
self.pki_path().join("devices_ca.crl")
}
2024-06-27 23:05:02 +00:00
/// Get PKI devices CA private key path
pub fn devices_ca_priv_key_path(&self) -> PathBuf {
self.pki_path().join("devices_ca.key")
}
2024-06-27 16:55:09 +00:00
}
#[cfg(test)]
mod test {
use crate::app_config::AppConfig;
#[test]
fn verify_cli() {
use clap::CommandFactory;
AppConfig::command().debug_assert()
}
2024-06-27 23:05:02 +00:00
}