Refactor to remove duplicate code and add some logs

This commit is contained in:
Valentin Brandl 2019-04-30 15:22:58 +02:00
parent bb52f9124f
commit f5a5560a62
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -3,6 +3,8 @@ extern crate actix_web;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive; extern crate serde_derive;
mod cache; mod cache;
@ -133,11 +135,17 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String), Err
]; ];
let cache = CacheState::read_from_file(&cache_dir, &head)?; let cache = CacheState::read_from_file(&cache_dir, &head)?;
match &cache { match &cache {
CacheState::Current(res) => return Ok((*res, head)), CacheState::Current(res) => {
info!("Using cache for {}", repo_dir);
return Ok((*res, head));
}
CacheState::Old(cache) => { CacheState::Old(cache) => {
info!("Updating cache for {}", repo_dir);
arg.push(format!("{}..HEAD", cache.head)); arg.push(format!("{}..HEAD", cache.head));
} }
CacheState::No => {} CacheState::No => {
info!("Creating cache for {}", repo_dir);
}
}; };
arg.push("--".to_string()); arg.push("--".to_string());
arg.push(".".to_string()); arg.push(".".to_string());
@ -168,33 +176,77 @@ fn remote_exists(url: &str) -> Result<bool, Error> {
Ok(CLIENT.head(url).send()?.status() == reqwest::StatusCode::OK) Ok(CLIENT.head(url).send()?.status() == reqwest::StatusCode::OK)
} }
fn calculate_hoc<T: Service>( enum HocResult {
Hoc {
hoc: u64,
hoc_pretty: String,
head: String,
url: String,
repo: String,
service_path: String,
},
NotFound,
}
fn handle_hoc_request<T, F>(
state: web::Data<Arc<State>>, state: web::Data<Arc<State>>,
data: web::Path<(String, String)>, data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> { mapper: F,
let service_path = format!("{}/{}/{}", T::domain(), data.0, data.1); ) -> Result<HttpResponse, Error>
where
T: Service,
F: Fn(HocResult) -> Result<HttpResponse, Error>,
{
hoc_request::<T>(state, data).and_then(mapper)
}
fn hoc_request<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> Result<HocResult, Error> {
let repo = format!("{}/{}", data.0, data.1);
let service_path = format!("{}/{}", T::domain(), repo);
let path = format!("{}/{}", state.repos, service_path); let path = format!("{}/{}", state.repos, service_path);
let file = Path::new(&path); let file = Path::new(&path);
if !file.exists() {
let url = format!("https://{}", service_path); let url = format!("https://{}", service_path);
if !file.exists() {
if !remote_exists(&url)? { if !remote_exists(&url)? {
return Ok(p404()); warn!("Repository does not exist: {}", url);
return Ok(HocResult::NotFound);
} }
info!("Cloning {} for the first time", url);
create_dir_all(file)?; create_dir_all(file)?;
let repo = Repository::init_bare(file)?; let repo = Repository::init_bare(file)?;
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?; repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
repo.remote_set_url("origin", &url)?; repo.remote_set_url("origin", &url)?;
} }
pull(&path)?; pull(&path)?;
let (hoc, _) = hoc(&service_path, &state.repos, &state.cache)?; let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?;
let hoc = match NumberPrefix::decimal(hoc as f64) { let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
Standalone(hoc) => hoc.to_string(), Standalone(hoc) => hoc.to_string(),
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix), Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
}; };
Ok(HocResult::Hoc {
hoc,
hoc_pretty,
head,
url,
repo,
service_path,
})
}
fn calculate_hoc<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> {
let mapper = |r| match r {
HocResult::NotFound => Ok(p404()),
HocResult::Hoc { hoc_pretty, .. } => {
let badge_opt = BadgeOptions { let badge_opt = BadgeOptions {
subject: "Hits-of-Code".to_string(), subject: "Hits-of-Code".to_string(),
color: "#007ec6".to_string(), color: "#007ec6".to_string(),
status: hoc, status: hoc_pretty,
}; };
let badge = Badge::new(badge_opt)?; let badge = Badge::new(badge_opt)?;
@ -213,38 +265,30 @@ fn calculate_hoc<T: Service>(
])) ]))
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))) .streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
} }
};
handle_hoc_request::<T, _>(state, data, mapper)
}
fn overview<T: Service>( fn overview<T: Service>(
state: web::Data<Arc<State>>, state: web::Data<Arc<State>>,
data: web::Path<(String, String)>, data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let repo = format!("{}/{}", data.0, data.1); let mapper = |r| match r {
let service_path = format!("{}/{}", T::domain(), repo); HocResult::NotFound => Ok(p404()),
let path = format!("{}/{}", state.repos, service_path); HocResult::Hoc {
let file = Path::new(&path); hoc,
let url = format!("https://{}", service_path); hoc_pretty,
if !file.exists() { url,
if !remote_exists(&url)? { head,
return Ok(p404()); repo,
} service_path,
create_dir_all(file)?; } => {
let repo = Repository::init_bare(file)?;
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
repo.remote_set_url("origin", &url)?;
}
pull(&path)?;
let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?;
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
Standalone(hoc) => hoc.to_string(),
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
};
let mut buf = Vec::new(); let mut buf = Vec::new();
let req_path = format!("{}/{}/{}", T::url_path(), data.0, data.1);
templates::overview( templates::overview(
&mut buf, &mut buf,
VERSION_INFO, VERSION_INFO,
&OPT.domain, &OPT.domain,
&req_path, &service_path,
&url, &url,
hoc, hoc,
&hoc_pretty, &hoc_pretty,
@ -259,6 +303,9 @@ fn overview<T: Service>(
.content_type("text/html") .content_type("text/html")
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))) .streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
} }
};
handle_hoc_request::<T, _>(state, data, mapper)
}
#[get("/")] #[get("/")]
fn index() -> HttpResponse { fn index() -> HttpResponse {
@ -279,7 +326,7 @@ fn css() -> HttpResponse {
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=warn"); std::env::set_var("RUST_LOG", "actix_web=info,hoc=info");
pretty_env_logger::init(); pretty_env_logger::init();
openssl_probe::init_ssl_cert_env_vars(); openssl_probe::init_ssl_cert_env_vars();
let interface = format!("{}:{}", OPT.host, OPT.port); let interface = format!("{}:{}", OPT.host, OPT.port);