package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/hook", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Wrong HTTP method", http.StatusMethodNotAllowed) return } bunnyCdnKey, keyPresent := r.URL.Query()["key"] if !keyPresent || len(bunnyCdnKey[0]) < 1 { http.Error(w, "Wrong HTTP method", http.StatusBadRequest) return } feeds, feedsPresent := r.URL.Query()["feed"] urls, urlsPresent := r.URL.Query()["url"] go func() { var allUrls []string if feedsPresent { for _, feed := range feeds { article, err := LatestArticle(feed) if err != nil { fmt.Println(err.Error()) continue } if article.Url != "" { allUrls = append(allUrls, article.Url) } } } if urlsPresent { for _, url := range urls { allUrls = append(allUrls, url) } } purge(allUrls, bunnyCdnKey[0]) }() }) log.Fatal(http.ListenAndServe(":8080", nil)) } func purge(urls []string, bunnyCdnKey string) { for _, url := range urls { fmt.Println("Purge:", url) client := http.DefaultClient req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil) req.Header.Add("User-Agent", "BunnyPurge") req.Header.Add("Content-Type", "application/json") req.Header.Add("Accept", "application/json") req.Header.Add("AccessKey", bunnyCdnKey) resp, err := client.Do(req) if err != nil || resp.StatusCode != 200 { fmt.Println("Failed to purge", url) } } }