From 2db66ee850d27cc5acf044debf15a9128deca574 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 21 Apr 2019 20:45:36 +0200 Subject: [PATCH] Check if repos exist before cloning --- src/main.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 38b0b70..0c534bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ #[macro_use] extern crate actix_web; +#[macro_use] +extern crate lazy_static; +extern crate reqwest; extern crate serde_json; #[macro_use] extern crate serde_derive; @@ -29,6 +32,10 @@ use std::{ }; use structopt::StructOpt; +lazy_static! { + static ref CLIENT: reqwest::Client = reqwest::Client::new(); +} + struct State { repos: String, cache: String, @@ -128,6 +135,10 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result { Ok(cache.count) } +fn remote_exists(url: &str) -> Result { + Ok(CLIENT.head(url).send()?.status() == reqwest::StatusCode::OK) +} + fn calculate_hoc( service: &str, state: web::Data>, @@ -137,10 +148,14 @@ fn calculate_hoc( let path = format!("{}/{}", state.repos, service_path); let file = Path::new(&path); if !file.exists() { + let url = format!("https://{}", service_path); + 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", &format!("https://{}", service_path))?; + repo.remote_set_url("origin", &url)?; } pull(&path)?; let hoc = hoc(&service_path, &state.repos, &state.cache)?; @@ -211,7 +226,7 @@ fn css() -> HttpResponse { } fn main() -> std::io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info"); + std::env::set_var("RUST_LOG", "actix_web=warn"); pretty_env_logger::init(); openssl_probe::init_ssl_cert_env_vars(); let opt = Opt::from_args();