hugo-micropub/config.go

196 lines
5.3 KiB
Go
Raw Normal View History

2019-11-07 10:00:24 +00:00
package main
import (
"errors"
2019-12-06 09:32:30 +00:00
"log"
2019-11-07 10:00:24 +00:00
"os"
"strconv"
2019-12-06 09:32:30 +00:00
"strings"
2019-11-07 10:00:24 +00:00
)
2019-12-06 09:32:30 +00:00
var (
BlogUrl string
IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget
SelectedStorage Storage
SelectedCdn Cdn
SelectedSocials Socials
SelectedNotificationServices NotificationServices
2019-12-06 09:32:30 +00:00
)
2019-12-22 12:58:50 +00:00
type SyndicationTarget struct {
Uid string `json:"uid"`
Name string `json:"name"`
}
2019-12-06 09:32:30 +00:00
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
2019-12-22 12:58:50 +00:00
// Syndication Targets (optional)
syndicationTargets, err := syndicationTargets()
if err != nil {
log.Println(err)
}
SyndicationTargets = syndicationTargets
2019-12-22 20:17:11 +00:00
// 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()
2019-12-22 19:27:12 +00:00
if err == nil {
return &BunnyCdn{key: bunnyCdnKey}
}
return nil
}()
2019-12-22 20:17:11 +00:00
if SelectedCdn == nil {
log.Println("No CDN configured")
}
// Find configured social networks (optional)
2019-12-22 19:27:12 +00:00
SelectedSocials = func() Socials {
2019-12-22 20:17:11 +00:00
var socials []Social = nil
2019-12-22 19:27:12 +00:00
// Microblog.pub
microblogUrl, err1 := microblogUrl()
microblogToken, err2 := microblogToken()
if err1 == nil && err2 == nil {
socials = append(socials, &MicroblogPub{
url: microblogUrl,
token: microblogToken,
})
}
return socials
}()
2019-12-22 20:17:11 +00:00
if SelectedSocials == nil {
log.Println("No social networks configured")
}
// Find configured notification services (optional)
SelectedNotificationServices = func() NotificationServices {
var notificationServices []NotificationService = nil
// Telegram
telegramUserId, err1 := telegramUserId()
telegramBotToken, err2 := telegramBotToken()
if err1 == nil && err2 == nil {
notificationServices = append(notificationServices, &Telegram{
userId: telegramUserId,
botToken: telegramBotToken,
})
}
return notificationServices
}()
if SelectedNotificationServices == nil {
log.Println("No notification services configured")
}
2019-12-06 09:32:30 +00:00
}
func giteaEndpoint() (string, error) {
2019-11-07 10:00:24 +00:00
giteaEndpoint := os.Getenv("GITEA_ENDPOINT")
if len(giteaEndpoint) == 0 || giteaEndpoint == "" {
return "", errors.New("GITEA_ENDPOINT not specified")
}
return giteaEndpoint, nil
}
2019-12-06 09:32:30 +00:00
func giteaToken() (string, error) {
2019-11-07 10:00:24 +00:00
giteaToken := os.Getenv("GITEA_TOKEN")
if len(giteaToken) == 0 || giteaToken == "" {
return "", errors.New("GITEA_TOKEN not specified")
}
return giteaToken, nil
}
2019-12-06 09:32:30 +00:00
func blogUrl() (string, error) {
2019-11-07 10:00:24 +00:00
blogURL := os.Getenv("BLOG_URL")
if len(blogURL) == 0 || blogURL == "" {
return "", errors.New("BLOG_URL not specified")
}
return blogURL, nil
}
2019-12-06 09:32:30 +00:00
func bunnyCdnKey() (string, error) {
bunnyCDNKey := os.Getenv("BUNNY_CDN_KEY")
if len(bunnyCDNKey) == 0 || bunnyCDNKey == "" {
2019-12-06 09:32:30 +00:00
return "", errors.New("BUNNY_CDN_KEY not specified, BunnyCDN features are deactivated")
}
return bunnyCDNKey, nil
}
2019-12-06 09:32:30 +00:00
2019-12-12 21:36:05 +00:00
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 telegramUserId() (int64, error) {
telegramUserIdString := os.Getenv("TELEGRAM_USER_ID")
telegramUserId, err := strconv.ParseInt(telegramUserIdString, 10, 64)
if err != nil || len(telegramUserIdString) == 0 || telegramUserIdString == "" {
return 0, errors.New("TELEGRAM_USER_ID not specified, Telegram features are deactivated")
}
return telegramUserId, nil
}
func telegramBotToken() (string, error) {
telegramBotToken := os.Getenv("TELEGRAM_BOT_TOKEN")
if len(telegramBotToken) == 0 || telegramBotToken == "" {
return "", errors.New("TELEGRAM_BOT_TOKEN not specified, Telegram features are deactivated")
}
return telegramBotToken, nil
}
2019-12-06 09:32:30 +00:00
func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
if len(webmentionIgnored) == 0 {
2019-12-22 12:58:50 +00:00
return make([]string, 0), errors.New("WEBMENTION_IGNORED not set, no URLs are ignored on Webmention sending")
2019-12-06 09:32:30 +00:00
}
return strings.Split(webmentionIgnored, ","), nil
2019-12-22 12:58:50 +00:00
}
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
}