Implement improved overview page
This commit is contained in:
parent
361b446f2a
commit
c81fc5478b
66
src/main.rs
66
src/main.rs
@ -11,13 +11,14 @@ mod cache;
|
|||||||
mod error;
|
mod error;
|
||||||
mod service;
|
mod service;
|
||||||
|
|
||||||
use crate::{cache::CacheState, error::Error};
|
use crate::{
|
||||||
|
cache::CacheState,
|
||||||
|
error::Error,
|
||||||
|
service::{Bitbucket, GitHub, Gitlab, Service},
|
||||||
|
};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::ErrorBadRequest,
|
error::ErrorBadRequest,
|
||||||
http::{
|
http::header::{CacheControl, CacheDirective, Expires},
|
||||||
self,
|
|
||||||
header::{CacheControl, CacheDirective, Expires},
|
|
||||||
},
|
|
||||||
middleware, web, App, HttpResponse, HttpServer,
|
middleware, web, App, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
use badge::{Badge, BadgeOptions};
|
use badge::{Badge, BadgeOptions};
|
||||||
@ -203,31 +204,46 @@ fn calculate_hoc<T: Service>(
|
|||||||
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn github(
|
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> {
|
||||||
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(
|
let (tx, rx_body) = mpsc::unbounded();
|
||||||
state: web::Data<Arc<State>>,
|
let _ = tx.unbounded_send(Bytes::from(buf));
|
||||||
data: web::Path<(String, String)>,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
calculate_hoc("gitlab.com", state, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bitbucket(
|
Ok(HttpResponse::Ok()
|
||||||
state: web::Data<Arc<State>>,
|
.content_type("text/html")
|
||||||
data: web::Path<(String, String)>,
|
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
calculate_hoc("bitbucket.org", state, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn overview(_: web::Path<(String, String)>) -> HttpResponse {
|
|
||||||
HttpResponse::TemporaryRedirect()
|
|
||||||
.header(http::header::LOCATION, "/")
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
Loading…
Reference in New Issue
Block a user