diff --git a/bunnycdn.go b/cdn.go similarity index 56% rename from bunnycdn.go rename to cdn.go index 6125be9..76d7074 100644 --- a/bunnycdn.go +++ b/cdn.go @@ -4,15 +4,22 @@ import ( "net/http" ) -func Purge(url string) { - if len(BunnyCdnKey) == 0 { - // BunnyCdn deactivated - return - } +type Cdn interface { + // Purge url from CDN + Purge(url string) +} + +// BunnyCDN +type BunnyCdn struct { + // Access Key + key string +} + +func (cdn BunnyCdn) Purge(url string) { 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", BunnyCdnKey) + req.Header.Add("AccessKey", cdn.key) _, _ = client.Do(req) } diff --git a/config.go b/config.go index aaa8635..152689c 100644 --- a/config.go +++ b/config.go @@ -11,11 +11,11 @@ var ( BlogUrl string GiteaEndpoint string GiteaToken string - BunnyCdnKey string MicroblogUrl string MicroblogToken string IgnoredWebmentionUrls []string SyndicationTargets []SyndicationTarget + SelectedCdn Cdn ) type SyndicationTarget struct { @@ -41,12 +41,6 @@ func init() { log.Fatal(err) } GiteaToken = giteaToken - // BunnyCDN (optional) - bunnyCdnKey, err := bunnyCdnKey() - if err != nil { - log.Println(err) - } - BunnyCdnKey = bunnyCdnKey // Microblog (optional) microblogUrl, err := microblogUrl() if err != nil { @@ -70,6 +64,17 @@ func init() { log.Println(err) } SyndicationTargets = syndicationTargets + // Find selected CDN (currently only BunnyCDN) + SelectedCdn = func() Cdn { + // BunnyCDN (optional) + bunnyCdnKey, err := bunnyCdnKey() + if err != nil { + log.Println(err) + } else { + return BunnyCdn{key: bunnyCdnKey} + } + return nil + }() } func giteaEndpoint() (string, error) { diff --git a/micropub.go b/micropub.go index afe4efc..24d2e4e 100644 --- a/micropub.go +++ b/micropub.go @@ -83,7 +83,7 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) { // Purge BunnyCDN in 10 seconds go func() { time.Sleep(10 * time.Second) - Purge(location) + SelectedCdn.Purge(location) time.Sleep(3 * time.Second) // Send webmentions go SendWebmentions(location) diff --git a/webmention.go b/webmention.go index 3774ed3..56f7129 100644 --- a/webmention.go +++ b/webmention.go @@ -177,7 +177,7 @@ func returnSuccess(target string, w http.ResponseWriter, r *http.Request) { // Purge BunnyCDN in 10 seconds go func() { time.Sleep(10 * time.Second) - Purge(target) + SelectedCdn.Purge(target) }() return } \ No newline at end of file