2019-11-07 10:00:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-01-01 12:44:44 +00:00
|
|
|
"github.com/caarlos0/env/v6"
|
2019-12-06 09:32:30 +00:00
|
|
|
"log"
|
2020-01-12 17:17:40 +00:00
|
|
|
"strings"
|
2019-11-07 10:00:24 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 09:32:30 +00:00
|
|
|
var (
|
2019-12-23 09:21:21 +00:00
|
|
|
BlogUrl string
|
|
|
|
IgnoredWebmentionUrls []string
|
|
|
|
SyndicationTargets []SyndicationTarget
|
|
|
|
SelectedStorage Storage
|
2020-01-01 12:44:44 +00:00
|
|
|
SelectedMediaStorage MediaStorage
|
2019-12-23 09:21:21 +00:00
|
|
|
SelectedCdn Cdn
|
|
|
|
SelectedSocials Socials
|
|
|
|
SelectedNotificationServices NotificationServices
|
2020-01-01 12:44:44 +00:00
|
|
|
MediaEndpointUrl string
|
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"`
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:44:44 +00:00
|
|
|
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")
|
2019-12-06 09:32:30 +00:00
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
// Blog URL (required)
|
2020-01-12 17:17:40 +00:00
|
|
|
if !strings.HasSuffix(cfg.BlogUrl, "/") {
|
|
|
|
return errors.New("missing trailing slash in BLOG_URL")
|
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
BlogUrl = cfg.BlogUrl
|
|
|
|
// Media endpoint
|
2020-01-12 17:17:40 +00:00
|
|
|
if !strings.HasSuffix(cfg.BaseUrl, "/") {
|
|
|
|
return errors.New("missing trailing slash in BASE_URL")
|
|
|
|
}
|
|
|
|
MediaEndpointUrl = cfg.BaseUrl + "media"
|
2019-12-06 09:32:30 +00:00
|
|
|
// Ignored Webmention URLs (optional)
|
2020-01-01 12:44:44 +00:00
|
|
|
IgnoredWebmentionUrls = cfg.IgnoredWebmentionUrls
|
2019-12-22 12:58:50 +00:00
|
|
|
// Syndication Targets (optional)
|
2020-01-01 12:44:44 +00:00
|
|
|
targets := make([]SyndicationTarget, 0)
|
|
|
|
for _, url := range cfg.SyndicationTargets {
|
|
|
|
targets = append(targets, SyndicationTarget{
|
|
|
|
Uid: url,
|
|
|
|
Name: url,
|
|
|
|
})
|
2019-12-22 12:58:50 +00:00
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
SyndicationTargets = targets
|
2019-12-22 20:17:11 +00:00
|
|
|
// Find selected storage
|
|
|
|
SelectedStorage = func() Storage {
|
|
|
|
// Gitea
|
2020-01-01 12:44:44 +00:00
|
|
|
if len(cfg.GiteaEndpoint) > 0 && len(cfg.GiteaToken) >= 0 {
|
2019-12-22 20:17:11 +00:00
|
|
|
return &Gitea{
|
2020-01-01 12:44:44 +00:00
|
|
|
endpoint: cfg.GiteaEndpoint,
|
|
|
|
token: cfg.GiteaToken,
|
2019-12-22 20:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if SelectedStorage == nil {
|
2020-01-01 12:44:44 +00:00
|
|
|
return errors.New("no storage configured")
|
|
|
|
}
|
2020-01-12 16:48:05 +00:00
|
|
|
// Find selected media storage (Optional)
|
2020-01-01 12:44:44 +00:00
|
|
|
SelectedMediaStorage = func() MediaStorage {
|
|
|
|
// BunnyCDN
|
2020-01-12 17:17:40 +00:00
|
|
|
// MEDIA_URL needs trailing slash too
|
|
|
|
if len(cfg.BunnyCdnStorageKey) > 0 && len(cfg.BunnyCdnStorageName) > 0 && len(cfg.MediaUrl) > 0 && strings.HasSuffix(cfg.MediaUrl, "/") {
|
2020-01-01 12:44:44 +00:00
|
|
|
return &BunnyCdnStorage{
|
|
|
|
key: cfg.BunnyCdnStorageKey,
|
|
|
|
storageZoneName: cfg.BunnyCdnStorageName,
|
|
|
|
baseLocation: cfg.MediaUrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if SelectedMediaStorage == nil {
|
2020-01-12 16:48:05 +00:00
|
|
|
log.Println("no media storage configured")
|
2019-12-22 20:17:11 +00:00
|
|
|
}
|
|
|
|
// Find selected CDN (optional)
|
2019-12-22 18:29:30 +00:00
|
|
|
SelectedCdn = func() Cdn {
|
2020-01-12 16:48:05 +00:00
|
|
|
// BunnyCDN
|
2020-01-01 12:44:44 +00:00
|
|
|
if len(cfg.BunnyCdnKey) > 0 {
|
|
|
|
return &BunnyCdn{key: cfg.BunnyCdnKey}
|
2019-12-22 18:29:30 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
2019-12-22 20:17:11 +00:00
|
|
|
if SelectedCdn == nil {
|
2020-01-01 12:44:44 +00:00
|
|
|
log.Println("no CDN configured")
|
2019-12-22 20:17:11 +00:00
|
|
|
}
|
|
|
|
// 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
|
2020-01-01 12:44:44 +00:00
|
|
|
if len(cfg.MicroblogUrl) > 0 && len(cfg.MicroblogToken) > 0 {
|
2019-12-22 19:27:12 +00:00
|
|
|
socials = append(socials, &MicroblogPub{
|
2020-01-01 12:44:44 +00:00
|
|
|
url: cfg.MicroblogUrl,
|
|
|
|
token: cfg.MicroblogToken,
|
2019-12-22 19:27:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return socials
|
|
|
|
}()
|
2019-12-22 20:17:11 +00:00
|
|
|
if SelectedSocials == nil {
|
2020-01-01 12:44:44 +00:00
|
|
|
log.Println("no social networks configured")
|
2019-12-22 20:17:11 +00:00
|
|
|
}
|
2019-12-23 09:21:21 +00:00
|
|
|
// Find configured notification services (optional)
|
|
|
|
SelectedNotificationServices = func() NotificationServices {
|
|
|
|
var notificationServices []NotificationService = nil
|
|
|
|
// Telegram
|
2020-01-01 12:44:44 +00:00
|
|
|
if cfg.TelegramUserId > 0 && len(cfg.TelegramBotToken) > 0 {
|
2019-12-23 09:21:21 +00:00
|
|
|
notificationServices = append(notificationServices, &Telegram{
|
2020-01-01 12:44:44 +00:00
|
|
|
userId: cfg.TelegramUserId,
|
|
|
|
botToken: cfg.TelegramBotToken,
|
2019-12-23 09:21:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return notificationServices
|
|
|
|
}()
|
|
|
|
if SelectedNotificationServices == nil {
|
|
|
|
log.Println("No notification services configured")
|
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
return nil
|
2019-12-22 12:58:50 +00:00
|
|
|
}
|