Refactor into smaller modules

This commit is contained in:
Valentin Brandl 2019-05-14 01:11:39 +02:00
parent 6d980d92be
commit d6835b96de
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
8 changed files with 111 additions and 6 deletions

71
src/config.rs Normal file
View File

@ -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(())
}

34
src/statics.rs Normal file
View File

@ -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<u8> = {
let mut buf = Vec::new();
templates::index(&mut buf, VERSION_INFO, &OPT.domain).unwrap();
buf
};
pub(crate) static ref P404: Vec<u8> = {
let mut buf = Vec::new();
templates::p404(&mut buf, VERSION_INFO).unwrap();
buf
};
pub(crate) static ref P500: Vec<u8> = {
let mut buf = Vec::new();
templates::p500(&mut buf, VERSION_INFO).unwrap();
buf
};
}

View File

@ -1,4 +1,4 @@
@use crate::VersionInfo;
@use crate::statics::VersionInfo;
@(title: &str, header: &str, content: Content, version_info: VersionInfo)

View File

@ -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)

View File

@ -1,5 +1,5 @@
@use super::base;
@use crate::VersionInfo;
@use crate::statics::VersionInfo;
@(version_info: VersionInfo, domain: &str)

View File

@ -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)

View File

@ -1,5 +1,5 @@
@use super::base;
@use crate::VersionInfo;
@use crate::statics::VersionInfo;
@(version_info: VersionInfo)

View File

@ -1,5 +1,5 @@
@use super::base;
@use crate::VersionInfo;
@use crate::statics::VersionInfo;
@(version_info: VersionInfo)