Refactor CDN code (enable adding more CDNs more easily)

kcoram/uplift
Jan-Lukas Else 2019-12-22 19:29:30 +01:00
parent c00f934658
commit 1182d9947b
4 changed files with 27 additions and 15 deletions

View File

@ -4,15 +4,22 @@ import (
"net/http" "net/http"
) )
func Purge(url string) { type Cdn interface {
if len(BunnyCdnKey) == 0 { // Purge url from CDN
// BunnyCdn deactivated Purge(url string)
return
} }
// BunnyCDN
type BunnyCdn struct {
// Access Key
key string
}
func (cdn BunnyCdn) Purge(url string) {
client := &http.Client{} client := &http.Client{}
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil) req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("AccessKey", BunnyCdnKey) req.Header.Add("AccessKey", cdn.key)
_, _ = client.Do(req) _, _ = client.Do(req)
} }

View File

@ -11,11 +11,11 @@ var (
BlogUrl string BlogUrl string
GiteaEndpoint string GiteaEndpoint string
GiteaToken string GiteaToken string
BunnyCdnKey string
MicroblogUrl string MicroblogUrl string
MicroblogToken string MicroblogToken string
IgnoredWebmentionUrls []string IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget SyndicationTargets []SyndicationTarget
SelectedCdn Cdn
) )
type SyndicationTarget struct { type SyndicationTarget struct {
@ -41,12 +41,6 @@ func init() {
log.Fatal(err) log.Fatal(err)
} }
GiteaToken = giteaToken GiteaToken = giteaToken
// BunnyCDN (optional)
bunnyCdnKey, err := bunnyCdnKey()
if err != nil {
log.Println(err)
}
BunnyCdnKey = bunnyCdnKey
// Microblog (optional) // Microblog (optional)
microblogUrl, err := microblogUrl() microblogUrl, err := microblogUrl()
if err != nil { if err != nil {
@ -70,6 +64,17 @@ func init() {
log.Println(err) log.Println(err)
} }
SyndicationTargets = syndicationTargets 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) { func giteaEndpoint() (string, error) {

View File

@ -83,7 +83,7 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
// Purge BunnyCDN in 10 seconds // Purge BunnyCDN in 10 seconds
go func() { go func() {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
Purge(location) SelectedCdn.Purge(location)
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
// Send webmentions // Send webmentions
go SendWebmentions(location) go SendWebmentions(location)

View File

@ -177,7 +177,7 @@ func returnSuccess(target string, w http.ResponseWriter, r *http.Request) {
// Purge BunnyCDN in 10 seconds // Purge BunnyCDN in 10 seconds
go func() { go func() {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
Purge(target) SelectedCdn.Purge(target)
}() }()
return return
} }