From 909f6585b5b100f53722927727c1897b9fb2f426 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 1 Nov 2020 13:57:56 +0100 Subject: [PATCH] Always return a SVG badge --- src/main.rs | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10036a8..d3e3e51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -300,11 +300,25 @@ pub(crate) async fn json_hoc( handle_hoc_request::(state, data, branch, mapper).await } +fn no_cache_response(body: Vec) -> HttpResponse { + let expiration = SystemTime::now() + Duration::from_secs(30); + HttpResponse::Ok() + .content_type("image/svg+xml") + .set(Expires(expiration.into())) + .set(CacheControl(vec![ + CacheDirective::MaxAge(0u32), + CacheDirective::MustRevalidate, + CacheDirective::NoCache, + CacheDirective::NoStore, + ])) + .body(body) +} + pub(crate) async fn calculate_hoc( state: web::Data>, data: web::Path<(String, String)>, branch: web::Query, -) -> Result { +) -> HttpResponse { let mapper = move |r| match r { HocResult::NotFound => p404(), HocResult::Hoc { hoc_pretty, .. } => { @@ -317,21 +331,23 @@ pub(crate) async fn calculate_hoc( // TODO: remove clone let body = badge.to_svg().as_bytes().to_vec(); - let expiration = SystemTime::now() + Duration::from_secs(30); - Ok(HttpResponse::Ok() - .content_type("image/svg+xml") - .set(Expires(expiration.into())) - .set(CacheControl(vec![ - CacheDirective::MaxAge(0u32), - CacheDirective::MustRevalidate, - CacheDirective::NoCache, - CacheDirective::NoStore, - ])) - .body(body)) + Ok(no_cache_response(body)) } }; let branch = branch.branch.as_deref().unwrap_or("master"); - handle_hoc_request::(state, data, branch, mapper).await + let error_badge = |_| { + let error_badge = Badge::new(BadgeOptions { + subject: "Hits-of-Code".to_string(), + color: "#ff0000".to_string(), + status: "error".to_string(), + }) + .unwrap(); + let body = error_badge.to_svg().as_bytes().to_vec(); + no_cache_response(body) + }; + handle_hoc_request::(state, data, branch, mapper) + .await + .unwrap_or_else(error_badge) } async fn overview(