hoc/src/config.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

2020-11-24 19:06:49 +01:00
use config::{Config, ConfigError, Environment, File};
2019-05-14 01:11:39 +02:00
use std::path::PathBuf;
2020-11-24 19:06:49 +01:00
#[derive(Debug, Deserialize)]
pub struct Settings {
2019-05-14 01:11:39 +02:00
/// Path to store cloned repositories
2020-11-24 19:06:49 +01:00
pub repodir: PathBuf,
2019-05-14 01:11:39 +02:00
/// Path to store cache
2020-11-24 19:06:49 +01:00
pub cachedir: PathBuf,
2019-05-14 01:11:39 +02:00
/// Port to listen on
2020-11-24 19:06:49 +01:00
pub port: u16,
2019-05-14 01:11:39 +02:00
/// Interface to listen on
2020-11-24 19:06:49 +01:00
pub host: String,
/// Base URL
pub base_url: String,
2019-05-14 01:11:39 +02:00
/// Number of worker threads
2020-11-24 19:06:49 +01:00
pub workers: usize,
2019-05-14 01:11:39 +02:00
}
2020-11-24 19:06:49 +01:00
impl Settings {
2023-06-13 10:22:37 +02:00
/// Load the configuration from file and environment.
///
/// # Errors
///
/// * File cannot be read or parsed
/// * Environment variables cannot be parsed
pub fn load() -> Result<Self, ConfigError> {
2022-02-26 16:22:36 +01:00
Config::builder()
.add_source(File::with_name("hoc.toml").required(false))
.add_source(Environment::with_prefix("hoc"))
2020-11-24 19:06:49 +01:00
.set_default("repodir", "./repos")?
.set_default("cachedir", "./cache")?
.set_default("workers", 4)?
.set_default("port", 8080)?
2022-02-26 16:22:36 +01:00
.set_default("host", "0.0.0.0")?
.build()?
.try_deserialize()
2020-11-24 19:06:49 +01:00
}
}