#!/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 newsproject.bitreport.pt zones. 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)" # Fetch existing entrypoint ruleset local existing existing=$(curl -fsS "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoints" \ -H "Authorization: Bearer $CF_API_TOKEN" 2>/dev/null || echo '{"result":{}}') local ruleset_id ruleset_id=$(echo "$existing" | python3 -c " import sys, json d = json.load(sys.stdin) r = d.get('result', {}) print(r.get('id', '')) " 2>/dev/null || echo "") local new_rule new_rule=$(python3 -c " import json, sys rule = { 'action': 'set_cache_settings', 'action_parameters': { 'cache': True, 'origin_cache_control': {'enabled': True} }, 'expression': sys.argv[1], 'description': sys.argv[2], 'enabled': True } print(json.dumps(rule)) " "$expr" "$rule_name") if [ -n "$ruleset_id" ]; then # Append the new rule to the existing ruleset local existing_rules existing_rules=$(echo "$existing" | python3 -c " import sys, json d = json.load(sys.stdin) print(json.dumps(d.get('result', {}).get('rules', []))) ") local payload payload=$(python3 -c " import json, sys existing = json.loads(sys.argv[1]) new_rule = json.loads(sys.argv[2]) # Remove any prior rule with same description to avoid duplicates 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 PATCH "$CF_API/zones/$zone_id/rulesets/$ruleset_id" \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ -d "$payload" | python3 -m json.tool else curl -fsS -X PUT "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoints" \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ -d "$(python3 -c "import json,sys; print(json.dumps({'rules': [json.loads(sys.argv[1])]}))" "$new_rule")" \ | 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 "newsproject.bitreport.pt") echo " newsproject.bitreport.pt : $ZONE_NEWS" 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 starts_with "/wp-json/") and not (http.request.uri.path starts_with "/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 starts_with "/wp-json/") and not (http.request.uri.path starts_with "/api/")' upsert_cache_rule "$ZONE_NEWS" "$EXPR_NEWS" "newsproject.bitreport.pt" echo "" echo "==> Waiting 10 s for rule propagation..." sleep 10 echo "==> Verification — double curl per zone:" for HOST in "https://andebol.net/" "https://newsproject.bitreport.pt/"; do echo "" echo "--- $HOST ---" for i in 1 2; do echo -n " pass $i: " curl -fsSI "$HOST" --max-time 15 \ -w "cf-cache-status=%{header{cf-cache-status}} TTFB=%{time_starttransfer}s\n" \ -o /dev/null 2>/dev/null done done echo "" echo "Done. Expected: cf-cache-status=HIT (or STALE/REVALIDATED) on pass 2." echo "If still DYNAMIC, wait 30 s and re-run the verification block only."