diff --git a/backend/src/cdn.rs b/backend/src/cdn.rs index 01f2d68..68a1552 100644 --- a/backend/src/cdn.rs +++ b/backend/src/cdn.rs @@ -1,4 +1,7 @@ -use crate::statics::{self, CF_ZONE_IDENT}; +use crate::{ + service::Service, + statics::{self, CF_ZONE_IDENT}, +}; use actix_web::{http::header, Error}; use awc::Client; use futures::Future; @@ -10,7 +13,7 @@ impl Cloudflare { &CF_ZONE_IDENT } - pub(crate) fn purge_cache( + pub(crate) fn purge_cache( client: &Client, file: &str, ) -> impl Future { @@ -23,7 +26,7 @@ impl Cloudflare { .header("X-Auth-Email", Self::auth_email()) .header("X-Auth-Key", Self::auth_key()) .content_type("application/json") - .send_json(&CfPurgeRequest::singleton(file)) + .send_json(&CfPurgeRequest::singleton::(file)) .from_err() .and_then(|mut response| { response @@ -48,8 +51,13 @@ struct CfPurgeRequest { } impl CfPurgeRequest { - fn singleton(file: &str) -> Self { - let url = format!("https://{}/{}", statics::HOSTNAME.as_str(), file); + fn singleton(file: &str) -> Self { + let url = format!( + "https://{}/{}/{}", + statics::HOSTNAME.as_str(), + T::path(), + file + ); Self { files: vec![url] } } } diff --git a/backend/src/main.rs b/backend/src/main.rs index 0fe6081..25d67bd 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -96,11 +96,11 @@ fn favicon32() -> HttpResponse { .body(FAVICON) } -fn purge_cache( +fn purge_cache( client: web::Data, - data: web::Path, + file: web::Path, ) -> impl Future { - Cloudflare::purge_cache(&client, &data) + Cloudflare::purge_cache::(&client, &file) .map(|success| HttpResponse::Ok().body(success.to_string())) } @@ -118,15 +118,26 @@ fn main() -> Result<()> { "/github/{user}/{repo}/{commit}/{file:.*}", web::get().to_async(handle_request::), ) + .route( + "/github/{file:.*}", + web::delete().to_async(purge_cache::), + ) .route( "/bitbucket/{user}/{repo}/{commit}/{file:.*}", web::get().to_async(handle_request::), ) + .route( + "/bitbucket//{file:.*}", + web::delete().to_async(purge_cache::), + ) .route( "/gitlab/{user}/{repo}/{commit}/{file:.*}", web::get().to_async(handle_request::), ) - .route("/purge/{path:.*}", web::delete().to_async(purge_cache)) + .route( + "/gitlab/{file:.*}", + web::delete().to_async(purge_cache::), + ) .service(actix_files::Files::new("/", "public").index_file("index.html")) }) .workers(OPT.workers) diff --git a/backend/src/service.rs b/backend/src/service.rs index f9cd62f..af1851e 100644 --- a/backend/src/service.rs +++ b/backend/src/service.rs @@ -69,6 +69,8 @@ pub(crate) trait Service { fn api_url(path: &FilePath) -> String; + fn path() -> &'static str; + fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String; fn request_head( @@ -124,6 +126,10 @@ impl Github { impl Service for Github { type Response = GitHubApiResponse; + fn path() -> &'static str { + "github" + } + fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String { format!( "https://raw.githubusercontent.com/{}/{}/{}/{}", @@ -151,6 +157,10 @@ pub(crate) struct Bitbucket; impl Service for Bitbucket { type Response = BitbucketApiResponse; + fn path() -> &'static str { + "bitbucket" + } + fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String { format!( "https://bitbucket.org/{}/{}/raw/{}/{}", @@ -175,6 +185,10 @@ pub(crate) struct GitLab; impl Service for GitLab { type Response = GitLabApiResponse; + fn path() -> &'static str { + "gitlab" + } + fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String { format!( "https://gitlab.com/{}/{}/raw/{}/{}",