hugo-micropub/config.go

160 lines
3.9 KiB
Go

package main
import (
"errors"
"log"
"os"
"strings"
)
var (
BlogUrl string
IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget
SelectedStorage Storage
SelectedCdn Cdn
SelectedSocials Socials
)
type SyndicationTarget struct {
Uid string `json:"uid"`
Name string `json:"name"`
}
func init() {
// Blog URL (required)
blogUrl, err := blogUrl()
if err != nil {
log.Fatal(err)
}
BlogUrl = blogUrl
// Ignored Webmention URLs (optional)
ignoredWebmentionUrls, err := ignoredWebmentionUrls()
if err != nil {
log.Println(err)
}
IgnoredWebmentionUrls = ignoredWebmentionUrls
// Syndication Targets (optional)
syndicationTargets, err := syndicationTargets()
if err != nil {
log.Println(err)
}
SyndicationTargets = syndicationTargets
// Find selected storage
SelectedStorage = func() Storage {
// Gitea
giteaEndpoint, err1 := giteaEndpoint()
giteaToken, err2 := giteaToken()
if err1 == nil && err2 == nil {
return &Gitea{
endpoint: giteaEndpoint,
token: giteaToken,
}
}
return nil
}()
if SelectedStorage == nil {
log.Fatal("No storage configured")
}
// Find selected CDN (optional)
SelectedCdn = func() Cdn {
// BunnyCDN (optional)
bunnyCdnKey, err := bunnyCdnKey()
if err == nil {
return &BunnyCdn{key: bunnyCdnKey}
}
return nil
}()
if SelectedCdn == nil {
log.Println("No CDN configured")
}
// Find configured social networks (optional)
SelectedSocials = func() Socials {
var socials []Social = nil
// Microblog.pub
microblogUrl, err1 := microblogUrl()
microblogToken, err2 := microblogToken()
if err1 == nil && err2 == nil {
socials = append(socials, &MicroblogPub{
url: microblogUrl,
token: microblogToken,
})
}
return socials
}()
if SelectedSocials == nil {
log.Println("No social networks configured")
}
}
func giteaEndpoint() (string, error) {
giteaEndpoint := os.Getenv("GITEA_ENDPOINT")
if len(giteaEndpoint) == 0 || giteaEndpoint == "" {
return "", errors.New("GITEA_ENDPOINT not specified")
}
return giteaEndpoint, nil
}
func giteaToken() (string, error) {
giteaToken := os.Getenv("GITEA_TOKEN")
if len(giteaToken) == 0 || giteaToken == "" {
return "", errors.New("GITEA_TOKEN not specified")
}
return giteaToken, nil
}
func blogUrl() (string, error) {
blogURL := os.Getenv("BLOG_URL")
if len(blogURL) == 0 || blogURL == "" {
return "", errors.New("BLOG_URL not specified")
}
return blogURL, nil
}
func bunnyCdnKey() (string, error) {
bunnyCDNKey := os.Getenv("BUNNY_CDN_KEY")
if len(bunnyCDNKey) == 0 || bunnyCDNKey == "" {
return "", errors.New("BUNNY_CDN_KEY not specified, BunnyCDN features are deactivated")
}
return bunnyCDNKey, nil
}
func microblogUrl() (string, error) {
microblogUrl := os.Getenv("MICROBLOG_URL")
if len(microblogUrl) == 0 || microblogUrl == "" {
return "", errors.New("MICROBLOG_URL not specified, microblog.pub features are deactivated")
}
return microblogUrl, nil
}
func microblogToken() (string, error) {
microblogToken := os.Getenv("MICROBLOG_TOKEN")
if len(microblogToken) == 0 || microblogToken == "" {
return "", errors.New("MICROBLOG_TOKEN not specified, microblog.pub features are deactivated")
}
return microblogToken, nil
}
func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
if len(webmentionIgnored) == 0 {
return make([]string, 0), errors.New("WEBMENTION_IGNORED not set, no URLs are ignored on Webmention sending")
}
return strings.Split(webmentionIgnored, ","), nil
}
func syndicationTargets() ([]SyndicationTarget, error) {
syndication := os.Getenv("SYNDICATION")
targets := make([]SyndicationTarget, 0)
if len(syndication) == 0 {
return targets, errors.New("SYNDICATION not set, no targets are returned when querying for syndication targets")
}
for _, url := range strings.Split(syndication, ",") {
targets = append(targets, SyndicationTarget{
Uid: url,
Name: url,
})
}
return targets, nil
}