diff options
| author | Christine Dodrill <me@christine.website> | 2021-01-16 21:38:22 -0500 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2021-01-16 21:38:22 -0500 |
| commit | 4bcc848bb178d9e4372ba13b750d620cabc2a9ac (patch) | |
| tree | e2a97a5a03c934de6307b3d543d5f4b249675ce7 /lib | |
| parent | 17af42bc698237d1560b8add144641ae3950b469 (diff) | |
| download | xesite-4bcc848bb178d9e4372ba13b750d620cabc2a9ac.tar.xz xesite-4bcc848bb178d9e4372ba13b750d620cabc2a9ac.zip | |
move poking services into app boot after systemd notify
Signed-off-by: Christine Dodrill <me@christine.website>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/cfcache/Cargo.toml | 20 | ||||
| -rw-r--r-- | lib/cfcache/examples/purge.rs | 15 | ||||
| -rw-r--r-- | lib/cfcache/src/lib.rs | 64 | ||||
| -rw-r--r-- | lib/mi/src/lib.rs | 12 |
4 files changed, 110 insertions, 1 deletions
diff --git a/lib/cfcache/Cargo.toml b/lib/cfcache/Cargo.toml new file mode 100644 index 0000000..85060c2 --- /dev/null +++ b/lib/cfcache/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "cfcache" +version = "0.1.0" +authors = ["Christine Dodrill <me@christine.website>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reqwest = { version = "0.10", features = ["json"] } +serde_json = "1.0" +serde = { version = "1", features = ["derive"] } +thiserror = "1" +tracing = "0.1" +tracing-futures = "0.2" + +[dev-dependencies] +eyre = "0.6.5" +kankyo = "0.3" +tokio = { version = "0.2", features = ["full"] } diff --git a/lib/cfcache/examples/purge.rs b/lib/cfcache/examples/purge.rs new file mode 100644 index 0000000..22c81a4 --- /dev/null +++ b/lib/cfcache/examples/purge.rs @@ -0,0 +1,15 @@ +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://christine.website/.within/health".to_string()]) + .await?; + + Ok(()) +} diff --git a/lib/cfcache/src/lib.rs b/lib/cfcache/src/lib.rs new file mode 100644 index 0000000..baf6775 --- /dev/null +++ b/lib/cfcache/src/lib.rs @@ -0,0 +1,64 @@ +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/lib/mi/src/lib.rs b/lib/mi/src/lib.rs index ec9d459..0e19bec 100644 --- a/lib/mi/src/lib.rs +++ b/lib/mi/src/lib.rs @@ -34,7 +34,7 @@ impl Client { }) } - #[instrument(skip(self))] + #[instrument(skip(self), err)] pub async fn mentioners(&self, url: String) -> Result<Vec<WebMention>> { Ok(self .cli @@ -46,6 +46,16 @@ impl Client { .json() .await?) } + + #[instrument(skip(self), err)] + pub async fn refresh(&self) -> Result<()> { + self.cli + .post("https://mi.within.website/api/blog/refresh") + .send() + .await? + .error_for_status()?; + Ok(()) + } } #[derive(Debug, Deserialize, Eq, PartialEq, Clone)] |
