diff --git a/src/main.rs b/src/main.rs index 85c7411..cd138ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,13 +11,14 @@ mod cache; mod error; mod service; -use crate::{cache::CacheState, error::Error}; +use crate::{ + cache::CacheState, + error::Error, + service::{Bitbucket, GitHub, Gitlab, Service}, +}; use actix_web::{ error::ErrorBadRequest, - http::{ - self, - header::{CacheControl, CacheDirective, Expires}, - }, + http::header::{CacheControl, CacheDirective, Expires}, middleware, web, App, HttpResponse, HttpServer, }; use badge::{Badge, BadgeOptions}; @@ -203,31 +204,46 @@ fn calculate_hoc( .streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))) } -fn github( +fn overview( state: web::Data>, data: web::Path<(String, String)>, ) -> Result { - calculate_hoc("github.com", state, data) -} + 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 mut buf = Vec::new(); + let req_path = format!("{}/{}/{}", T::url_path(), data.0, data.1); + templates::overview( + &mut buf, + COMMIT, + VERSION, + &OPT.domain, + &req_path, + &url, + hoc, + &head, + &T::commit_url(&repo, &head), + )?; -fn gitlab( - state: web::Data>, - data: web::Path<(String, String)>, -) -> Result { - calculate_hoc("gitlab.com", state, data) -} + let (tx, rx_body) = mpsc::unbounded(); + let _ = tx.unbounded_send(Bytes::from(buf)); -fn bitbucket( - state: web::Data>, - data: web::Path<(String, String)>, -) -> Result { - calculate_hoc("bitbucket.org", state, data) -} - -fn overview(_: web::Path<(String, String)>) -> HttpResponse { - HttpResponse::TemporaryRedirect() - .header(http::header::LOCATION, "/") - .finish() + Ok(HttpResponse::Ok() + .content_type("text/html") + .streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))) } #[get("/")]