Merge branch 'feature/json-output'

This commit is contained in:
Valentin Brandl 2019-06-16 14:33:47 +02:00
commit d23588172b
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -17,7 +17,7 @@ mod service;
mod statics; mod statics;
use crate::{ use crate::{
cache::CacheState, cache::{Cache, CacheState},
error::{Error, Result}, error::{Error, Result},
service::{Bitbucket, FormService, GitHub, Gitlab, Service}, service::{Bitbucket, FormService, GitHub, Gitlab, Service},
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO}, statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
@ -150,14 +150,8 @@ where
T: Service, T: Service,
F: Fn(HocResult) -> Result<HttpResponse>, F: Fn(HocResult) -> Result<HttpResponse>,
{ {
hoc_request::<T>(state, data).and_then(mapper) futures::future::result(Ok(()))
} .and_then(move |_| {
fn hoc_request<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> impl Future<Item = HocResult, Error = Error> {
futures::future::result(Ok(())).and_then(move |_| {
let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase()); let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase());
let service_path = format!("{}/{}", T::domain(), repo); let service_path = format!("{}/{}", T::domain(), repo);
let path = format!("{}/{}", state.repos, service_path); let path = format!("{}/{}", state.repos, service_path);
@ -190,6 +184,21 @@ fn hoc_request<T: Service>(
service_path, service_path,
}) })
}) })
.and_then(mapper)
}
fn json_hoc<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> impl Future<Item = HttpResponse, Error = Error> {
let mapper = |r| match r {
HocResult::NotFound => p404(),
HocResult::Hoc { hoc, head, .. } => Ok(HttpResponse::Ok().json(Cache {
head: head.into(),
count: hoc,
})),
};
handle_hoc_request::<T, _>(state, data, mapper)
} }
fn calculate_hoc<T: Service>( fn calculate_hoc<T: Service>(
@ -331,6 +340,9 @@ fn main() -> Result<()> {
.service(web::resource("/github/{user}/{repo}").to_async(calculate_hoc::<GitHub>)) .service(web::resource("/github/{user}/{repo}").to_async(calculate_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}").to_async(calculate_hoc::<Gitlab>)) .service(web::resource("/gitlab/{user}/{repo}").to_async(calculate_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}").to_async(calculate_hoc::<Bitbucket>)) .service(web::resource("/bitbucket/{user}/{repo}").to_async(calculate_hoc::<Bitbucket>))
.service(web::resource("/github/{user}/{repo}/json").to_async(json_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}/json").to_async(json_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}/json").to_async(json_hoc::<Bitbucket>))
.service(web::resource("/view/github/{user}/{repo}").to_async(overview::<GitHub>)) .service(web::resource("/view/github/{user}/{repo}").to_async(overview::<GitHub>))
.service(web::resource("/view/gitlab/{user}/{repo}").to_async(overview::<Gitlab>)) .service(web::resource("/view/gitlab/{user}/{repo}").to_async(overview::<Gitlab>))
.service(web::resource("/view/bitbucket/{user}/{repo}").to_async(overview::<Bitbucket>)) .service(web::resource("/view/bitbucket/{user}/{repo}").to_async(overview::<Bitbucket>))