diff --git a/backend/src/cdn.rs b/backend/src/cdn.rs index 0c53dc8..c7052db 100644 --- a/backend/src/cdn.rs +++ b/backend/src/cdn.rs @@ -13,7 +13,7 @@ impl Cloudflare { &CF_ZONE_IDENT } - pub(crate) fn dbg( + pub(crate) fn purge_cache( client: &Client, file: &str, ) -> impl Future { @@ -31,29 +31,23 @@ impl Cloudflare { .send_json(&payload) .from_err() .and_then(|response| HttpResponse::build(response.status()).streaming(response)) - } - - pub(crate) fn purge_cache( - client: &Client, - file: &str, - ) -> impl Future { - client - .post(format!( - "https://api.cloudflare.com/client/v4/zones/{}/purge_cache", - Self::identifier() - )) - .header(header::USER_AGENT, statics::USER_AGENT.as_str()) - .header("X-Auth-Email", Self::auth_email()) - .header("X-Auth-Key", Self::auth_key()) - .content_type("application/json") - .send_json(&CfPurgeRequest::singleton::(file)) - .from_err() - .and_then(|mut response| { - response - .json::() - .map(|resp| resp.success) - .from_err() - }) + // client + // .post(format!( + // "https://api.cloudflare.com/client/v4/zones/{}/purge_cache", + // Self::identifier() + // )) + // .header(header::USER_AGENT, statics::USER_AGENT.as_str()) + // .header("X-Auth-Email", Self::auth_email()) + // .header("X-Auth-Key", Self::auth_key()) + // .content_type("application/json") + // .send_json(&CfPurgeRequest::singleton::(file)) + // .from_err() + // .and_then(|mut response| { + // response + // .json::() + // .map(|resp| resp.success) + // .from_err() + // }) } fn auth_key() -> &'static str { @@ -83,7 +77,7 @@ impl CfPurgeRequest { } } -#[derive(Deserialize)] -struct CfPurgeResponse { - success: bool, -} +// #[derive(Deserialize)] +// struct CfPurgeResponse { +// success: bool, +// } diff --git a/backend/src/main.rs b/backend/src/main.rs index 85a7c15..8763059 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -35,32 +35,30 @@ use time_cache::{Cache, CacheResult}; fn proxy_file( client: web::Data, data: web::Path, -) -> Box> { - Box::new( - client - .get(&T::raw_url( - &data.user, - &data.repo, - &data.commit, - &data.file, - )) - .header(header::USER_AGENT, statics::USER_AGENT.as_str()) - .send() - .from_err() - .and_then(move |response| match response.status() { - StatusCode::OK => { - let mime = mime_guess::guess_mime_type(&*data.file); - Ok(HttpResponse::Ok() - .content_type(mime.to_string().as_str()) - .set(CacheControl(vec![ - CacheDirective::Public, - CacheDirective::MaxAge(2_592_000_000), - ])) - .streaming(response)) - } - code => Ok(HttpResponse::build(code).finish()), - }), - ) +) -> impl Future { + client + .get(&T::raw_url( + &data.user, + &data.repo, + &data.commit, + &data.file, + )) + .header(header::USER_AGENT, statics::USER_AGENT.as_str()) + .send() + .from_err() + .and_then(move |response| match response.status() { + StatusCode::OK => { + let mime = mime_guess::guess_mime_type(&*data.file); + Ok(HttpResponse::Ok() + .content_type(mime.to_string().as_str()) + .set(CacheControl(vec![ + CacheDirective::Public, + CacheDirective::MaxAge(2_592_000_000), + ])) + .streaming(response)) + } + code => Ok(HttpResponse::build(code).finish()), + }) } fn redirect( @@ -109,18 +107,6 @@ fn redirect( ) } -fn handle_request( - client: web::Data, - cache: web::Data, - data: web::Path, -) -> Box> { - if data.commit.len() == 40 { - proxy_file::(client, data) - } else { - redirect::(client, cache, data) - } -} - fn serve_gist( client: web::Data, data: web::Path, @@ -160,49 +146,28 @@ fn favicon32() -> HttpResponse { .body(FAVICON) } -fn purge_cache( - client: web::Data, +fn purge_local_cache( cache: web::Data, data: web::Path, -) -> Box> { - if data.commit.len() == 40 { - Box::new( - Cloudflare::purge_cache::(&client, &data.path()) - .map(|success| HttpResponse::Ok().body(success.to_string())), - ) - } else { - let cache = cache.clone(); - Box::new(futures::future::ok(()).map(move |_| { - if let Ok(mut cache) = cache.write() { - let key = data.to_key::(); - cache.invalidate(&key); - HttpResponse::Ok().finish() - } else { - HttpResponse::InternalServerError().finish() - } - })) - } +) -> impl Future { + let cache = cache.clone(); + futures::future::ok(()).map(move |_| { + if let Ok(mut cache) = cache.write() { + let key = data.to_key::(); + cache.invalidate(&key); + HttpResponse::Ok().finish() + } else { + HttpResponse::InternalServerError().finish() + } + }) } -fn dbg( +fn purge_cf_cache( client: web::Data, - cache: web::Data, data: web::Path, -) -> Box> { - if data.commit.len() == 40 { - Box::new(Cloudflare::dbg::(&client, &data.path())) - } else { - let cache = cache.clone(); - Box::new(futures::future::ok(()).map(move |_| { - if let Ok(mut cache) = cache.write() { - let key = data.to_key::(); - cache.invalidate(&key); - HttpResponse::Ok().finish() - } else { - HttpResponse::InternalServerError().finish() - } - })) - } +) -> impl Future { + Cloudflare::purge_cache::(&client, &data.path()) + // .map(|success| HttpResponse::Ok().body(success.to_string())) } fn main() -> Result<()> { @@ -219,33 +184,57 @@ fn main() -> Result<()> { .wrap(middleware::NormalizePath) .service(favicon32) .route( - "/github/{user}/{repo}/{commit}/{file:.*}", - web::get().to_async(handle_request::), + "/github/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ) .route( "/github/{user}/{repo}/{commit}/{file:.*}", - web::delete().to_async(dbg::), + web::get().to_async(redirect::), + ) + .route( + "/github/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::delete().to_async(purge_cf_cache::), + ) + .route( + "/github/{user}/{repo}/{commit}/{file:.*}", + web::delete().to_async(purge_local_cache::), + ) + .route( + "/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ) .route( "/bitbucket/{user}/{repo}/{commit}/{file:.*}", - web::get().to_async(handle_request::), + web::get().to_async(redirect::), + ) + .route( + "/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::delete().to_async(purge_cf_cache::), ) .route( "/bitbucket/{user}/{repo}/{commit}/{file:.*}", - web::delete().to_async(dbg::), + web::delete().to_async(purge_local_cache::), + ) + .route( + "/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ) .route( "/gitlab/{user}/{repo}/{commit}/{file:.*}", - web::get().to_async(handle_request::), + web::get().to_async(redirect::), + ) + .route( + "/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::delete().to_async(purge_cf_cache::), + ) + .route( + "/gitlab/{user}/{repo}/{commit}/{file:.*}", + web::delete().to_async(purge_cf_cache::), ) .route( "/gist/{user}/{repo}/{commit}/{file:.*}", web::get().to_async(serve_gist), ) - .route( - "/gitlab/{user}/{repo}/{commit}/{file:.*}", - web::delete().to_async(dbg::), - ) .service(actix_files::Files::new("/", "./public").index_file("index.html")) }) .workers(OPT.workers)