diff --git a/cdn.go b/cdn.go index 76d7074..5acf9b4 100644 --- a/cdn.go +++ b/cdn.go @@ -15,7 +15,7 @@ type BunnyCdn struct { key string } -func (cdn BunnyCdn) Purge(url 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") diff --git a/config.go b/config.go index 152689c..9d0d815 100644 --- a/config.go +++ b/config.go @@ -11,11 +11,10 @@ var ( BlogUrl string GiteaEndpoint string GiteaToken string - MicroblogUrl string - MicroblogToken string IgnoredWebmentionUrls []string SyndicationTargets []SyndicationTarget SelectedCdn Cdn + SelectedSocials Socials ) type SyndicationTarget struct { @@ -41,17 +40,6 @@ func init() { log.Fatal(err) } GiteaToken = giteaToken - // Microblog (optional) - microblogUrl, err := microblogUrl() - if err != nil { - log.Println(err) - } - MicroblogUrl = microblogUrl - microblogToken, err := microblogToken() - if err != nil { - log.Println(err) - } - MicroblogToken = microblogToken // Ignored Webmention URLs (optional) ignoredWebmentionUrls, err := ignoredWebmentionUrls() if err != nil { @@ -68,13 +56,25 @@ func init() { SelectedCdn = func() Cdn { // BunnyCDN (optional) bunnyCdnKey, err := bunnyCdnKey() - if err != nil { - log.Println(err) - } else { - return BunnyCdn{key: bunnyCdnKey} + if err == nil { + return &BunnyCdn{key: bunnyCdnKey} } return nil }() + // Find configured social networks + SelectedSocials = func() Socials { + var socials []Social + // Microblog.pub + microblogUrl, err1 := microblogUrl() + microblogToken, err2 := microblogToken() + if err1 == nil && err2 == nil { + socials = append(socials, &MicroblogPub{ + url: microblogUrl, + token: microblogToken, + }) + } + return socials + }() } func giteaEndpoint() (string, error) { diff --git a/microblogpub.go b/microblogpub.go deleted file mode 100644 index bba126b..0000000 --- a/microblogpub.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "net/http" -) - -func PostToMicroblog(location string, text string) { - if len(MicroblogUrl) == 0 || len(MicroblogToken) == 0 { - // Microblog.pub deactivated - return - } - if len(text) == 0 { - text = location - } - note := &struct { - Content string `json:"content"` - }{ - Content: "[" + text + "](" + location + ")", - } - bytesRepresentation, err := json.Marshal(note) - if err != nil { - return - } - client := &http.Client{} - req, _ := http.NewRequest("POST", MicroblogUrl+"api/new_note", bytes.NewBuffer(bytesRepresentation)) - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", "Bearer "+MicroblogToken) - _, _ = client.Do(req) -} diff --git a/micropub.go b/micropub.go index 24d2e4e..21a63cd 100644 --- a/micropub.go +++ b/micropub.go @@ -87,7 +87,7 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) { time.Sleep(3 * time.Second) // Send webmentions go SendWebmentions(location) - go PostToMicroblog(location, entry.title) + go SelectedSocials.Post(location, entry.title) }() return } diff --git a/social.go b/social.go new file mode 100644 index 0000000..589aab3 --- /dev/null +++ b/social.go @@ -0,0 +1,46 @@ +package main + +import ( + "bytes" + "encoding/json" + "net/http" +) + +type Social interface { + Post(location string, text string) +} + +type Socials []Social + +// Post to all socials +func (socials *Socials) Post(location string, text string) { + for _, social := range *socials { + social.Post(location, text) + } +} + +// Microblog.pub +type MicroblogPub struct { + url string + token string +} + +func (social *MicroblogPub) Post(location string, text string) { + if len(text) == 0 { + text = location + } + note := &struct { + Content string `json:"content"` + }{ + Content: "[" + text + "](" + location + ")", + } + bytesRepresentation, err := json.Marshal(note) + if err != nil { + return + } + client := &http.Client{} + req, _ := http.NewRequest("POST", social.url+"api/new_note", bytes.NewBuffer(bytesRepresentation)) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+social.token) + _, _ = client.Do(req) +}