Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions feather/server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,30 @@ use crate::{favicon::Favicon, Options};
const DEFAULT_CONFIG: &str = include_str!("../config.toml");

/// Loads the config, creating a default config if needed.
pub fn load(path: &str) -> anyhow::Result<Config> {
pub fn load(path: &str) -> anyhow::Result<ConfigContainer> {
let path = Path::new(path);
let default_config = DEFAULT_CONFIG;
let mut is_created = false;

if !path.exists() {
log::info!("Creating default config");
fs::write(path, default_config)?;
is_created = true;
}

let config_string = fs::read_to_string(path)?;
let config: Config = toml::from_str(&config_string).context("invalid config.toml file")?;

Ok(config)
Ok(ConfigContainer {
config,
was_config_created: is_created,
})
}

/// A wrapper for the result returned by [load].
pub struct ConfigContainer {
pub config: Config,
pub was_config_created: bool,
}

#[derive(Debug, Deserialize)]
Expand Down
11 changes: 8 additions & 3 deletions feather/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ const CONFIG_PATH: &str = "config.toml";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
log::info!("Loading configuration");
let config =
feather_server::config::load(CONFIG_PATH).context("failed to load configuration file")?;
let feather_server::config::ConfigContainer {
config,
was_config_created,
} = feather_server::config::load(CONFIG_PATH).context("failed to load configuration file")?;
logging::init(config.log.level);
if was_config_created {
log::info!("Created default config");
}
log::info!("Loaded config");

log::info!("Creating server");
let options = config.to_options();
Expand Down