2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-06 17:07:48 +00:00
|
|
|
"errors"
|
2021-06-17 14:34:57 +00:00
|
|
|
"log"
|
2020-12-24 10:00:16 +00:00
|
|
|
"net/url"
|
2020-10-14 19:20:17 +00:00
|
|
|
"strings"
|
2020-10-06 17:07:48 +00:00
|
|
|
|
2020-07-28 19:17:07 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2020-10-15 18:54:43 +00:00
|
|
|
Server *configServer `mapstructure:"server"`
|
|
|
|
Db *configDb `mapstructure:"database"`
|
|
|
|
Cache *configCache `mapstructure:"cache"`
|
|
|
|
DefaultBlog string `mapstructure:"defaultblog"`
|
|
|
|
Blogs map[string]*configBlog `mapstructure:"blogs"`
|
|
|
|
User *configUser `mapstructure:"user"`
|
|
|
|
Hooks *configHooks `mapstructure:"hooks"`
|
|
|
|
Micropub *configMicropub `mapstructure:"micropub"`
|
|
|
|
PathRedirects []*configRegexRedirect `mapstructure:"pathRedirects"`
|
2020-10-26 16:37:31 +00:00
|
|
|
ActivityPub *configActivityPub `mapstructure:"activityPub"`
|
2021-04-23 17:36:57 +00:00
|
|
|
Webmention *configWebmention `mapstructure:"webmention"`
|
2020-11-06 17:45:31 +00:00
|
|
|
Notifications *configNotifications `mapstructure:"notifications"`
|
2021-02-27 07:31:06 +00:00
|
|
|
PrivateMode *configPrivateMode `mapstructure:"privateMode"`
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type configServer struct {
|
2021-02-16 20:27:52 +00:00
|
|
|
Logging bool `mapstructure:"logging"`
|
|
|
|
LogFile string `mapstructure:"logFile"`
|
|
|
|
Port int `mapstructure:"port"`
|
|
|
|
PublicAddress string `mapstructure:"publicAddress"`
|
|
|
|
ShortPublicAddress string `mapstructure:"shortPublicAddress"`
|
|
|
|
PublicHTTPS bool `mapstructure:"publicHttps"`
|
2021-03-19 09:10:47 +00:00
|
|
|
Tor bool `mapstructure:"tor"`
|
2021-02-16 20:27:52 +00:00
|
|
|
SecurityHeaders bool `mapstructure:"securityHeaders"`
|
|
|
|
CSPDomains []string `mapstructure:"cspDomains"`
|
|
|
|
LetsEncryptMail string `mapstructure:"letsEncryptMail"`
|
|
|
|
JWTSecret string `mapstructure:"jwtSecret"`
|
2020-12-24 10:00:16 +00:00
|
|
|
publicHostname string
|
|
|
|
shortPublicHostname string
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type configDb struct {
|
2021-03-26 08:33:46 +00:00
|
|
|
File string `mapstructure:"file"`
|
|
|
|
DumpFile string `mapstructure:"dumpFile"`
|
2021-06-29 15:07:08 +00:00
|
|
|
Debug bool `mapstructure:"debug"`
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 20:45:26 +00:00
|
|
|
type configCache struct {
|
2020-11-20 14:33:20 +00:00
|
|
|
Enable bool `mapstructure:"enable"`
|
|
|
|
Expiration int `mapstructure:"expiration"`
|
2020-07-29 20:45:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:48:01 +00:00
|
|
|
type configBlog struct {
|
2020-11-09 18:34:22 +00:00
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Lang string `mapstructure:"lang"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
Pagination int `mapstructure:"pagination"`
|
2020-11-15 10:34:48 +00:00
|
|
|
DefaultSection string `mapstructure:"defaultsection"`
|
2020-11-09 18:34:22 +00:00
|
|
|
Sections map[string]*section `mapstructure:"sections"`
|
|
|
|
Taxonomies []*taxonomy `mapstructure:"taxonomies"`
|
|
|
|
Menus map[string]*menu `mapstructure:"menus"`
|
|
|
|
Photos *photos `mapstructure:"photos"`
|
2020-11-15 10:34:48 +00:00
|
|
|
Search *search `mapstructure:"search"`
|
2021-01-04 19:29:49 +00:00
|
|
|
BlogStats *blogStats `mapstructure:"blogStats"`
|
2021-05-08 19:22:48 +00:00
|
|
|
Blogroll *configBlogroll `mapstructure:"blogroll"`
|
2020-11-09 18:34:22 +00:00
|
|
|
CustomPages []*customPage `mapstructure:"custompages"`
|
2020-11-09 18:54:06 +00:00
|
|
|
Telegram *configTelegram `mapstructure:"telegram"`
|
2020-12-23 15:53:10 +00:00
|
|
|
PostAsHome bool `mapstructure:"postAsHome"`
|
2021-01-20 12:38:24 +00:00
|
|
|
RandomPost *randomPost `mapstructure:"randomPost"`
|
2021-01-23 16:24:47 +00:00
|
|
|
Comments *comments `mapstructure:"comments"`
|
2020-09-01 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type section struct {
|
2020-10-06 17:07:48 +00:00
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
PathTemplate string `mapstructure:"pathtemplate"`
|
2020-09-01 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type taxonomy struct {
|
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
2020-07-31 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 11:37:58 +00:00
|
|
|
type menu struct {
|
|
|
|
Items []*menuItem `mapstructure:"items"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type menuItem struct {
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Link string `mapstructure:"link"`
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:03:05 +00:00
|
|
|
type photos struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Parameter string `mapstructure:"parameter"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
}
|
|
|
|
|
2020-11-15 10:34:48 +00:00
|
|
|
type search struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
Placeholder string `mapstructure:"placeholder"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 19:29:49 +00:00
|
|
|
type blogStats struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:22:48 +00:00
|
|
|
type configBlogroll struct {
|
2021-05-10 15:37:34 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Opml string `mapstructure:"opml"`
|
|
|
|
AuthHeader string `mapstructure:"authHeader"`
|
|
|
|
AuthValue string `mapstructure:"authValue"`
|
|
|
|
Categories []string `mapstructure:"categories"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
2021-05-08 19:22:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 11:40:16 +00:00
|
|
|
type customPage struct {
|
2020-11-20 14:33:20 +00:00
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Template string `mapstructure:"template"`
|
|
|
|
Cache bool `mapstructure:"cache"`
|
|
|
|
CacheExpiration int `mapstructure:"cacheExpiration"`
|
|
|
|
Data *interface{} `mapstructure:"data"`
|
2020-10-13 11:40:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 12:38:24 +00:00
|
|
|
type randomPost struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
}
|
|
|
|
|
2021-01-23 16:24:47 +00:00
|
|
|
type comments struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
}
|
|
|
|
|
2020-08-01 13:16:21 +00:00
|
|
|
type configUser struct {
|
2021-02-28 07:57:15 +00:00
|
|
|
Nick string `mapstructure:"nick"`
|
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
TOTP string `mapstructure:"totp"`
|
|
|
|
AppPasswords []*appPassword `mapstructure:"appPasswords"`
|
|
|
|
Picture string `mapstructure:"picture"`
|
|
|
|
Email string `mapstructure:"email"`
|
|
|
|
Link string `mapstructure:"link"`
|
|
|
|
Identities []string `mapstructure:"identities"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type appPassword struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
2020-08-01 13:16:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 16:46:25 +00:00
|
|
|
type configHooks struct {
|
|
|
|
Shell string `mapstructure:"shell"`
|
2020-10-18 15:22:24 +00:00
|
|
|
Hourly []string `mapstructure:"hourly"`
|
2020-09-20 16:46:25 +00:00
|
|
|
PreStart []string `mapstructure:"prestart"`
|
2020-10-19 18:25:30 +00:00
|
|
|
// Can use template
|
|
|
|
PostPost []string `mapstructure:"postpost"`
|
2020-11-09 15:40:12 +00:00
|
|
|
PostUpdate []string `mapstructure:"postupdate"`
|
2020-10-19 18:25:30 +00:00
|
|
|
PostDelete []string `mapstructure:"postdelete"`
|
2020-09-20 16:46:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 17:07:48 +00:00
|
|
|
type configMicropub struct {
|
2020-10-14 19:20:17 +00:00
|
|
|
CategoryParam string `mapstructure:"categoryParam"`
|
|
|
|
ReplyParam string `mapstructure:"replyParam"`
|
|
|
|
LikeParam string `mapstructure:"likeParam"`
|
|
|
|
BookmarkParam string `mapstructure:"bookmarkParam"`
|
|
|
|
AudioParam string `mapstructure:"audioParam"`
|
|
|
|
PhotoParam string `mapstructure:"photoParam"`
|
|
|
|
PhotoDescriptionParam string `mapstructure:"photoDescriptionParam"`
|
2021-05-25 20:17:38 +00:00
|
|
|
LocationParam string `mapstructure:"locationParam"`
|
2020-10-14 19:20:17 +00:00
|
|
|
MediaStorage *configMicropubMedia `mapstructure:"mediaStorage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type configMicropubMedia struct {
|
2021-06-23 12:28:51 +00:00
|
|
|
MediaURL string `mapstructure:"mediaUrl"`
|
|
|
|
// BunnyCDN
|
|
|
|
BunnyStorageKey string `mapstructure:"bunnyStorageKey"`
|
|
|
|
BunnyStorageName string `mapstructure:"bunnyStorageName"`
|
|
|
|
BunnyStorageRegion string `mapstructure:"bunnyStorageRegion"`
|
|
|
|
// FTP
|
|
|
|
FTPAddress string `mapstructure:"ftpAddress"`
|
|
|
|
FTPUser string `mapstructure:"ftpUser"`
|
|
|
|
FTPPassword string `mapstructure:"ftpPassword"`
|
|
|
|
// Tinify
|
|
|
|
TinifyKey string `mapstructure:"tinifyKey"`
|
|
|
|
// Shortpixel
|
|
|
|
ShortPixelKey string `mapstructure:"shortPixelKey"`
|
|
|
|
// Cloudflare
|
|
|
|
CloudflareCompressionEnabled bool `mapstructure:"cloudflareCompressionEnabled"`
|
2020-10-06 17:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:54:43 +00:00
|
|
|
type configRegexRedirect struct {
|
|
|
|
From string `mapstructure:"from"`
|
|
|
|
To string `mapstructure:"to"`
|
2020-11-10 06:53:08 +00:00
|
|
|
Type int `mapstructure:"type"`
|
2020-10-15 18:54:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 16:37:31 +00:00
|
|
|
type configActivityPub struct {
|
2021-03-10 10:29:20 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
KeyPath string `mapstructure:"keyPath"`
|
|
|
|
TagsTaxonomies []string `mapstructure:"tagsTaxonomies"`
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 17:45:31 +00:00
|
|
|
type configNotifications struct {
|
|
|
|
Telegram *configTelegram `mapstructure:"telegram"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type configTelegram struct {
|
2021-02-08 19:33:48 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
ChatID string `mapstructure:"chatId"`
|
|
|
|
BotToken string `mapstructure:"botToken"`
|
|
|
|
InstantViewHash string `mapstructure:"instantViewHash"`
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 07:31:06 +00:00
|
|
|
type configPrivateMode struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
}
|
|
|
|
|
2021-04-23 17:36:57 +00:00
|
|
|
type configWebmention struct {
|
|
|
|
DisableSending bool `mapstructure:"disableSending"`
|
|
|
|
DisableReceiving bool `mapstructure:"disableReceiving"`
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) initConfig() error {
|
2021-06-17 14:34:57 +00:00
|
|
|
log.Println("Initialize configuration...")
|
2020-07-28 19:17:07 +00:00
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.AddConfigPath("./config/")
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-14 16:23:56 +00:00
|
|
|
// Defaults
|
2020-08-04 17:42:09 +00:00
|
|
|
viper.SetDefault("server.logging", false)
|
2020-11-03 17:58:32 +00:00
|
|
|
viper.SetDefault("server.logFile", "data/access.log")
|
2020-08-04 17:42:09 +00:00
|
|
|
viper.SetDefault("server.port", 8080)
|
2020-09-15 15:01:53 +00:00
|
|
|
viper.SetDefault("server.publicAddress", "http://localhost:8080")
|
2020-08-04 17:42:09 +00:00
|
|
|
viper.SetDefault("server.publicHttps", false)
|
|
|
|
viper.SetDefault("database.file", "data/db.sqlite")
|
|
|
|
viper.SetDefault("cache.enable", true)
|
|
|
|
viper.SetDefault("cache.expiration", 600)
|
|
|
|
viper.SetDefault("user.nick", "admin")
|
|
|
|
viper.SetDefault("user.password", "secret")
|
2020-09-20 16:46:25 +00:00
|
|
|
viper.SetDefault("hooks.shell", "/bin/bash")
|
2020-10-06 17:07:48 +00:00
|
|
|
viper.SetDefault("micropub.categoryParam", "tags")
|
|
|
|
viper.SetDefault("micropub.replyParam", "replylink")
|
|
|
|
viper.SetDefault("micropub.likeParam", "likelink")
|
|
|
|
viper.SetDefault("micropub.bookmarkParam", "link")
|
|
|
|
viper.SetDefault("micropub.audioParam", "audio")
|
|
|
|
viper.SetDefault("micropub.photoParam", "images")
|
|
|
|
viper.SetDefault("micropub.photoDescriptionParam", "imagealts")
|
2021-05-25 20:17:38 +00:00
|
|
|
viper.SetDefault("micropub.locationParam", "location")
|
2020-10-26 16:37:31 +00:00
|
|
|
viper.SetDefault("activityPub.keyPath", "data/private.pem")
|
2021-03-10 10:29:20 +00:00
|
|
|
viper.SetDefault("activityPub.tagsTaxonomies", []string{"tags"})
|
2021-04-23 17:36:57 +00:00
|
|
|
viper.SetDefault("webmention.disableSending", false)
|
|
|
|
viper.SetDefault("webmention.disableReceiving", false)
|
2020-08-04 17:42:09 +00:00
|
|
|
// Unmarshal config
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cfg = &config{}
|
|
|
|
err = viper.Unmarshal(a.cfg)
|
2020-08-04 17:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-06 17:07:48 +00:00
|
|
|
// Check config
|
2021-06-06 12:39:42 +00:00
|
|
|
publicURL, err := url.Parse(a.cfg.Server.PublicAddress)
|
2020-12-24 10:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cfg.Server.publicHostname = publicURL.Hostname()
|
|
|
|
if a.cfg.Server.ShortPublicAddress != "" {
|
|
|
|
shortPublicURL, err := url.Parse(a.cfg.Server.ShortPublicAddress)
|
2020-12-24 10:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cfg.Server.shortPublicHostname = shortPublicURL.Hostname()
|
2020-10-14 16:23:56 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if a.cfg.Server.JWTSecret == "" {
|
2020-12-15 16:40:14 +00:00
|
|
|
return errors.New("no JWT secret configured")
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if len(a.cfg.Blogs) == 0 {
|
2020-10-06 17:07:48 +00:00
|
|
|
return errors.New("no blog configured")
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if len(a.cfg.DefaultBlog) == 0 || a.cfg.Blogs[a.cfg.DefaultBlog] == nil {
|
2020-10-06 17:07:48 +00:00
|
|
|
return errors.New("no default blog or default blog not present")
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if a.cfg.Micropub.MediaStorage != nil {
|
|
|
|
if a.cfg.Micropub.MediaStorage.MediaURL == "" ||
|
|
|
|
a.cfg.Micropub.MediaStorage.BunnyStorageKey == "" ||
|
|
|
|
a.cfg.Micropub.MediaStorage.BunnyStorageName == "" {
|
|
|
|
a.cfg.Micropub.MediaStorage.BunnyStorageKey = ""
|
|
|
|
a.cfg.Micropub.MediaStorage.BunnyStorageName = ""
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cfg.Micropub.MediaStorage.MediaURL = strings.TrimSuffix(a.cfg.Micropub.MediaStorage.MediaURL, "/")
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if pm := a.cfg.PrivateMode; pm != nil && pm.Enabled {
|
|
|
|
a.cfg.ActivityPub = &configActivityPub{Enabled: false}
|
2021-02-27 07:31:06 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if wm := a.cfg.Webmention; wm != nil && wm.DisableReceiving {
|
2021-04-23 17:36:57 +00:00
|
|
|
// Disable comments for all blogs
|
2021-06-06 12:39:42 +00:00
|
|
|
for _, b := range a.cfg.Blogs {
|
2021-04-23 17:36:57 +00:00
|
|
|
b.Comments = &comments{Enabled: false}
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 19:22:48 +00:00
|
|
|
// Check config for each blog
|
2021-06-06 12:39:42 +00:00
|
|
|
for _, blog := range a.cfg.Blogs {
|
2021-05-08 19:22:48 +00:00
|
|
|
if br := blog.Blogroll; br != nil && br.Enabled && br.Opml == "" {
|
|
|
|
br.Enabled = false
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 14:34:57 +00:00
|
|
|
log.Println("Initialized configuration")
|
2020-07-28 19:17:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-20 21:45:38 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) httpsConfigured() bool {
|
|
|
|
return a.cfg.Server.PublicHTTPS || a.cfg.Server.SecurityHeaders || strings.HasPrefix(a.cfg.Server.PublicAddress, "https")
|
2021-02-20 21:45:38 +00:00
|
|
|
}
|