Skip to content

Purge all domains on Fastly #6835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/worker/fastly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl Fastly {
/// Invalidate a path on Fastly
///
/// This method takes a path and invalidates the cached content on Fastly. The path must not
/// contain a wildcard, since the Fastly API does not support wildcard invalidations.
/// contain a wildcard, since the Fastly API does not support wildcard invalidations. Paths are
/// invalidated for both domains that are associated with the Fastly service.
///
/// Requests are authenticated using a token that is sent in a header. The token is passed to
/// the application as an environment variable.
Expand All @@ -38,11 +39,21 @@ impl Fastly {
));
}

let domains = [
&self.static_domain_name,
&format!("fastly-{}", self.static_domain_name),
];
let path = path.trim_start_matches('/');
let url = format!(
"https://api.fastly.com/purge/{}/{}",
self.static_domain_name, path
);

for domain in domains.iter() {
let url = format!("https://api.fastly.com/purge/{}/{}", domain, path);
self.purge_url(client, &url)?;
}

Ok(())
}

fn purge_url(&self, client: &Client, url: &str) -> anyhow::Result<()> {
trace!(?url);

let api_token = self.api_token.expose_secret();
Expand All @@ -54,7 +65,7 @@ impl Fastly {

debug!("sending invalidation request to Fastly");
let response = client
.post(&url)
.post(url)
.headers(headers)
.send()
.context("failed to send invalidation request to Fastly")?;
Expand All @@ -76,7 +87,7 @@ impl Fastly {
"invalidation request to Fastly failed"
);

Err(error).with_context(|| format!("failed to invalidate {path} on Fastly"))
Err(error).with_context(|| format!("failed to purge {url}"))
}
}
}
Expand Down