-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare.go
42 lines (37 loc) · 952 Bytes
/
cloudflare.go
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
package main
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/cloudflare/cloudflare-go"
)
func invalidateCloudflare(data []*Cloudflare, wg *sync.WaitGroup, ch chan<- *RequestError) {
for _, v := range data {
go func(v *Cloudflare, wg *sync.WaitGroup) {
fmt.Println("Starting Cloudflare cache invalidation for", v.ZoneID)
defer wg.Done()
opt := cloudflare.HTTPClient(&http.Client{
Timeout: 60 * time.Second,
})
api, err := cloudflare.New(v.Key, v.Email, opt)
if err != nil {
ch <- newError(CF, v.ZoneID, err.Error())
return
}
if v.PurgeAll {
_, err := api.PurgeEverything(v.ZoneID)
if err != nil {
ch <- newError(CF, v.ZoneID, err.Error())
}
} else {
req := cloudflare.PurgeCacheRequest{}
req.Files = append(req.Files, v.Resources...)
_, err := api.PurgeCache(v.ZoneID, req)
if err != nil {
ch <- newError(CF, v.ZoneID, err.Error())
}
}
}(v, wg)
}
}