Refactor into smaller modules and implement new logger

This commit is contained in:
Valentin Brandl 2019-05-14 01:12:13 +02:00
parent 60cba9951f
commit 2236cf8b53
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -8,13 +8,16 @@ extern crate log;
extern crate serde_derive; extern crate serde_derive;
mod cache; mod cache;
mod config;
mod error; mod error;
mod service; mod service;
mod statics;
use crate::{ use crate::{
cache::CacheState, cache::CacheState,
error::{Error, Result}, error::{Error, Result},
service::{Bitbucket, FormService, GitHub, Gitlab, Service}, service::{Bitbucket, FormService, GitHub, Gitlab, Service},
statics::{CLIENT, CSS, FAVICON, INDEX, OPT, P404, P500, VERSION_INFO},
}; };
use actix_web::{ use actix_web::{
error::ErrorBadRequest, error::ErrorBadRequest,
@ -29,20 +32,14 @@ use number_prefix::{NumberPrefix, Prefixed, Standalone};
use std::{ use std::{
borrow::Cow, borrow::Cow,
fs::create_dir_all, fs::create_dir_all,
path::{Path, PathBuf}, path::Path,
process::Command, process::Command,
sync::Arc, sync::Arc,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use structopt::StructOpt;
include!(concat!(env!("OUT_DIR"), "/templates.rs")); include!(concat!(env!("OUT_DIR"), "/templates.rs"));
pub struct VersionInfo<'a> {
pub commit: &'a str,
pub version: &'a str,
}
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct GeneratorForm<'a> { struct GeneratorForm<'a> {
service: FormService, service: FormService,
@ -50,71 +47,11 @@ struct GeneratorForm<'a> {
repo: Cow<'a, str>, repo: Cow<'a, str>,
} }
const VERSION_INFO: VersionInfo = VersionInfo {
commit: env!("VERGEN_SHA_SHORT"),
version: env!("CARGO_PKG_VERSION"),
};
lazy_static! {
static ref CLIENT: reqwest::Client = reqwest::Client::new();
static ref OPT: Opt = Opt::from_args();
static ref INDEX: Vec<u8> = {
let mut buf = Vec::new();
templates::index(&mut buf, VERSION_INFO, &OPT.domain).unwrap();
buf
};
static ref P404: Vec<u8> = {
let mut buf = Vec::new();
templates::p404(&mut buf, VERSION_INFO).unwrap();
buf
};
static ref P500: Vec<u8> = {
let mut buf = Vec::new();
templates::p500(&mut buf, VERSION_INFO).unwrap();
buf
};
}
struct State { struct State {
repos: String, repos: String,
cache: String, cache: String,
} }
const CSS: &str = include_str!("../static/tacit-css.min.css");
const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(
short = "o",
long = "outdir",
parse(from_os_str),
default_value = "./repos"
)]
/// Path to store cloned repositories
outdir: PathBuf,
#[structopt(
short = "c",
long = "cachedir",
parse(from_os_str),
default_value = "./cache"
)]
/// Path to store cache
cachedir: PathBuf,
#[structopt(short = "p", long = "port", default_value = "8080")]
/// Port to listen on
port: u16,
#[structopt(short = "h", long = "host", default_value = "0.0.0.0")]
/// Interface to listen on
host: String,
#[structopt(short = "d", long = "domain", default_value = "hitsofcode.com")]
/// Interface to listen on
domain: String,
#[structopt(short = "w", long = "workers", default_value = "4")]
/// Number of worker threads
workers: usize,
}
fn pull(path: impl AsRef<Path>) -> Result<()> { fn pull(path: impl AsRef<Path>) -> Result<()> {
let repo = Repository::open_bare(path)?; let repo = Repository::open_bare(path)?;
let mut origin = repo.find_remote("origin")?; let mut origin = repo.find_remote("origin")?;
@ -362,16 +299,14 @@ fn favicon32() -> HttpResponse {
HttpResponse::Ok().content_type("image/png").body(FAVICON) HttpResponse::Ok().content_type("image/png").body(FAVICON)
} }
fn main() -> std::io::Result<()> { fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info,hoc=info"); config::init()?;
pretty_env_logger::init();
openssl_probe::init_ssl_cert_env_vars();
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.outdir.display().to_string(),
cache: OPT.cachedir.display().to_string(), cache: OPT.cachedir.display().to_string(),
}); });
HttpServer::new(move || { Ok(HttpServer::new(move || {
App::new() App::new()
.data(state.clone()) .data(state.clone())
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
@ -389,5 +324,5 @@ fn main() -> std::io::Result<()> {
}) })
.workers(OPT.workers) .workers(OPT.workers)
.bind(interface)? .bind(interface)?
.run() .run()?)
} }