Make purging more ergonomic

This commit is contained in:
Valentin Brandl 2019-07-28 16:10:04 +02:00
parent c5da9c5812
commit fd5b802d25
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 42 additions and 9 deletions

View File

@ -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<T: Service>(
client: &Client,
file: &str,
) -> impl Future<Item = bool, Error = Error> {
@ -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::<T>(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<T: Service>(file: &str) -> Self {
let url = format!(
"https://{}/{}/{}",
statics::HOSTNAME.as_str(),
T::path(),
file
);
Self { files: vec![url] }
}
}

View File

@ -96,11 +96,11 @@ fn favicon32() -> HttpResponse {
.body(FAVICON)
}
fn purge_cache(
fn purge_cache<T: Service>(
client: web::Data<Client>,
data: web::Path<String>,
file: web::Path<String>,
) -> impl Future<Item = HttpResponse, Error = Error> {
Cloudflare::purge_cache(&client, &data)
Cloudflare::purge_cache::<T>(&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::<Github>),
)
.route(
"/github/{file:.*}",
web::delete().to_async(purge_cache::<Github>),
)
.route(
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(handle_request::<Bitbucket>),
)
.route(
"/bitbucket//{file:.*}",
web::delete().to_async(purge_cache::<Bitbucket>),
)
.route(
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(handle_request::<GitLab>),
)
.route("/purge/{path:.*}", web::delete().to_async(purge_cache))
.route(
"/gitlab/{file:.*}",
web::delete().to_async(purge_cache::<GitLab>),
)
.service(actix_files::Files::new("/", "public").index_file("index.html"))
})
.workers(OPT.workers)

View File

@ -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<S>(
@ -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/{}/{}",