Added cloudflare cache invalidation

This commit is contained in:
Keith Morrow 2015-06-11 07:42:44 -04:00
parent f2ea9239f8
commit 05991e4ec3

View file

@ -9,6 +9,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"sync"
@ -37,6 +38,7 @@ type Config struct {
Token string `json:"token"`
Email string `json:"email"`
Domain string `json:"domain"`
Url string `json:"url"`
} `json:"cloudflare-cache-invalidate"`
}
@ -177,16 +179,46 @@ func delfile(w http.ResponseWriter, r *http.Request) {
return
}
if config.CfCacheInvalidate.Enabled {
if config.Http.Enabled {
cfInvalidate(ident, false)
}
if config.Https.Enabled {
cfInvalidate(ident, true)
}
}
os.Remove(identPath)
http.Redirect(w, r, "/", 301)
}
func cfInvalidate(ident string, https bool) {
var invUrl string
if https {
invUrl = "https://" + config.CfCacheInvalidate.Url
} else {
invUrl = "http://" + config.CfCacheInvalidate.Url
}
invUrl += "/i/" + ident
if _, err := http.PostForm("https://www.cloudflare.com/api_json.html", url.Values{
"a": {"zone_file_purge"},
"tkn": {config.CfCacheInvalidate.Token},
"email": {config.CfCacheInvalidate.Email},
"z": {config.CfCacheInvalidate.Domain},
"url": {invUrl},
}); err != nil {
log.Printf("Cache invalidate failed for '%s': '%s'", ident, err.Error())
}
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/up", upload)
http.HandleFunc("/del", delfile)
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
http.Handle("/i/", http.StripPrefix("/i", http.FileServer(http.Dir("i"))))
config = readConfig()
validateConfig(config)