jlelse
/
hugo-micropub
Archived
1
Fork 0
This repository has been archived on 2020-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
hugo-micropub/config.go

148 lines
4.5 KiB
Go

package main
import (
"errors"
"github.com/caarlos0/env/v6"
"log"
"strings"
)
var (
BlogUrl string
MediaEndpointUrl string
IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget
SelectedStorage Storage
SelectedMediaStorage MediaStorage
SelectedCdn Cdn
SelectedNotificationServices NotificationServices
SelectedImageCompression ImageCompression
)
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"`
GitFilepath string `env:"GIT_FILEPATH" envDefault:"/tmp/micropubrepo"`
GitUrl string `env:"GIT_URL"`
GitUsername string `env:"GIT_USERNAME"`
GitPassword string `env:"GIT_PASSWORD"`
GitAuthorName string `env:"GIT_AUTHOR_NAME" envDefault:"hugo-micropub"`
GitAuthorEmail string `env:"GIT_AUTHOR_EMAIL" envDefault:"hugo-micropub@example.com"`
BunnyCdnKey string `env:"BUNNY_CDN_KEY"`
BunnyCdnStorageKey string `env:"BUNNY_CDN_STORAGE_KEY"`
BunnyCdnStorageName string `env:"BUNNY_CDN_STORAGE_NAME"`
TelegramUserId int `env:"TELEGRAM_USER_ID"`
TelegramBotToken string `env:"TELEGRAM_BOT_TOKEN"`
IgnoredWebmentionUrls []string `env:"WEBMENTION_IGNORED" envSeparator:","`
SyndicationTargets []string `env:"SYNDICATION" envSeparator:","`
TinifyKey string `env:"TINIFY_KEY"`
}
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)
if !strings.HasSuffix(cfg.BlogUrl, "/") {
return errors.New("missing trailing slash in BLOG_URL")
}
BlogUrl = cfg.BlogUrl
// Media endpoint
if !strings.HasSuffix(cfg.BaseUrl, "/") {
return errors.New("missing trailing slash in BASE_URL")
}
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 {
// Git
if len(cfg.GitFilepath) > 0 && len(cfg.GitUrl) > 0 && len(cfg.GitUsername) > 0 && len(cfg.GitPassword) > 0 && len(cfg.GitAuthorName) > 0 && len(cfg.GitAuthorEmail) > 0 {
return &Git{
filepath: cfg.GitFilepath,
url: cfg.GitUrl,
username: cfg.GitUsername,
password: cfg.GitPassword,
name: cfg.GitAuthorName,
email: cfg.GitAuthorEmail,
}
}
return nil
}()
if SelectedStorage == nil {
return errors.New("no storage configured")
}
// Find selected media storage (Optional)
SelectedMediaStorage = func() MediaStorage {
// BunnyCDN
// MEDIA_URL needs trailing slash too
if len(cfg.BunnyCdnStorageKey) > 0 && len(cfg.BunnyCdnStorageName) > 0 && len(cfg.MediaUrl) > 0 && strings.HasSuffix(cfg.MediaUrl, "/") {
return &BunnyCdnStorage{
key: cfg.BunnyCdnStorageKey,
storageZoneName: cfg.BunnyCdnStorageName,
baseLocation: cfg.MediaUrl,
}
}
return nil
}()
if SelectedMediaStorage == nil {
log.Println("no media storage configured")
}
// Find selected CDN (optional)
SelectedCdn = func() Cdn {
// BunnyCDN
if len(cfg.BunnyCdnKey) > 0 {
return &BunnyCdn{key: cfg.BunnyCdnKey}
}
return nil
}()
if SelectedCdn == nil {
log.Println("no CDN 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")
}
// Find configured image compression service (optional)
SelectedImageCompression = func() ImageCompression {
// Tinify
if len(cfg.TinifyKey) > 0 {
return &Tinify{
key: cfg.TinifyKey,
}
}
return nil
}()
if SelectedImageCompression == nil {
log.Println("no image compression configured")
}
return nil
}