Make purging more ergonomic
This commit is contained in:
parent
c5da9c5812
commit
fd5b802d25
@ -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 actix_web::{http::header, Error};
|
||||||
use awc::Client;
|
use awc::Client;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
@ -10,7 +13,7 @@ impl Cloudflare {
|
|||||||
&CF_ZONE_IDENT
|
&CF_ZONE_IDENT
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn purge_cache(
|
pub(crate) fn purge_cache<T: Service>(
|
||||||
client: &Client,
|
client: &Client,
|
||||||
file: &str,
|
file: &str,
|
||||||
) -> impl Future<Item = bool, Error = Error> {
|
) -> impl Future<Item = bool, Error = Error> {
|
||||||
@ -23,7 +26,7 @@ impl Cloudflare {
|
|||||||
.header("X-Auth-Email", Self::auth_email())
|
.header("X-Auth-Email", Self::auth_email())
|
||||||
.header("X-Auth-Key", Self::auth_key())
|
.header("X-Auth-Key", Self::auth_key())
|
||||||
.content_type("application/json")
|
.content_type("application/json")
|
||||||
.send_json(&CfPurgeRequest::singleton(file))
|
.send_json(&CfPurgeRequest::singleton::<T>(file))
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
response
|
response
|
||||||
@ -48,8 +51,13 @@ struct CfPurgeRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CfPurgeRequest {
|
impl CfPurgeRequest {
|
||||||
fn singleton(file: &str) -> Self {
|
fn singleton<T: Service>(file: &str) -> Self {
|
||||||
let url = format!("https://{}/{}", statics::HOSTNAME.as_str(), file);
|
let url = format!(
|
||||||
|
"https://{}/{}/{}",
|
||||||
|
statics::HOSTNAME.as_str(),
|
||||||
|
T::path(),
|
||||||
|
file
|
||||||
|
);
|
||||||
Self { files: vec![url] }
|
Self { files: vec![url] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,11 +96,11 @@ fn favicon32() -> HttpResponse {
|
|||||||
.body(FAVICON)
|
.body(FAVICON)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn purge_cache(
|
fn purge_cache<T: Service>(
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
data: web::Path<String>,
|
file: web::Path<String>,
|
||||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
) -> 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()))
|
.map(|success| HttpResponse::Ok().body(success.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,15 +118,26 @@ fn main() -> Result<()> {
|
|||||||
"/github/{user}/{repo}/{commit}/{file:.*}",
|
"/github/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::get().to_async(handle_request::<Github>),
|
web::get().to_async(handle_request::<Github>),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/github/{file:.*}",
|
||||||
|
web::delete().to_async(purge_cache::<Github>),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
|
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::get().to_async(handle_request::<Bitbucket>),
|
web::get().to_async(handle_request::<Bitbucket>),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/bitbucket//{file:.*}",
|
||||||
|
web::delete().to_async(purge_cache::<Bitbucket>),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
|
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::get().to_async(handle_request::<GitLab>),
|
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"))
|
.service(actix_files::Files::new("/", "public").index_file("index.html"))
|
||||||
})
|
})
|
||||||
.workers(OPT.workers)
|
.workers(OPT.workers)
|
||||||
|
@ -69,6 +69,8 @@ pub(crate) trait Service {
|
|||||||
|
|
||||||
fn api_url(path: &FilePath) -> String;
|
fn api_url(path: &FilePath) -> String;
|
||||||
|
|
||||||
|
fn path() -> &'static str;
|
||||||
|
|
||||||
fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
||||||
|
|
||||||
fn request_head<S>(
|
fn request_head<S>(
|
||||||
@ -124,6 +126,10 @@ impl Github {
|
|||||||
impl Service for Github {
|
impl Service for Github {
|
||||||
type Response = GitHubApiResponse;
|
type Response = GitHubApiResponse;
|
||||||
|
|
||||||
|
fn path() -> &'static str {
|
||||||
|
"github"
|
||||||
|
}
|
||||||
|
|
||||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
||||||
format!(
|
format!(
|
||||||
"https://raw.githubusercontent.com/{}/{}/{}/{}",
|
"https://raw.githubusercontent.com/{}/{}/{}/{}",
|
||||||
@ -151,6 +157,10 @@ pub(crate) struct Bitbucket;
|
|||||||
impl Service for Bitbucket {
|
impl Service for Bitbucket {
|
||||||
type Response = BitbucketApiResponse;
|
type Response = BitbucketApiResponse;
|
||||||
|
|
||||||
|
fn path() -> &'static str {
|
||||||
|
"bitbucket"
|
||||||
|
}
|
||||||
|
|
||||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
||||||
format!(
|
format!(
|
||||||
"https://bitbucket.org/{}/{}/raw/{}/{}",
|
"https://bitbucket.org/{}/{}/raw/{}/{}",
|
||||||
@ -175,6 +185,10 @@ pub(crate) struct GitLab;
|
|||||||
impl Service for GitLab {
|
impl Service for GitLab {
|
||||||
type Response = GitLabApiResponse;
|
type Response = GitLabApiResponse;
|
||||||
|
|
||||||
|
fn path() -> &'static str {
|
||||||
|
"gitlab"
|
||||||
|
}
|
||||||
|
|
||||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
||||||
format!(
|
format!(
|
||||||
"https://gitlab.com/{}/{}/raw/{}/{}",
|
"https://gitlab.com/{}/{}/raw/{}/{}",
|
||||||
|
Loading…
Reference in New Issue
Block a user