Use new config

This commit is contained in:
Valentin Brandl 2020-11-24 19:06:49 +01:00
parent 8e78d13443
commit 9e33742d81
4 changed files with 34 additions and 35 deletions

View File

@ -1,41 +1,41 @@
use config::{Config, ConfigError, Environment, File};
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)] #[derive(Debug, Deserialize)]
pub(crate) struct Opt { pub struct Settings {
#[structopt(
short = "o",
long = "outdir",
parse(from_os_str),
default_value = "./repos"
)]
/// Path to store cloned repositories /// Path to store cloned repositories
pub(crate) outdir: PathBuf, pub repodir: PathBuf,
#[structopt(
short = "c",
long = "cachedir",
parse(from_os_str),
default_value = "./cache"
)]
/// Path to store cache /// Path to store cache
pub(crate) cachedir: PathBuf, pub cachedir: PathBuf,
#[structopt(short = "p", long = "port", default_value = "8080")]
/// Port to listen on /// Port to listen on
pub(crate) port: u16, pub port: u16,
#[structopt(short = "h", long = "host", default_value = "0.0.0.0")]
/// Interface to listen on /// Interface to listen on
pub(crate) host: String, pub host: String,
#[structopt(short = "d", long = "domain", default_value = "hitsofcode.com")] /// Base URL
/// Interface to listen on pub base_url: String,
pub(crate) domain: String,
#[structopt(short = "w", long = "workers", default_value = "4")]
/// Number of worker threads /// Number of worker threads
pub(crate) workers: usize, pub workers: usize,
} }
pub(crate) fn init() { pub(crate) fn init() {
dotenv::dotenv().ok();
std::env::set_var("RUST_LOG", "actix_web=info,hoc=info"); std::env::set_var("RUST_LOG", "actix_web=info,hoc=info");
openssl_probe::init_ssl_cert_env_vars(); openssl_probe::init_ssl_cert_env_vars();
tracing_subscriber::fmt().init(); tracing_subscriber::fmt().init();
} }
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
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()
}
}

View File

@ -371,7 +371,7 @@ async fn overview<T: Service>(
let repo_info = RepoInfo { let repo_info = RepoInfo {
commit_url: &T::commit_url(&repo, &head), commit_url: &T::commit_url(&repo, &head),
commits, commits,
domain: &OPT.domain, base_url: &OPT.base_url,
head: &head, head: &head,
hoc, hoc,
hoc_pretty: &hoc_pretty, hoc_pretty: &hoc_pretty,
@ -399,7 +399,7 @@ async fn index() -> Result<HttpResponse> {
&mut buf, &mut buf,
VERSION_INFO, VERSION_INFO,
REPO_COUNT.load(Ordering::Relaxed), REPO_COUNT.load(Ordering::Relaxed),
&OPT.domain, &OPT.base_url,
)?; )?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf)) Ok(HttpResponse::Ok().content_type("text/html").body(buf))
} }
@ -412,7 +412,7 @@ async fn generate(params: web::Form<GeneratorForm<'_>>) -> Result<HttpResponse>
&mut buf, &mut buf,
VERSION_INFO, VERSION_INFO,
REPO_COUNT.load(Ordering::Relaxed), REPO_COUNT.load(Ordering::Relaxed),
&OPT.domain, &OPT.base_url,
params.service.url(), params.service.url(),
params.service.service(), params.service.service(),
&repo, &repo,
@ -442,7 +442,7 @@ fn favicon32() -> HttpResponse {
async fn start_server() -> std::io::Result<()> { async fn start_server() -> std::io::Result<()> {
let interface = format!("{}:{}", OPT.host, OPT.port); let interface = format!("{}:{}", OPT.host, OPT.port);
let state = Arc::new(State { let state = Arc::new(State {
repos: OPT.outdir.display().to_string(), repos: OPT.repodir.display().to_string(),
cache: OPT.cachedir.display().to_string(), cache: OPT.cachedir.display().to_string(),
}); });
HttpServer::new(move || { HttpServer::new(move || {

View File

@ -1,6 +1,5 @@
use crate::{config::Opt, count::count_repositories}; use crate::{config::Settings, count::count_repositories};
use std::sync::atomic::AtomicUsize; use std::sync::atomic::AtomicUsize;
use structopt::StructOpt;
pub struct VersionInfo<'a> { pub struct VersionInfo<'a> {
pub commit: &'a str, pub commit: &'a str,
@ -16,7 +15,7 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
lazy_static! { lazy_static! {
pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new(); 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 = pub(crate) static ref REPO_COUNT: AtomicUsize =
AtomicUsize::new(count_repositories(&OPT.outdir).unwrap()); AtomicUsize::new(count_repositories(&OPT.repodir).unwrap());
} }

View File

@ -1,7 +1,7 @@
pub struct RepoInfo<'a> { pub struct RepoInfo<'a> {
pub commit_url: &'a str, pub commit_url: &'a str,
pub commits: u64, pub commits: u64,
pub domain: &'a str, pub base_url: &'a str,
pub head: &'a str, pub head: &'a str,
pub hoc: u64, pub hoc: u64,
pub hoc_pretty: &'a str, pub hoc_pretty: &'a str,