From c81fc5478baf62d82a790d23ac1e1cbf6b88bfdf Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Mon, 29 Apr 2019 20:42:17 +0200 Subject: [PATCH] Implement improved overview page --- src/main.rs | 66 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 25 deletions(-) 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("/")]