yagcdn/backend/src/cdn.rs

84 lines
2.3 KiB
Rust
Raw Normal View History

2019-07-28 16:10:04 +02:00
use crate::{
service::Service,
statics::{self, CF_ZONE_IDENT},
};
2019-07-28 17:07:15 +02:00
use actix_web::{http::header, Error, HttpResponse};
2019-07-28 14:45:39 +02:00
use awc::Client;
use futures::Future;
pub(crate) struct Cloudflare;
impl Cloudflare {
fn identifier() -> &'static str {
&CF_ZONE_IDENT
}
2019-08-09 16:19:55 +02:00
pub(crate) fn purge_cache<T: Service>(
2019-07-28 17:07:15 +02:00
client: &Client,
file: &str,
) -> impl Future<Item = HttpResponse, Error = Error> {
2019-07-28 17:40:08 +02:00
let payload = CfPurgeRequest::singleton::<T>(file);
println!("{:#?}", payload);
2019-07-28 17:07:15 +02:00
client
.post(format!(
"https://api.cloudflare.com/client/v4/zones/{}/purge_cache",
Self::identifier()
))
2019-08-11 13:48:04 +02:00
.header(header::USER_AGENT, &*statics::USER_AGENT.as_str())
2019-07-28 17:07:15 +02:00
.header("X-Auth-Email", Self::auth_email())
.header("X-Auth-Key", Self::auth_key())
.content_type("application/json")
2019-07-28 17:40:08 +02:00
.send_json(&payload)
2019-07-28 17:07:15 +02:00
.from_err()
.and_then(|response| HttpResponse::build(response.status()).streaming(response))
2019-08-09 16:19:55 +02:00
// client
// .post(format!(
// "https://api.cloudflare.com/client/v4/zones/{}/purge_cache",
// Self::identifier()
// ))
2019-08-11 13:48:04 +02:00
// .header(header::USER_AGENT, statics::USER_AGENT.as_ref())
2019-08-09 16:19:55 +02:00
// .header("X-Auth-Email", Self::auth_email())
// .header("X-Auth-Key", Self::auth_key())
// .content_type("application/json")
// .send_json(&CfPurgeRequest::singleton::<T>(file))
// .from_err()
// .and_then(|mut response| {
// response
// .json::<CfPurgeResponse>()
// .map(|resp| resp.success)
// .from_err()
// })
2019-07-28 14:45:39 +02:00
}
fn auth_key() -> &'static str {
&statics::CF_AUTH_KEY
}
fn auth_email() -> &'static str {
&statics::CF_AUTH_USER
}
}
2019-07-28 17:40:08 +02:00
#[derive(Serialize, Debug)]
2019-07-28 14:45:39 +02:00
struct CfPurgeRequest {
files: Vec<String>,
}
impl CfPurgeRequest {
2019-07-28 16:10:04 +02:00
fn singleton<T: Service>(file: &str) -> Self {
2019-07-28 20:45:58 +02:00
Self {
files: vec![format!(
"https://{}/{}/{}",
2019-08-11 13:48:04 +02:00
statics::HOSTNAME.as_ref(),
2019-07-28 20:45:58 +02:00
T::path(),
file
)],
}
2019-07-28 14:45:39 +02:00
}
}
2019-08-09 16:19:55 +02:00
// #[derive(Deserialize)]
// struct CfPurgeResponse {
// success: bool,
// }