Skip to content

Commit 89659c5

Browse files
committed
port busted-urls to go
1 parent f744d71 commit 89659c5

5 files changed

Lines changed: 101 additions & 57 deletions

File tree

.github/workflows/cicd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
runs-on: ubuntu-20.04
66
steps:
77
- uses: actions/checkout@v2
8-
- run: sudo apt-get update --quiet && sudo apt-get install --quiet awscli s3cmd libdbd-sqlite3-perl libyaml-syck-perl
8+
- run: sudo apt-get update --quiet && sudo apt-get install --quiet awscli s3cmd libdbd-sqlite3-perl libyaml-syck-perl golang-go
99
- run: curl -Ss -L https://github.com/gohugoio/hugo/releases/download/v0.49/hugo_0.49_Linux-64bit.tar.gz > /tmp/x.tgz
1010
- run: cd /tmp && tar xvf /tmp/x.tgz && mv hugo hugo-0.49
1111
- run: cp etc/s3cmd.ini ~/.s3cfg

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ build: clean
1616

1717
push: build
1818
git push --quiet
19-
s3cmd sync --region us-west-2 --delete-removed --disable-multipart --no-preserve public/ s3://blog.afoolishmanifesto.com | tee $(log) && set-redirects && busted-urls $(log) && rm -f $(log)
19+
s3cmd sync --region us-west-2 --delete-removed --disable-multipart --no-preserve public/ s3://blog.afoolishmanifesto.com | tee $(log) && set-redirects && go run ./bin/busted-urls < $(log) && rm -f $(log)

bin/busted-urls

Lines changed: 0 additions & 55 deletions
This file was deleted.

bin/busted-urls/main.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
"os"
11+
"regexp"
12+
"strings"
13+
)
14+
15+
const zoneID = "07b12080f7b57c1869b98fbceb028569"
16+
17+
var authKey, authEmail string
18+
19+
func init() {
20+
authKey = os.Getenv("CF_AUTH_KEY")
21+
if authKey == "" {
22+
fmt.Fprintln(os.Stderr, "set CF_AUTH_KEY (https://api.cloudflare.com/#getting-started-requests)")
23+
os.Exit(1)
24+
}
25+
authEmail = os.Getenv("CF_AUTH_EMAIL")
26+
if authEmail == "" {
27+
fmt.Fprintln(os.Stderr, "set CF_AUTH_EMAIL (https://api.cloudflare.com/#getting-started-requests)")
28+
os.Exit(1)
29+
}
30+
}
31+
32+
func main() {
33+
pat := regexp.MustCompile(`'s3://(\S+)'`)
34+
a := make([]string, 0, 30)
35+
r := bufio.NewScanner(os.Stdin)
36+
for r.Scan() {
37+
f := pat.FindStringSubmatch(r.Text())
38+
if len(f) == 0 {
39+
continue
40+
}
41+
url := f[1]
42+
if strings.HasSuffix(url, "/index.html") {
43+
url = strings.TrimSuffix(url, "index.html")
44+
}
45+
46+
a = append(a, "https://" + url)
47+
48+
if len(a) == 30 {
49+
purge(a)
50+
a = a[:0]
51+
}
52+
}
53+
54+
if len(a) > 0 {
55+
purge(a)
56+
}
57+
}
58+
59+
func purge(a []string) {
60+
fmt.Fprintln(os.Stderr, "purging", a)
61+
62+
var in struct {
63+
Files []string `json:"files"`
64+
}
65+
in.Files = a
66+
67+
b, err := json.Marshal(in)
68+
if err != nil {
69+
panic("couldn't encode json: " + err.Error())
70+
}
71+
72+
req, err := http.NewRequest(
73+
"POST", "https://api.cloudflare.com/client/v4/zones/"+zoneID+"/purge_cache",
74+
bytes.NewReader(b),
75+
)
76+
if err != nil {
77+
panic("couldn't create request: " + err.Error())
78+
}
79+
80+
req.Header.Add("User-Agent", "foolish-cacheclear")
81+
req.Header.Add("Content-Type", "application/json")
82+
req.Header.Add("X-Auth-Key", authKey)
83+
req.Header.Add("X-Auth-Email", authEmail)
84+
85+
resp, err := http.DefaultClient.Do(req)
86+
if err != nil {
87+
fmt.Fprintf(os.Stderr, "couldn't bust cache: %s\n", err)
88+
return
89+
}
90+
defer resp.Body.Close()
91+
92+
if resp.StatusCode != 200 {
93+
fmt.Fprintln(os.Stderr, resp.Status)
94+
io.Copy(os.Stderr, resp.Body)
95+
}
96+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/frioux/blog
2+
3+
go 1.19

0 commit comments

Comments
 (0)