#!/usr/bin/env bash # Apply Cloudflare HTML Cache Rules for news-platform zones (BIT-21912 / BIT-21847) # # Usage: CF_API_TOKEN= bash deploy/scripts/cf-cache-rules-apply.sh # # Requires: CF_API_TOKEN with Cache Rules write permission for andebol.net # and bitreport.pt zones. # # Notes discovered during BIT-21922: # - newsproject.bitreport.pt is a subdomain of zone bitreport.pt (not its own zone) # - CF API endpoint is /entrypoint (singular), not /entrypoints # - starts_with operator not supported on Free plan; use contains # - edge_ttl override_origin required to cache HTML (origin_cache_control alone is insufficient) # - Verification must use GET requests (not HEAD/-I); rule expression matches GET only set -euo pipefail : "${CF_API_TOKEN:?CF_API_TOKEN must be set (Cloudflare API token with Cache Rules write)}" CF_API="https://api.cloudflare.com/client/v4" get_zone_id() { local name=$1 local result result=$(curl -fsS "$CF_API/zones?name=$name" \ -H "Authorization: Bearer $CF_API_TOKEN") echo "$result" | python3 -c " import sys, json d = json.load(sys.stdin) if not d.get('result'): raise SystemExit(f'Zone not found or token lacks read permission for: ${name}') print(d['result'][0]['id']) " } upsert_cache_rule() { local zone_id=$1 local expr=$2 local zone_name=$3 local rule_name="News platform — cache HTML (respect origin)" local new_rule new_rule=$(python3 -c " import json, sys rule = { 'action': 'set_cache_settings', 'action_parameters': { 'cache': True, 'edge_ttl': {'mode': 'override_origin', 'default': 60} }, 'expression': sys.argv[1], 'description': sys.argv[2], 'enabled': True } print(json.dumps(rule)) " "$expr" "$rule_name") # Fetch existing entrypoint ruleset (returns 200 with result:null if none exists) local existing existing=$(curl -sS "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoint" \ -H "Authorization: Bearer $CF_API_TOKEN" 2>/dev/null || echo '{"result":null}') local ruleset_id ruleset_id=$(echo "$existing" | python3 -c " import sys, json d = json.load(sys.stdin) r = d.get('result') or {} print(r.get('id', '')) " 2>/dev/null || echo "") local payload if [ -n "$ruleset_id" ]; then # Update existing ruleset: dedup by description then append local existing_rules existing_rules=$(echo "$existing" | python3 -c " import sys, json d = json.load(sys.stdin) print(json.dumps((d.get('result') or {}).get('rules', []))) ") payload=$(python3 -c " import json, sys existing = json.loads(sys.argv[1]) new_rule = json.loads(sys.argv[2]) rules = [r for r in existing if r.get('description') != new_rule['description']] rules.append(new_rule) print(json.dumps({'rules': rules})) " "$existing_rules" "$new_rule") curl -fsS -X PUT "$CF_API/zones/$zone_id/rulesets/$ruleset_id" \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ --data-raw "$payload" | python3 -m json.tool else # Create new entrypoint ruleset payload=$(python3 -c " import json, sys print(json.dumps({'rules': [json.loads(sys.argv[1])]})) " "$new_rule") curl -fsS -X PUT "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoint" \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ --data-raw "$payload" | python3 -m json.tool fi echo "Cache rule applied for $zone_name (zone $zone_id)" } echo "==> Resolving zone IDs..." ZONE_ANDEBOL=$(get_zone_id "andebol.net") echo " andebol.net : $ZONE_ANDEBOL" ZONE_NEWS=$(get_zone_id "bitreport.pt") echo " newsproject.bitreport.pt: $ZONE_NEWS (zone: bitreport.pt)" echo "==> Applying cache rule for andebol.net..." EXPR_ANDEBOL='(http.request.method eq "GET") and (http.host in {"andebol.net" "www.andebol.net"}) and not (http.request.uri.path contains "/wp-admin") and not (http.request.uri.path contains "/wp-login.php") and not (http.request.uri.path contains "/wp-json/") and not (http.request.uri.path contains "/api/")' upsert_cache_rule "$ZONE_ANDEBOL" "$EXPR_ANDEBOL" "andebol.net" echo "==> Applying cache rule for newsproject.bitreport.pt..." EXPR_NEWS='(http.request.method eq "GET") and (http.host eq "newsproject.bitreport.pt") and not (http.request.uri.path contains "/wp-admin") and not (http.request.uri.path contains "/wp-login.php") and not (http.request.uri.path contains "/wp-json/") and not (http.request.uri.path contains "/api/")' upsert_cache_rule "$ZONE_NEWS" "$EXPR_NEWS" "newsproject.bitreport.pt" echo "" echo "==> Waiting 30 s for rule propagation..." sleep 30 echo "==> Verification — double GET per zone (rule matches GET only, not HEAD):" for HOST in "https://andebol.net/" "https://newsproject.bitreport.pt/"; do echo "" echo "--- $HOST ---" for i in 1 2; do CF=$(curl -fsS "$HOST" --max-time 15 -D - -o /dev/null 2>/dev/null | grep -i "cf-cache-status:" | tr -d '\r') AGE=$(curl -fsS "$HOST" --max-time 15 -D - -o /dev/null 2>/dev/null | grep -i "^age:" | tr -d '\r') TTFB=$(curl -fsS "$HOST" --max-time 15 -D - -o /dev/null -w "%{time_starttransfer}" 2>/dev/null | tail -1) echo " pass $i: $CF | $AGE | TTFB=${TTFB}s" done done echo "" echo "Done. Expected: cf-cache-status=HIT on pass 2." echo "If still MISS/DYNAMIC, wait 60 s and re-run the verification block."