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 | |
| parent | 292c43392102cde951d345cf012e0231826877a5 (diff) | |
| download | xesite-49c62851137d2757d6e8019636c908b4612f61d1.tar.xz xesite-49c62851137d2757d6e8019636c908b4612f61d1.zip | |
purge cloudflare
Signed-off-by: Xe Iaso <me@christine.website>
| -rw-r--r-- | Cargo.lock | 16 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -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 | ||||
| -rw-r--r-- | src/app/mod.rs | 6 | ||||
| -rw-r--r-- | src/app/poke.rs | 48 | ||||
| -rw-r--r-- | src/post/mod.rs | 11 |
8 files changed, 6 insertions, 176 deletions
@@ -402,21 +402,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] -name = "cfcache" -version = "0.1.0" -dependencies = [ - "eyre", - "kankyo", - "reqwest", - "serde", - "serde_json", - "thiserror", - "tokio", - "tracing", - "tracing-futures", -] - -[[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3243,7 +3228,6 @@ dependencies = [ "axum 0.5.16", "axum-extra", "axum-macros", - "cfcache", "chrono", "color-eyre", "derive_more", @@ -50,7 +50,6 @@ uuid = { version = "0.8", features = ["serde", "v4"] } xesite_types = { path = "./lib/xesite_types" } # workspace dependencies -cfcache = { path = "./lib/cfcache" } xe_jsonfeed = { path = "./lib/jsonfeed" } mi = { path = "./lib/mi" } patreon = { path = "./lib/patreon" } 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(()) - } -} diff --git a/src/app/mod.rs b/src/app/mod.rs index 9a331a4..73320e1 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -68,9 +68,9 @@ pub async fn init(cfg: PathBuf) -> Result<State> { cfg.clone().mi_token.clone(), crate::APPLICATION_NAME.to_string(), )?; - let blog = crate::post::load(cfg.clone(), "blog").await?; - let gallery = crate::post::load(cfg.clone(), "gallery").await?; - let talks = crate::post::load(cfg.clone(), "talks").await?; + let blog = crate::post::load("blog").await?; + let gallery = crate::post::load("gallery").await?; + let talks = crate::post::load("talks").await?; let mut everything: Vec<Post> = vec![]; { diff --git a/src/app/poke.rs b/src/app/poke.rs index ef5f882..404b14d 100644 --- a/src/app/poke.rs +++ b/src/app/poke.rs @@ -7,18 +7,9 @@ pub async fn the_cloud() -> Result<()> { info!("waiting for things to settle"); delay_for(Duration::from_secs(10)).await; - info!("purging cloudflare cache"); - cloudflare().await?; - - info!("waiting for the cloudflare cache to purge globally"); - delay_for(Duration::from_secs(45)).await; - info!("poking mi"); mi().await?; - info!("poking bing"); - bing().await?; - info!("poking google"); google().await?; @@ -26,19 +17,6 @@ pub async fn the_cloud() -> Result<()> { } #[instrument(err)] -async fn bing() -> Result<()> { - let cli = reqwest::Client::new(); - cli.get("https://www.bing.com/ping") - .query(&[("sitemap", "https://xeiaso.net/sitemap.xml")]) - .header("User-Agent", crate::APPLICATION_NAME) - .send() - .await? - .error_for_status()?; - - Ok(()) -} - -#[instrument(err)] async fn google() -> Result<()> { let cli = reqwest::Client::new(); cli.get("https://www.google.com/ping") @@ -52,32 +30,6 @@ async fn google() -> Result<()> { } #[instrument(err)] -async fn cloudflare() -> Result<()> { - let cli = cfcache::Client::new(env::var("CF_TOKEN")?, env::var("CF_ZONE_ID")?)?; - cli.purge( - vec![ - "https://xeiaso.net/sitemap.xml", - "https://xeiaso.net", - "https://xeiaso.net/blog", - "https://xeiaso.net/blog.atom", - "https://xeiaso.net/blog.json", - "https://xeiaso.net/blog.rss", - "https://xeiaso.net/gallery", - "https://xeiaso.net/talks", - "https://xeiaso.net/resume", - "https://xeiaso.net/signalboost", - "https://xeiaso.net/feeds", - ] - .into_iter() - .map(|i| i.to_string()) - .collect(), - ) - .await?; - - Ok(()) -} - -#[instrument(err)] async fn mi() -> Result<()> { let cli = mi::Client::new(env::var("MI_TOKEN")?, crate::APPLICATION_NAME.to_string())?; cli.refresh().await?; diff --git a/src/post/mod.rs b/src/post/mod.rs index 263471d..0ad5cc0 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -83,12 +83,7 @@ impl Post { } } -async fn read_post( - cfg: Arc<Config>, - dir: &str, - fname: PathBuf, - cli: &Option<mi::Client>, -) -> Result<Post> { +async fn read_post(dir: &str, fname: PathBuf, cli: &Option<mi::Client>) -> Result<Post> { debug!( "loading {}", fname.clone().into_os_string().into_string().unwrap() @@ -151,7 +146,7 @@ async fn read_post( }) } -pub async fn load(cfg: Arc<Config>, dir: &str) -> Result<Vec<Post>> { +pub async fn load(dir: &str) -> Result<Vec<Post>> { let cli = match std::env::var("MI_TOKEN") { Ok(token) => mi::Client::new(token.to_string(), crate::APPLICATION_NAME.to_string()).ok(), Err(_) => None, @@ -159,7 +154,7 @@ pub async fn load(cfg: Arc<Config>, dir: &str) -> Result<Vec<Post>> { let futs = glob(&format!("{}/*.markdown", dir))? .filter_map(Result::ok) - .map(|fname| read_post(cfg.clone(), dir, fname, cli.borrow())); + .map(|fname| read_post(dir, fname, cli.borrow())); let mut result: Vec<Post> = futures::future::join_all(futs) .await |
