jlelse
/
hugo-micropub
Archived
1
Fork 0

Purge BunnyCDN 10 seconds after published

This commit is contained in:
Jan-Lukas Else 2019-11-09 17:35:54 +01:00
parent 6b41fcfb09
commit 7795fa171d
3 changed files with 32 additions and 0 deletions

18
bunnycdn.go Normal file
View File

@ -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)
}

View File

@ -28,3 +28,12 @@ func GetBlogURL() (string, error) {
}
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
}

View File

@ -54,6 +54,11 @@ func handleMicroPub(w http.ResponseWriter, r *http.Request) {
} else {
w.Header().Add("Location", location)
w.WriteHeader(http.StatusAccepted)
// Purge BunnyCDN in 10 seconds
go func() {
time.Sleep(10 * time.Second)
Purge(location)
}()
return
}
} else {