Skip to content

Commit 5f7edbc

Browse files
authored
Merge pull request #72 from jdno/fastly-surrogate-keys
Invalidate the cache on Fastly using surrogate keys
2 parents 8f4b455 + e8149ef commit 5f7edbc

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ pub(crate) struct Config {
204204

205205
/// An API token for Fastly with the `purge_select` scope.
206206
pub(crate) fastly_api_token: Option<String>,
207-
/// The static domain name that is used with Fastly, e.g. `static.rust-lang.org`.
208-
pub(crate) fastly_static_domain: Option<String>,
207+
/// The Fastly service ID to purge when releasing.
208+
pub(crate) fastly_service_id: Option<String>,
209209

210210
/// Temporary variable to test Fastly in the dev environment only.
211211
pub(crate) invalidate_fastly: bool,
@@ -245,7 +245,7 @@ impl Config {
245245
github_app_key: maybe_env("GITHUB_APP_KEY")?,
246246
github_app_id: maybe_env("GITHUB_APP_ID")?,
247247
fastly_api_token: maybe_env("FASTLY_API_TOKEN")?,
248-
fastly_static_domain: maybe_env("FASTLY_STATIC_DOMAIN")?,
248+
fastly_service_id: maybe_env("FASTLY_SERVICE_ID")?,
249249
invalidate_fastly: bool_env("INVALIDATE_FASTLY")?,
250250
})
251251
}
@@ -270,8 +270,8 @@ impl Config {
270270
}
271271

272272
pub(crate) fn fastly(&self) -> Option<Fastly> {
273-
if let (Some(token), Some(domain)) = (&self.fastly_api_token, &self.fastly_static_domain) {
274-
Some(Fastly::new(token.clone(), domain.clone()))
273+
if let (Some(token), Some(service_id)) = (&self.fastly_api_token, &self.fastly_service_id) {
274+
Some(Fastly::new(token.clone(), service_id.clone()))
275275
} else {
276276
None
277277
}

src/fastly.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ use curl::easy::Easy;
33

44
pub struct Fastly {
55
api_token: String,
6-
domain: String,
6+
service_id: String,
77
client: Easy,
88
}
99

1010
impl Fastly {
11-
pub fn new(api_token: String, domain: String) -> Self {
11+
pub fn new(api_token: String, service_id: String) -> Self {
1212
Self {
1313
api_token,
14-
domain,
14+
service_id,
1515
client: Easy::new(),
1616
}
1717
}
1818

1919
pub fn purge(&mut self, path: &str) -> Result<(), Error> {
2020
let sanitized_path = path.trim_start_matches('/');
2121
let url = format!(
22-
"https://api.fastly.com/purge/{}/{}",
23-
self.domain, sanitized_path
22+
"https://api.fastly.com/service/{}/purge/{}",
23+
self.service_id, sanitized_path
2424
);
2525

2626
self.start_new_request()?;

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ impl Context {
574574
}
575575

576576
fn invalidate_releases(&self) -> Result<(), Error> {
577+
// The following paths need to be added as surrogate keys to the Fastly service, otherwise
578+
// they won't be invalidated. See the following pull request for an example:
579+
// https://github.com/rust-lang/simpleinfra/pull/295
577580
let paths = ["/dist/*".into()];
578581

579582
self.invalidate_cloudfront(&self.config.cloudfront_static_id, &paths)?;
@@ -626,15 +629,14 @@ impl Context {
626629
None => {
627630
println!();
628631
println!("WARNING! Skipped Fastly invalidation of: {:?}", paths);
629-
println!("Set PROMOTE_RELEASE_FASTLY_API_TOKEN and PROMOTE_RELEASE_FASTLY_STATIC_DOMAIN if you want to invalidate Fastly");
632+
println!("Set PROMOTE_RELEASE_FASTLY_API_TOKEN and PROMOTE_RELEASE_FASTLY_SERVICE_ID if you want to invalidate Fastly");
630633
println!();
631634
return Ok(());
632635
}
633636
};
634637

635638
for path in paths {
636-
let path = path.replace('*', "");
637-
fastly.purge(&path)?;
639+
fastly.purge(path)?;
638640
}
639641

640642
Ok(())

0 commit comments

Comments
 (0)