Skip to content

Commit 127da28

Browse files
committed
purge: Support configuring StrikeTracker API via env variables
1 parent de8b0a8 commit 127da28

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

purge.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,53 @@
1111
* $ REQUEST_URI="/example" php purge.php
1212
*/
1313

14-
$configFile = __DIR__ . '/config.json';
15-
1614
if ( !isset( $_SERVER[ 'REQUEST_URI' ] )
17-
|| !is_readable( $configFile )
1815
|| !function_exists( 'curl_init' )
1916
) {
2017
http_response_code( 500 );
2118
echo "Context error.\n";
2219
exit;
2320
}
2421

25-
$config = json_decode( file_get_contents( $configFile ) );
26-
$hwConfig = $config->highwinds;
27-
if ( !$hwConfig
28-
|| !$hwConfig->api_url
29-
|| !$hwConfig->api_token
30-
|| !$hwConfig->account_hash
31-
|| !$hwConfig->file_hostname
22+
// Highwinds StrikeTracker
23+
$striketrackerUrl = getenv( 'STRIKETRACKER_URL' ) ?: 'https://striketracker.highwinds.com';
24+
$striketrackerToken = getenv( 'STRIKETRACKER_TOKEN' ) ?: false;
25+
$striketrackerAccountHash = getenv( 'STRIKETRACKER_ACCOUNT' ) ?: false;
26+
// This is configurable because the purge script may be invoked
27+
// from a hostname different from the one canonically serving the asset,
28+
// or. e.g. from the CLI.
29+
$striketrackerPurgeHostname = getenv( 'STRIKETRACKER_PURGE_HOSTNAME' ) ?: 'code.jquery.com';
30+
31+
if ( !$striketrackerUrl
32+
|| !$striketrackerToken
33+
|| !$striketrackerAccountHash
34+
|| !$striketrackerPurgeHostname
3235
) {
33-
http_response_code( 500 );
34-
echo "Configuration error.\n";
35-
exit;
36+
$configFile = __DIR__ . '/config.json';
37+
$configJson = @file_get_contents( $configFile );
38+
$config = $configJson ? json_decode( $configJson ) : false;
39+
$hwConfig = $config ? $config->highwinds : false;
40+
if ( !$hwConfig
41+
|| !$hwConfig->api_url
42+
|| !$hwConfig->api_token
43+
|| !$hwConfig->account_hash
44+
|| !$hwConfig->file_hostname
45+
) {
46+
http_response_code( 500 );
47+
echo "Configuration error.\n";
48+
exit;
49+
}
50+
$striketrackerUrl = $hwConfig->api_url;
51+
$striketrackerToken = $hwConfig->api_token;
52+
$striketrackerAccountHash = $hwConfig->account_hash;
53+
$striketrackerPurgeHostname = $hwConfig->file_hostname;
3654
}
3755

3856
// The StrikeTracker Purge API is protocol-sensitive.
3957
// HTTP and HTTPS need to be purged separately, or
4058
// we can use a protocol-relative file url, which Highwinds
4159
// supports as short-cut for purging both.
42-
$file = "//{$hwConfig->file_hostname}/" . ltrim( $_SERVER[ 'REQUEST_URI' ], '/' );
60+
$file = "//{$striketrackerPurgeHostname}/" . ltrim( $_SERVER[ 'REQUEST_URI' ], '/' );
4361

4462
/**
4563
* Make an HTTP POST request, submitting JSON data, and receiving JSON data.
@@ -67,10 +85,10 @@ function jq_request_post_json( $url, array $headers, array $postData ) {
6785

6886
$result = jq_request_post_json(
6987
// url
70-
"{$hwConfig->api_url}/api/accounts/{$hwConfig->account_hash}/purge",
88+
"{$striketrackerUrl}/api/accounts/{$striketrackerAccountHash}/purge",
7189
// headers
7290
array(
73-
"Authorization: Bearer {$hwConfig->api_token}",
91+
"Authorization: Bearer {$striketrackerToken}",
7492
"Content-Type: application/json",
7593
),
7694
// post body (will be encoded as JSON)

0 commit comments

Comments
 (0)