Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve overall config error handling #469

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::signing::parse_secret_key;
use crate::store::Store;
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use serde::Deserialize;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -32,7 +32,8 @@ pub(crate) struct SigningKey {
}

// TODO(conni2461): users to restrict access
#[derive(Deserialize, Debug, Default)]
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub(crate) struct Config {
#[serde(default = "default_bind")]
pub(crate) bind: String,
Expand All @@ -46,6 +47,7 @@ pub(crate) struct Config {
#[serde(default = "default_virtual_store")]
pub(crate) virtual_nix_store: String,

#[serde(default)]
pub(crate) real_nix_store: Option<String>,

#[serde(default)]
Expand All @@ -63,19 +65,52 @@ pub(crate) struct Config {
pub(crate) store: Store,
}

pub(crate) fn load() -> Result<Config> {
let settings_file = std::env::var("CONFIG_FILE").unwrap_or_else(|_| "settings.toml".to_owned());
impl Default for Config {
fn default() -> Self {
Config {
bind: default_bind(),
workers: default_workers(),
max_connection_rate: default_connection_rate(),
priority: default_priority(),
virtual_nix_store: default_virtual_store(),
real_nix_store: None,
sign_key_path: None,
sign_key_paths: Vec::new(),
tls_cert_path: None,
tls_key_path: None,
secret_keys: Vec::new(),
store: Store::new(default_virtual_store(), None),
}
}
}

let mut settings: Config = if Path::new(&settings_file).exists() {
impl Config {
pub(crate) fn load(settings_file: &Path) -> Result<Config> {
toml::from_str(
&read_to_string(&settings_file)
.with_context(|| format!("Couldn't read config file '{settings_file}'"))?,
&read_to_string(settings_file).with_context(|| {
format!("Couldn't read config file '{}'", settings_file.display())
})?,
)
.with_context(|| format!("Couldn't parse config file '{settings_file}'"))?
} else {
Config::default()
.with_context(|| format!("Couldn't parse config file '{}'", settings_file.display()))
}
}

pub(crate) fn load() -> Result<Config> {
let mut settings = match std::env::var("CONFIG_FILE") {
Err(_) => {
if Path::new("settings.toml").exists() {
Config::load(Path::new("settings.toml"))?
} else {
return Ok(Config::default());
}
}
Ok(settings_file) => Config::load(Path::new(&settings_file))?,
};

if settings.workers == 0 {
bail!("workers must be greater than 0");
}

if let Some(sign_key_path) = &settings.sign_key_path {
log::warn!(
"The sign_key_path configuration option is deprecated. Use sign_key_paths instead."
Expand Down