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 SelectedSocials Socials 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"` 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:","` 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 { // 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 (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 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") } // 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 }