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 {
|
2021-03-22 12:46:10 +01:00
|
|
|
#[deprecated]
|
2020-11-24 19:06:49 +01:00
|
|
|
pub fn new() -> Result<Self, ConfigError> {
|
2021-03-22 12:46:10 +01:00
|
|
|
Self::load()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load() -> Result<Self, ConfigError> {
|
2020-11-24 19:06:49 +01:00
|
|
|
let mut config = Config::new();
|
|
|
|
config
|
|
|
|
.merge(File::with_name("hoc.toml").required(false))?
|
|
|
|
.merge(Environment::with_prefix("hoc"))?
|
|
|
|
.set_default("repodir", "./repos")?
|
|
|
|
.set_default("cachedir", "./cache")?
|
|
|
|
.set_default("workers", 4)?
|
|
|
|
.set_default("port", 8080)?
|
|
|
|
.set_default("host", "0.0.0.0")?;
|
|
|
|
config.try_into()
|
|
|
|
}
|
|
|
|
}
|