Refactor CDN code (enable adding more CDNs more easily)
parent
c00f934658
commit
1182d9947b
|
@ -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)
|
||||
}
|
19
config.go
19
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) {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue