Merge branch 'feature/logging'
This commit is contained in:
commit
e7e0c75e6f
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -807,6 +807,7 @@ dependencies = [
|
||||
"futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"number_prefix 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pretty_env_logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -12,6 +12,7 @@ bytes = "0.4.12"
|
||||
futures = "0.1.25"
|
||||
git2 = "0.8.0"
|
||||
lazy_static = "1.3.0"
|
||||
log = "0.4.6"
|
||||
number_prefix = "0.3.0"
|
||||
openssl-probe = "0.1.2"
|
||||
pretty_env_logger = "0.3.0"
|
||||
|
113
src/main.rs
113
src/main.rs
@ -3,6 +3,8 @@ extern crate actix_web;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
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)?;
|
||||
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) => {
|
||||
info!("Updating cache for {}", repo_dir);
|
||||
arg.push(format!("{}..HEAD", cache.head));
|
||||
}
|
||||
CacheState::No => {}
|
||||
CacheState::No => {
|
||||
info!("Creating cache for {}", repo_dir);
|
||||
}
|
||||
};
|
||||
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)
|
||||
}
|
||||
|
||||
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>>,
|
||||
data: web::Path<(String, String)>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let service_path = format!("{}/{}/{}", T::domain(), data.0, data.1);
|
||||
mapper: F,
|
||||
) -> 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 file = Path::new(&path);
|
||||
if !file.exists() {
|
||||
let url = format!("https://{}", service_path);
|
||||
if !file.exists() {
|
||||
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)?;
|
||||
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, _) = hoc(&service_path, &state.repos, &state.cache)?;
|
||||
let hoc = match NumberPrefix::decimal(hoc as f64) {
|
||||
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),
|
||||
};
|
||||
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 {
|
||||
subject: "Hits-of-Code".to_string(),
|
||||
color: "#007ec6".to_string(),
|
||||
status: hoc,
|
||||
status: hoc_pretty,
|
||||
};
|
||||
let badge = Badge::new(badge_opt)?;
|
||||
|
||||
@ -212,39 +264,31 @@ fn calculate_hoc<T: Service>(
|
||||
CacheDirective::NoStore,
|
||||
]))
|
||||
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
||||
}
|
||||
};
|
||||
handle_hoc_request::<T, _>(state, data, mapper)
|
||||
}
|
||||
|
||||
fn overview<T: Service>(
|
||||
state: web::Data<Arc<State>>,
|
||||
data: web::Path<(String, String)>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let repo = format!("{}/{}", data.0, data.1);
|
||||
let service_path = format!("{}/{}", T::domain(), repo);
|
||||
let path = format!("{}/{}", state.repos, service_path);
|
||||
let file = Path::new(&path);
|
||||
let url = format!("https://{}", service_path);
|
||||
if !file.exists() {
|
||||
if !remote_exists(&url)? {
|
||||
return Ok(p404());
|
||||
}
|
||||
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 mapper = |r| match r {
|
||||
HocResult::NotFound => Ok(p404()),
|
||||
HocResult::Hoc {
|
||||
hoc,
|
||||
hoc_pretty,
|
||||
url,
|
||||
head,
|
||||
repo,
|
||||
service_path,
|
||||
} => {
|
||||
let mut buf = Vec::new();
|
||||
let req_path = format!("{}/{}/{}", T::url_path(), data.0, data.1);
|
||||
templates::overview(
|
||||
&mut buf,
|
||||
VERSION_INFO,
|
||||
&OPT.domain,
|
||||
&req_path,
|
||||
&service_path,
|
||||
&url,
|
||||
hoc,
|
||||
&hoc_pretty,
|
||||
@ -258,6 +302,9 @@ fn overview<T: Service>(
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
||||
}
|
||||
};
|
||||
handle_hoc_request::<T, _>(state, data, mapper)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
@ -279,7 +326,7 @@ fn css() -> HttpResponse {
|
||||
}
|
||||
|
||||
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();
|
||||
openssl_probe::init_ssl_cert_env_vars();
|
||||
let interface = format!("{}:{}", OPT.host, OPT.port);
|
||||
|
Loading…
Reference in New Issue
Block a user