Skip to content

Commit b28eb26

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

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

src/config.rs

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

6767
Ok(config)
6868
}
69+
70+
pub async fn load_or_create(path: &PathBuf) -> Result<Config, ()> {
71+
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+
Ok(config)
90+
}
91+
}
6992
}
7093

7194
impl Default for Config {

src/main.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,12 @@ async fn main() -> Result<(), ()> {
4242
.init();
4343

4444
// 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();
45+
// The aws-lc-rs backend is the default for rustls and is installed here
46+
// to avoid runtime ambiguity when multiple crypto backends are present.
47+
let provider = rustls::crypto::aws_lc_rs::default_provider();
5148
let _ = provider.install_default();
5249

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-
}
50+
let mut config = Config::load_or_create(&args.config).await?;
7451

7552
// Resolve these paths so that the rest of the code doesn't need to.
7653
let cert_path = config.webserver.certificate.to_string_lossy().to_string();

0 commit comments

Comments
 (0)