From 9e33742d81b01e21260a2a3d3ccd6622eed7db5c Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Tue, 24 Nov 2020 19:06:49 +0100 Subject: [PATCH] Use new config --- src/config.rs | 52 ++++++++++++++++++++++++------------------------- src/main.rs | 8 ++++---- src/statics.rs | 7 +++---- src/template.rs | 2 +- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/config.rs b/src/config.rs index c44c537..feab98b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,41 +1,41 @@ +use config::{Config, ConfigError, Environment, File}; use std::path::PathBuf; -use structopt::StructOpt; -#[derive(StructOpt, Debug)] -pub(crate) struct Opt { - #[structopt( - short = "o", - long = "outdir", - parse(from_os_str), - default_value = "./repos" - )] +#[derive(Debug, Deserialize)] +pub struct Settings { /// Path to store cloned repositories - pub(crate) outdir: PathBuf, - #[structopt( - short = "c", - long = "cachedir", - parse(from_os_str), - default_value = "./cache" - )] + pub repodir: PathBuf, /// Path to store cache - pub(crate) cachedir: PathBuf, - #[structopt(short = "p", long = "port", default_value = "8080")] + pub cachedir: PathBuf, /// Port to listen on - pub(crate) port: u16, - #[structopt(short = "h", long = "host", default_value = "0.0.0.0")] + pub port: u16, /// Interface to listen on - pub(crate) host: String, - #[structopt(short = "d", long = "domain", default_value = "hitsofcode.com")] - /// Interface to listen on - pub(crate) domain: String, - #[structopt(short = "w", long = "workers", default_value = "4")] + pub host: String, + /// Base URL + pub base_url: String, /// Number of worker threads - pub(crate) workers: usize, + pub workers: usize, } pub(crate) fn init() { + dotenv::dotenv().ok(); std::env::set_var("RUST_LOG", "actix_web=info,hoc=info"); openssl_probe::init_ssl_cert_env_vars(); tracing_subscriber::fmt().init(); } + +impl Settings { + pub fn new() -> Result { + 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() + } +} diff --git a/src/main.rs b/src/main.rs index d3e3e51..499daca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -371,7 +371,7 @@ async fn overview( let repo_info = RepoInfo { commit_url: &T::commit_url(&repo, &head), commits, - domain: &OPT.domain, + base_url: &OPT.base_url, head: &head, hoc, hoc_pretty: &hoc_pretty, @@ -399,7 +399,7 @@ async fn index() -> Result { &mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed), - &OPT.domain, + &OPT.base_url, )?; Ok(HttpResponse::Ok().content_type("text/html").body(buf)) } @@ -412,7 +412,7 @@ async fn generate(params: web::Form>) -> Result &mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed), - &OPT.domain, + &OPT.base_url, params.service.url(), params.service.service(), &repo, @@ -442,7 +442,7 @@ fn favicon32() -> HttpResponse { async fn start_server() -> std::io::Result<()> { let interface = format!("{}:{}", OPT.host, OPT.port); let state = Arc::new(State { - repos: OPT.outdir.display().to_string(), + repos: OPT.repodir.display().to_string(), cache: OPT.cachedir.display().to_string(), }); HttpServer::new(move || { diff --git a/src/statics.rs b/src/statics.rs index 53e0576..387fdc6 100644 --- a/src/statics.rs +++ b/src/statics.rs @@ -1,6 +1,5 @@ -use crate::{config::Opt, count::count_repositories}; +use crate::{config::Settings, count::count_repositories}; use std::sync::atomic::AtomicUsize; -use structopt::StructOpt; pub struct VersionInfo<'a> { pub commit: &'a str, @@ -16,7 +15,7 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png"); lazy_static! { pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new(); - pub(crate) static ref OPT: Opt = Opt::from_args(); + pub(crate) static ref OPT: Settings = Settings::new().unwrap(); pub(crate) static ref REPO_COUNT: AtomicUsize = - AtomicUsize::new(count_repositories(&OPT.outdir).unwrap()); + AtomicUsize::new(count_repositories(&OPT.repodir).unwrap()); } diff --git a/src/template.rs b/src/template.rs index 68fd97d..ceae682 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,7 +1,7 @@ pub struct RepoInfo<'a> { pub commit_url: &'a str, pub commits: u64, - pub domain: &'a str, + pub base_url: &'a str, pub head: &'a str, pub hoc: u64, pub hoc_pretty: &'a str,