diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..e0a848c --- /dev/null +++ b/src/config.rs @@ -0,0 +1,71 @@ +use crate::{error::Result, statics::OPT}; +use log::LevelFilter; +use log4rs::{ + append::{console::ConsoleAppender, file::FileAppender}, + config::{Appender, Config, Root}, + encode::pattern::PatternEncoder, +}; +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" + )] + /// Path to store cloned repositories + pub(crate) outdir: PathBuf, + #[structopt( + short = "c", + long = "cachedir", + parse(from_os_str), + default_value = "./cache" + )] + /// Path to store cache + pub(crate) cachedir: PathBuf, + #[structopt(short = "p", long = "port", default_value = "8080")] + /// Port to listen on + pub(crate) port: u16, + #[structopt(short = "h", long = "host", default_value = "0.0.0.0")] + /// 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")] + /// Number of worker threads + pub(crate) workers: usize, + #[structopt( + short = "l", + long = "logfile", + parse(from_os_str), + default_value = "./hoc.log" + )] + /// The logfile + pub(crate) logfile: PathBuf, +} + +pub(crate) fn init() -> Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info,hoc=info"); + // pretty_env_logger::init(); + openssl_probe::init_ssl_cert_env_vars(); + let stdout = ConsoleAppender::builder().build(); + let file = FileAppender::builder() + .encoder(Box::new(PatternEncoder::new("{d} - {m}{n}"))) + .build(&OPT.logfile) + .unwrap(); + let config = Config::builder() + .appender(Appender::builder().build("stdout", Box::new(stdout))) + .appender(Appender::builder().build("file", Box::new(file))) + .build( + Root::builder() + .appender("stdout") + .appender("file") + .build(LevelFilter::Info), + )?; + log4rs::init_config(config)?; + Ok(()) +} diff --git a/src/statics.rs b/src/statics.rs new file mode 100644 index 0000000..fdf248a --- /dev/null +++ b/src/statics.rs @@ -0,0 +1,34 @@ +use crate::{config::Opt, templates}; +use structopt::StructOpt; + +pub struct VersionInfo<'a> { + pub commit: &'a str, + pub version: &'a str, +} + +pub(crate) const VERSION_INFO: VersionInfo = VersionInfo { + commit: env!("VERGEN_SHA_SHORT"), + version: env!("CARGO_PKG_VERSION"), +}; +pub(crate) const CSS: &str = include_str!("../static/tacit-css.min.css"); +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 INDEX: Vec = { + let mut buf = Vec::new(); + templates::index(&mut buf, VERSION_INFO, &OPT.domain).unwrap(); + buf + }; + pub(crate) static ref P404: Vec = { + let mut buf = Vec::new(); + templates::p404(&mut buf, VERSION_INFO).unwrap(); + buf + }; + pub(crate) static ref P500: Vec = { + let mut buf = Vec::new(); + templates::p500(&mut buf, VERSION_INFO).unwrap(); + buf + }; +} diff --git a/templates/base.rs.html b/templates/base.rs.html index 567cc7e..0b3eb56 100644 --- a/templates/base.rs.html +++ b/templates/base.rs.html @@ -1,4 +1,4 @@ -@use crate::VersionInfo; +@use crate::statics::VersionInfo; @(title: &str, header: &str, content: Content, version_info: VersionInfo) diff --git a/templates/generate.rs.html b/templates/generate.rs.html index 86b2c03..ea14d8a 100644 --- a/templates/generate.rs.html +++ b/templates/generate.rs.html @@ -1,5 +1,5 @@ @use super::base; -@use crate::VersionInfo; +@use crate::statics::VersionInfo; @(version_info: VersionInfo, domain: &str, url: &str, service: &str, path: &str) diff --git a/templates/index.rs.html b/templates/index.rs.html index adbcd91..85f2184 100644 --- a/templates/index.rs.html +++ b/templates/index.rs.html @@ -1,5 +1,5 @@ @use super::base; -@use crate::VersionInfo; +@use crate::statics::VersionInfo; @(version_info: VersionInfo, domain: &str) diff --git a/templates/overview.rs.html b/templates/overview.rs.html index a9cdd3c..8e32283 100644 --- a/templates/overview.rs.html +++ b/templates/overview.rs.html @@ -1,5 +1,5 @@ @use super::base; -@use crate::VersionInfo; +@use crate::statics::VersionInfo; @(version_info: VersionInfo, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str) diff --git a/templates/p404.rs.html b/templates/p404.rs.html index 8189f6f..6586b6c 100644 --- a/templates/p404.rs.html +++ b/templates/p404.rs.html @@ -1,5 +1,5 @@ @use super::base; -@use crate::VersionInfo; +@use crate::statics::VersionInfo; @(version_info: VersionInfo) diff --git a/templates/p500.rs.html b/templates/p500.rs.html index 4798fca..2852ff8 100644 --- a/templates/p500.rs.html +++ b/templates/p500.rs.html @@ -1,5 +1,5 @@ @use super::base; -@use crate::VersionInfo; +@use crate::statics::VersionInfo; @(version_info: VersionInfo)