Skip to content

Commit 22142a7

Browse files
committed
Move config loading to a separate function.
1 parent 7a15665 commit 22142a7

2 files changed

Lines changed: 39 additions & 41 deletions

File tree

src/config.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ impl Config {
6666

6767
Ok(config)
6868
}
69+
70+
pub async fn load_or_create(path: &PathBuf) -> Result<Config, ()> {
71+
let mut config = if path.exists() {
72+
Self::read_from_file(path)?
73+
} else {
74+
tracing::info!(
75+
"No config file found at {}, creating a default config file.",
76+
path.display()
77+
);
78+
let config = Self::default();
79+
80+
let serialized =
81+
toml::to_string_pretty(&config).map_err(|e| tracing::error!("Failed to serialize config: {e}"))?;
82+
83+
let dir = path
84+
.parent()
85+
.ok_or_else(|| tracing::error!("Failed to get parent directory of config file."))?;
86+
std::fs::create_dir_all(dir).map_err(|e| tracing::error!("Failed to create config directory: {e}"))?;
87+
std::fs::write(path, serialized).map_err(|e| tracing::error!("Failed to save config file: {e}"))?;
88+
89+
config
90+
};
91+
92+
config.resolve_paths();
93+
Ok(config)
94+
}
95+
96+
fn resolve_paths(&mut self) {
97+
let cert_path = self.webserver.certificate.to_string_lossy().to_string();
98+
let cert_path =
99+
shellexpand::full(&cert_path).expect("Failed to expand certificate path");
100+
self.webserver.certificate = cert_path.to_string().into();
101+
102+
let private_key_path = self.webserver.private_key.to_string_lossy().to_string();
103+
let private_key_path =
104+
shellexpand::full(&private_key_path).expect("Failed to expand private key path");
105+
self.webserver.private_key = private_key_path.to_string().into();
106+
}
69107
}
70108

71109
impl Default for Config {

src/main.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,7 @@ async fn main() -> Result<(), ()> {
4141
.with(EnvFilter::from_default_env())
4242
.init();
4343

44-
// Ensure rustls has a single crypto provider selected at process start.
45-
// When multiple crypto backends (ring, aws-lc-rs) are present the crate
46-
// requires an explicit choice. Install the default provider now.
47-
// Prefer the `ring` provider explicitly to avoid runtime ambiguity
48-
// when multiple crypto backends are present in the dependency graph.
49-
// Construct the provider from the `ring` module and install it.
50-
let provider = rustls::crypto::ring::default_provider();
51-
let _ = provider.install_default();
52-
53-
let mut config;
54-
if args.config.exists() {
55-
config = Config::read_from_file(args.config).unwrap_or_else(|()| std::process::exit(1));
56-
} else {
57-
tracing::info!(
58-
"No config file found at {}, creating a default config file.",
59-
args.config.display()
60-
);
61-
config = Config::default();
62-
63-
let serialized_config =
64-
toml::to_string_pretty(&config).map_err(|e| tracing::error!("Failed to serialize config: {e}"))?;
65-
66-
let config_dir = args
67-
.config
68-
.parent()
69-
.ok_or_else(|| tracing::error!("Failed to get parent directory of config file."))?;
70-
std::fs::create_dir_all(config_dir).map_err(|e| tracing::error!("Failed to create config directory: {e}"))?;
71-
std::fs::write(args.config, serialized_config)
72-
.map_err(|e| tracing::error!("Failed to save config file: {e}"))?;
73-
}
74-
75-
// Resolve these paths so that the rest of the code doesn't need to.
76-
let cert_path = config.webserver.certificate.to_string_lossy().to_string();
77-
let cert_path =
78-
shellexpand::full(&cert_path).map_err(|e| tracing::error!("Failed to expand certificate path: {e}"))?;
79-
config.webserver.certificate = cert_path.to_string().into();
80-
81-
let private_key_path = config.webserver.private_key.to_string_lossy().to_string();
82-
let private_key_path =
83-
shellexpand::full(&private_key_path).map_err(|e| tracing::error!("Failed to expand private key path: {e}"))?;
84-
config.webserver.private_key = private_key_path.to_string().into();
44+
let mut config = Config::load_or_create(&args.config).await?;
8545

8646
tracing::debug!("Using configuration:\n{:#?}", config);
8747

0 commit comments

Comments
 (0)