hugo-micropub/config.go

135 lines
3.9 KiB
Go

package main
import (
"errors"
"github.com/caarlos0/env/v6"
"log"
)
var (
BlogUrl string
IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget
SelectedStorage Storage
SelectedMediaStorage MediaStorage
SelectedCdn Cdn
SelectedSocials Socials
SelectedNotificationServices NotificationServices
MediaEndpointUrl string
)
type SyndicationTarget struct {
Uid string `json:"uid"`
Name string `json:"name"`
}
type config struct {
BlogUrl string `env:"BLOG_URL,required"`
BaseUrl string `env:"BASE_URL,required"`
MediaUrl string `env:"MEDIA_URL"`
GiteaEndpoint string `env:"GITEA_ENDPOINT"`
GiteaToken string `env:"GITEA_TOKEN"`
BunnyCdnKey string `env:"BUNNY_CDN_KEY"`
BunnyCdnStorageKey string `env:"BUNNY_CDN_STORAGE_KEY"`
BunnyCdnStorageName string `env:"BUNNY_CDN_STORAGE_NAME"`
MicroblogUrl string `env:"MICROBLOG_URL"`
MicroblogToken string `env:"MICROBLOG_TOKEN"`
TelegramUserId int64 `env:"TELEGRAM_USER_ID"`
TelegramBotToken string `env:"TELEGRAM_BOT_TOKEN"`
IgnoredWebmentionUrls []string `env:"WEBMENTION_IGNORED" envSeparator:","`
SyndicationTargets []string `env:"SYNDICATION" envSeparator:","`
}
func initConfig() (err error) {
cfg := config{}
if err := env.Parse(&cfg); err != nil {
return errors.New("failed to parse config, probably not all required env vars set")
}
// Blog URL (required)
BlogUrl = cfg.BlogUrl
// Media endpoint
MediaEndpointUrl = cfg.BaseUrl + "/media"
// Ignored Webmention URLs (optional)
IgnoredWebmentionUrls = cfg.IgnoredWebmentionUrls
// Syndication Targets (optional)
targets := make([]SyndicationTarget, 0)
for _, url := range cfg.SyndicationTargets {
targets = append(targets, SyndicationTarget{
Uid: url,
Name: url,
})
}
SyndicationTargets = targets
// Find selected storage
SelectedStorage = func() Storage {
// Gitea
if len(cfg.GiteaEndpoint) > 0 && len(cfg.GiteaToken) >= 0 {
return &Gitea{
endpoint: cfg.GiteaEndpoint,
token: cfg.GiteaToken,
}
}
return nil
}()
if SelectedStorage == nil {
return errors.New("no storage configured")
}
// Find selected media storage
SelectedMediaStorage = func() MediaStorage {
// BunnyCDN
if len(cfg.BunnyCdnStorageKey) > 0 && len(cfg.BunnyCdnStorageName) > 0 && len(cfg.MediaUrl) > 0 {
return &BunnyCdnStorage{
key: cfg.BunnyCdnStorageKey,
storageZoneName: cfg.BunnyCdnStorageName,
baseLocation: cfg.MediaUrl,
}
}
return nil
}()
if SelectedMediaStorage == nil {
return errors.New("no media storage configured")
}
// Find selected CDN (optional)
SelectedCdn = func() Cdn {
// BunnyCDN (optional)
if len(cfg.BunnyCdnKey) > 0 {
return &BunnyCdn{key: cfg.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
if len(cfg.MicroblogUrl) > 0 && len(cfg.MicroblogToken) > 0 {
socials = append(socials, &MicroblogPub{
url: cfg.MicroblogUrl,
token: cfg.MicroblogToken,
})
}
return socials
}()
if SelectedSocials == nil {
log.Println("no social networks configured")
}
// Find configured notification services (optional)
SelectedNotificationServices = func() NotificationServices {
var notificationServices []NotificationService = nil
// Telegram
if cfg.TelegramUserId > 0 && len(cfg.TelegramBotToken) > 0 {
notificationServices = append(notificationServices, &Telegram{
userId: cfg.TelegramUserId,
botToken: cfg.TelegramBotToken,
})
}
return notificationServices
}()
if SelectedNotificationServices == nil {
log.Println("No notification services configured")
}
return nil
}