Skip to content

Commit

Permalink
make config file optional
Browse files Browse the repository at this point in the history
fixes #397
  • Loading branch information
Mic92 committed Nov 25, 2024
1 parent 90d523b commit a9ef99c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::store::Store;
use anyhow::{Context, Result};
use serde::Deserialize;
use std::fs::read_to_string;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

fn default_bind() -> String {
"[::]:5000".into()
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(crate) struct SigningKey {
}

// TODO(conni2461): users to restrict access
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Default)]
pub(crate) struct Config {
#[serde(default = "default_bind")]
pub(crate) bind: String,
Expand Down Expand Up @@ -65,11 +65,15 @@ pub(crate) struct Config {

pub(crate) fn load() -> Result<Config> {
let settings_file = std::env::var("CONFIG_FILE").unwrap_or_else(|_| "settings.toml".to_owned());
let mut settings: Config = toml::from_str(
&read_to_string(&settings_file)
.with_context(|| format!("Couldn't read config file '{settings_file}'"))?,
)
.with_context(|| format!("Couldn't parse config file '{settings_file}'"))?;

let mut settings: Config = if Path::new(&settings_file).exists() {
toml::from_str(&read_to_string(&settings_file)
.with_context(|| format!("Couldn't read config file '{settings_file}'"))?)
.with_context(|| format!("Couldn't parse config file '{settings_file}'"))?
} else {
Config::default()
};

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

0 comments on commit a9ef99c

Please sign in to comment.