diff options
| author | Xe Iaso <me@christine.website> | 2022-09-21 02:31:43 +0000 |
|---|---|---|
| committer | Xe Iaso <me@christine.website> | 2022-09-21 02:31:43 +0000 |
| commit | 49c62851137d2757d6e8019636c908b4612f61d1 (patch) | |
| tree | 141577daeb9f3e89def22fa930ea4fcbf3622813 /lib/cfcache | |
| parent | 292c43392102cde951d345cf012e0231826877a5 (diff) | |
| download | xesite-49c62851137d2757d6e8019636c908b4612f61d1.tar.xz xesite-49c62851137d2757d6e8019636c908b4612f61d1.zip | |
purge cloudflare
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'lib/cfcache')
| -rw-r--r-- | lib/cfcache/Cargo.toml | 21 | ||||
| -rw-r--r-- | lib/cfcache/examples/purge.rs | 15 | ||||
| -rw-r--r-- | lib/cfcache/src/lib.rs | 64 |
3 files changed, 0 insertions, 100 deletions
diff --git a/lib/cfcache/Cargo.toml b/lib/cfcache/Cargo.toml deleted file mode 100644 index c272906..0000000 --- a/lib/cfcache/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "cfcache" -version = "0.1.0" -authors = ["Xe Iaso <me@xeiaso.net>"] -edition = "2018" -license = "zlib" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -reqwest = { version = "0.11", features = ["json"] } -serde_json = "1" -serde = { version = "1", features = ["derive"] } -thiserror = "1" -tracing = "0.1" -tracing-futures = "0.2" - -[dev-dependencies] -eyre = "0.6.8" -kankyo = "0.3" -tokio = { version = "1", features = ["full"] } diff --git a/lib/cfcache/examples/purge.rs b/lib/cfcache/examples/purge.rs deleted file mode 100644 index 43e269f..0000000 --- a/lib/cfcache/examples/purge.rs +++ /dev/null @@ -1,15 +0,0 @@ -use eyre::Result; - -#[tokio::main] -async fn main() -> Result<()> { - kankyo::init()?; - - let key = std::env::var("CF_TOKEN")?; - let zone_id = std::env::var("CF_ZONE_ID")?; - - let cli = cfcache::Client::new(key, zone_id)?; - cli.purge(vec!["https://xeiaso.net/.within/health".to_string()]) - .await?; - - Ok(()) -} diff --git a/lib/cfcache/src/lib.rs b/lib/cfcache/src/lib.rs deleted file mode 100644 index baf6775..0000000 --- a/lib/cfcache/src/lib.rs +++ /dev/null @@ -1,64 +0,0 @@ -use reqwest::header; -use tracing::instrument; - -pub type Result<T = ()> = std::result::Result<T, Error>; - -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("json error: {0}")] - Json(#[from] serde_json::Error), - - #[error("request error: {0}")] - Request(#[from] reqwest::Error), - - #[error("invalid header value: {0}")] - InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue), -} - -pub struct Client { - zone_id: String, - cli: reqwest::Client, -} - -static USER_AGENT: &str = concat!( - "xesite ", - env!("CARGO_PKG_NAME"), - "/", - env!("CARGO_PKG_VERSION") -); - -impl Client { - pub fn new(api_key: String, zone_id: String) -> Result<Self> { - let mut headers = header::HeaderMap::new(); - headers.insert( - header::AUTHORIZATION, - header::HeaderValue::from_str(&format!("Bearer {}", api_key))?, - ); - - let cli = reqwest::Client::builder() - .user_agent(USER_AGENT) - .default_headers(headers) - .build()?; - - Ok(Self { zone_id, cli }) - } - - #[instrument(skip(self), err)] - pub async fn purge(&self, urls: Vec<String>) -> Result { - #[derive(serde::Serialize)] - struct Files { - files: Vec<String>, - } - - self.cli - .post(&format!( - "https://api.cloudflare.com/client/v4/zones/{}/purge_cache", - self.zone_id - )) - .json(&Files { files: urls }) - .send() - .await? - .error_for_status()?; - Ok(()) - } -} |
