-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
239 lines (210 loc) · 7.14 KB
/
Copy pathmain.go
File metadata and controls
239 lines (210 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// cache-warmer — pre-warm Cloudflare image transforms for a static site.
//
// A Go port of the original warm-cache.sh bash script. The rationale in short:
//
// Every /cdn-cgi/image/... variant is generated on first request (cold),
// which adds latency for whichever real visitor hits it first. This tool
// requests each variant ahead of time so Cloudflare generates + edge-caches
// it, in both AVIF and WebP, so no visitor hits a cold image.
//
// It is a drop-in for ANY site: it crawls the deployed HTML/CSS under WEBROOT
// for /cdn-cgi/image/ URLs (nothing is hardcoded per-site), then fetches each
// one through DOMAIN so Cloudflare's edge is what gets warmed.
//
// Usage:
//
// ./cache-warmer # use defaults below
// DOMAIN=https://example.com WEBROOT=/var/www/html ./cache-warmer
//
// Cron (6am on the 1st of each month, logged):
//
// 0 6 1 * * /root/cache-warmer >> /var/log/warm-cache.log 2>&1
package main
import (
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// --- config (override via environment, mirrors the bash script) ------------
// config is just data + functions that operate on it — Go's answer to a
// "settings class".
type config struct {
domain string // public origin, e.g. https://example.com
webroot string // deployed site directory to scan for transform URLs
ua string // User-Agent (allowlist this in Cloudflare WAF if needed)
timeout time.Duration // per-request timeout
}
// envOr mirrors bash's "${VAR:-default}".
func envOr(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func loadConfig() config {
// MAX_TIME is seconds in the bash script; keep the same meaning here.
secs, err := strconv.Atoi(envOr("MAX_TIME", "20"))
if err != nil || secs <= 0 {
secs = 20
}
return config{
// TrimRight mirrors DOMAIN="${DOMAIN%/}" (strip trailing slash).
domain: strings.TrimRight(envOr("DOMAIN", "https://example.com"), "/"),
webroot: envOr("WEBROOT", "/var/www/html"),
ua: envOr("UA", "CF-Transforms-Cache-Warmer/1.0"),
timeout: time.Duration(secs) * time.Second,
}
}
// --- discovery --------------------------------------------------------------
// transformRe is the same pattern the bash script greps for. It stops at the
// first delimiter that would end the URL in markup or CSS: double quote,
// single quote, backtick, space, or closing paren (CSS url(...)).
var transformRe = regexp.MustCompile("/cdn-cgi/image/[^\"'` )]+")
// discoverPaths walks webroot for .html/.css files and returns every unique
// /cdn-cgi/image/ path found, sorted (equivalent of grep -rhoE ... | sort -u).
func discoverPaths(webroot string) ([]string, error) {
found := make(map[string]struct{}) // a "set": map with empty-struct values
err := filepath.WalkDir(webroot, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil // descend into directories
}
ext := strings.ToLower(filepath.Ext(path))
if ext != ".html" && ext != ".css" {
return nil // same filter as grep --include='*.html' --include='*.css'
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
for _, match := range transformRe.FindAll(data, -1) {
found[string(match)] = struct{}{}
}
return nil
})
if err != nil {
return nil, err
}
paths := make([]string, 0, len(found))
for p := range found {
paths = append(paths, p)
}
sort.Strings(paths)
return paths, nil
}
// --- warming ----------------------------------------------------------------
// Accept headers: first pass prefers AVIF, second forces WebP (no avif
// offered) so both cached formats get generated. format=auto keys the
// Cloudflare cache on this header.
const (
acceptAVIF = "image/avif,image/webp,image/*,*/*;q=0.8"
acceptWebP = "image/webp,image/*,*/*;q=0.8"
)
// stats is the Go equivalent of the bash counters total/ok/miss/hit/errors.
type stats struct {
total, ok, hit, miss, errors int
}
// warm performs one request and records the outcome — the port of bash's
// warm() function (the curl call plus its header/status parsing).
func warm(client *http.Client, cfg config, label, accept, url string, st *stats) {
start := time.Now()
code := "ERR"
cf := "?"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err == nil {
req.Header.Set("User-Agent", cfg.ua)
req.Header.Set("Accept", accept)
var resp *http.Response
resp, err = client.Do(req)
if err == nil {
// Drain and close the body: content is irrelevant (bash sent it
// to /dev/null), but draining lets Go reuse the connection.
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
code = strconv.Itoa(resp.StatusCode)
// Header names are case-insensitive; Get canonicalises
// "CF-Cache-Status" for us.
if v := resp.Header.Get("Cf-Cache-Status"); v != "" {
cf = v
}
}
}
elapsed := time.Since(start).Seconds()
st.total++
switch {
case err != nil:
st.errors++
fmt.Printf(" [%-4s] ERR cf=%-8s %.3fs %s (%v)\n", label, cf, elapsed, url, err)
return
case code == "200":
st.ok++
switch strings.ToUpper(cf) {
case "HIT":
st.hit++
case "MISS", "EXPIRED", "UPDATING", "REVALIDATED":
st.miss++
}
default:
st.errors++
}
fmt.Printf(" [%-4s] %s cf=%-8s %.3fs %s\n", label, code, cf, elapsed, url)
}
// --- main --------------------------------------------------------------------
func main() {
cfg := loadConfig()
// Validate webroot, like the bash [[ ! -d "$WEBROOT" ]] check.
info, err := os.Stat(cfg.webroot)
if err != nil || !info.IsDir() {
fmt.Fprintf(os.Stderr, "ERROR: WEBROOT not found: %s\n", cfg.webroot)
fmt.Fprintln(os.Stderr, "Set WEBROOT to the deployed site directory.")
os.Exit(1)
}
// Go formats dates with a reference-time layout ("Mon Jan 2 15:04:05
// 2006"), not strftime tokens — this matches bash's date '+%F %T %Z'.
fmt.Printf("=== cache-warmer — %s ===\n", time.Now().Format("2006-01-02 15:04:05 MST"))
fmt.Println("Domain :", cfg.domain)
fmt.Println("Webroot:", cfg.webroot)
paths, err := discoverPaths(cfg.webroot)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: scanning %s: %v\n", cfg.webroot, err)
os.Exit(1)
}
if len(paths) == 0 {
fmt.Fprintf(os.Stderr, "ERROR: no /cdn-cgi/image/ URLs found under %s\n", cfg.webroot)
os.Exit(1)
}
fmt.Printf("Found %d unique transform variants; warming AVIF + WebP for each.\n\n", len(paths))
// CheckRedirect => report 3xx as-is instead of following it, matching
// curl WITHOUT -L (the bash script would count a redirect as an error).
client := &http.Client{
Timeout: cfg.timeout,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
}
var st stats
for _, p := range paths {
url := cfg.domain + p
warm(client, cfg, "avif", acceptAVIF, url, &st)
warm(client, cfg, "webp", acceptWebP, url, &st)
}
fmt.Println()
fmt.Println("=== summary ===")
fmt.Println("requests :", st.total)
fmt.Printf("200 OK : %d (edge HIT: %d, generated/MISS: %d)\n", st.ok, st.hit, st.miss)
fmt.Println("errors :", st.errors)
// Non-zero exit if anything failed, so cron mail / logs flag it.
if st.errors > 0 {
os.Exit(1)
}
}