Purge BunnyCDN 10 seconds after published
parent
6b41fcfb09
commit
7795fa171d
|
@ -0,0 +1,18 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Purge(url string) {
|
||||||
|
accessKey, err := GetBunnyCDNKey()
|
||||||
|
if err != nil || len(accessKey) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
client := &http.Client{}
|
||||||
|
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
req.Header.Add("Accept", "application/json")
|
||||||
|
req.Header.Add("AccessKey", accessKey)
|
||||||
|
_, _ = client.Do(req)
|
||||||
|
}
|
|
@ -28,3 +28,12 @@ func GetBlogURL() (string, error) {
|
||||||
}
|
}
|
||||||
return blogURL, nil
|
return blogURL, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func GetBunnyCDNKey() (string, error) {
|
||||||
|
bunnyCDNKey := os.Getenv("BUNNY_CDN_KEY")
|
||||||
|
if len(bunnyCDNKey) == 0 || bunnyCDNKey == "" {
|
||||||
|
return "", errors.New("BUNNY_CDN_KEY not specified")
|
||||||
|
}
|
||||||
|
return bunnyCDNKey, nil
|
||||||
|
}
|
||||||
|
|
5
main.go
5
main.go
|
@ -54,6 +54,11 @@ func handleMicroPub(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
w.Header().Add("Location", location)
|
w.Header().Add("Location", location)
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
// Purge BunnyCDN in 10 seconds
|
||||||
|
go func() {
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
Purge(location)
|
||||||
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue