Skip to content

Commit

Permalink
port busted-urls to go
Browse files Browse the repository at this point in the history
  • Loading branch information
frioux committed Nov 19, 2022
1 parent f744d71 commit 89659c5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- run: sudo apt-get update --quiet && sudo apt-get install --quiet awscli s3cmd libdbd-sqlite3-perl libyaml-syck-perl
- run: sudo apt-get update --quiet && sudo apt-get install --quiet awscli s3cmd libdbd-sqlite3-perl libyaml-syck-perl golang-go
- run: curl -Ss -L https://github.com/gohugoio/hugo/releases/download/v0.49/hugo_0.49_Linux-64bit.tar.gz > /tmp/x.tgz
- run: cd /tmp && tar xvf /tmp/x.tgz && mv hugo hugo-0.49
- run: cp etc/s3cmd.ini ~/.s3cfg
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ build: clean

push: build
git push --quiet
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)
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)
55 changes: 0 additions & 55 deletions bin/busted-urls

This file was deleted.

96 changes: 96 additions & 0 deletions bin/busted-urls/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
)

const zoneID = "07b12080f7b57c1869b98fbceb028569"

var authKey, authEmail string

func init() {
authKey = os.Getenv("CF_AUTH_KEY")
if authKey == "" {
fmt.Fprintln(os.Stderr, "set CF_AUTH_KEY (https://api.cloudflare.com/#getting-started-requests)")
os.Exit(1)
}
authEmail = os.Getenv("CF_AUTH_EMAIL")
if authEmail == "" {
fmt.Fprintln(os.Stderr, "set CF_AUTH_EMAIL (https://api.cloudflare.com/#getting-started-requests)")
os.Exit(1)
}
}

func main() {
pat := regexp.MustCompile(`'s3://(\S+)'`)
a := make([]string, 0, 30)
r := bufio.NewScanner(os.Stdin)
for r.Scan() {
f := pat.FindStringSubmatch(r.Text())
if len(f) == 0 {
continue
}
url := f[1]
if strings.HasSuffix(url, "/index.html") {
url = strings.TrimSuffix(url, "index.html")
}

a = append(a, "https://" + url)

if len(a) == 30 {
purge(a)
a = a[:0]
}
}

if len(a) > 0 {
purge(a)
}
}

func purge(a []string) {
fmt.Fprintln(os.Stderr, "purging", a)

var in struct {
Files []string `json:"files"`
}
in.Files = a

b, err := json.Marshal(in)
if err != nil {
panic("couldn't encode json: " + err.Error())
}

req, err := http.NewRequest(
"POST", "https://api.cloudflare.com/client/v4/zones/"+zoneID+"/purge_cache",
bytes.NewReader(b),
)
if err != nil {
panic("couldn't create request: " + err.Error())
}

req.Header.Add("User-Agent", "foolish-cacheclear")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-Auth-Key", authKey)
req.Header.Add("X-Auth-Email", authEmail)

resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't bust cache: %s\n", err)
return
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
fmt.Fprintln(os.Stderr, resp.Status)
io.Copy(os.Stderr, resp.Body)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/frioux/blog

go 1.19

0 comments on commit 89659c5

Please sign in to comment.