You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

70 lines
1.5 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. )
  7. func main() {
  8. http.HandleFunc("/hook", func(w http.ResponseWriter, r *http.Request) {
  9. if r.Method != http.MethodPost {
  10. http.Error(w, "Wrong HTTP method", http.StatusMethodNotAllowed)
  11. return
  12. }
  13. bunnyCdnKey, keyPresent := r.URL.Query()["key"]
  14. if !keyPresent || len(bunnyCdnKey[0]) < 1 {
  15. http.Error(w, "Wrong HTTP method", http.StatusBadRequest)
  16. return
  17. }
  18. feeds, feedsPresent := r.URL.Query()["feed"]
  19. urls, urlsPresent := r.URL.Query()["url"]
  20. go func() {
  21. var allUrls []string
  22. if feedsPresent {
  23. for _, feed := range feeds {
  24. article, err := LatestArticle(feed)
  25. if err != nil {
  26. fmt.Println(err.Error())
  27. continue
  28. }
  29. if article.Url != "" {
  30. allUrls = append(allUrls, article.Url)
  31. }
  32. }
  33. }
  34. if urlsPresent {
  35. for _, url := range urls {
  36. allUrls = append(allUrls, url)
  37. }
  38. }
  39. purge(allUrls, bunnyCdnKey[0])
  40. }()
  41. })
  42. log.Fatal(http.ListenAndServe(":8080", nil))
  43. }
  44. func purge(urls []string, bunnyCdnKey string) {
  45. for _, url := range urls {
  46. fmt.Println("Purge:", url)
  47. client := http.DefaultClient
  48. req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
  49. req.Header.Add("User-Agent", "BunnyPurge")
  50. req.Header.Add("Content-Type", "application/json")
  51. req.Header.Add("Accept", "application/json")
  52. req.Header.Add("AccessKey", bunnyCdnKey)
  53. resp, err := client.Do(req)
  54. if err != nil || resp.StatusCode != 200 {
  55. fmt.Println("Failed to purge", url)
  56. }
  57. }
  58. }