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"
|
2021-11-04 07:52:16 +00:00
|
|
|
"net/http"
|
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
|
|
|
|
2022-03-16 07:28:03 +00:00
|
|
|
"github.com/samber/lo"
|
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"`
|
2022-01-24 08:43:06 +00:00
|
|
|
IndexNow *configIndexNow `mapstructure:"indexNow"`
|
2021-07-23 10:59:24 +00:00
|
|
|
EasterEgg *configEasterEgg `mapstructure:"easterEgg"`
|
2021-11-22 15:36:17 +00:00
|
|
|
MapTiles *configMapTiles `mapstructure:"mapTiles"`
|
2021-12-16 19:21:54 +00:00
|
|
|
TTS *configTTS `mapstructure:"tts"`
|
2022-02-23 20:33:02 +00:00
|
|
|
Pprof *configPprof `mapstructure:"pprof"`
|
|
|
|
Debug bool `mapstructure:"debug"`
|
2021-12-14 16:38:36 +00:00
|
|
|
initialized bool
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type configServer struct {
|
2021-10-13 07:01:54 +00:00
|
|
|
Logging bool `mapstructure:"logging"`
|
|
|
|
LogFile string `mapstructure:"logFile"`
|
|
|
|
Port int `mapstructure:"port"`
|
|
|
|
PublicAddress string `mapstructure:"publicAddress"`
|
|
|
|
ShortPublicAddress string `mapstructure:"shortPublicAddress"`
|
|
|
|
MediaAddress string `mapstructure:"mediaAddress"`
|
|
|
|
PublicHTTPS bool `mapstructure:"publicHttps"`
|
|
|
|
TailscaleHTTPS bool `mapstructure:"tailscaleHttps"`
|
|
|
|
Tailscale *configTailscale `mapstructure:"tailscale"`
|
|
|
|
Tor bool `mapstructure:"tor"`
|
2022-03-10 13:52:56 +00:00
|
|
|
TorSingleHop bool `mapstructure:"torSingleHop"`
|
2021-10-13 07:01:54 +00:00
|
|
|
SecurityHeaders bool `mapstructure:"securityHeaders"`
|
|
|
|
CSPDomains []string `mapstructure:"cspDomains"`
|
2020-12-24 10:00:16 +00:00
|
|
|
publicHostname string
|
|
|
|
shortPublicHostname string
|
2021-08-02 18:43:24 +00:00
|
|
|
mediaHostname string
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 07:01:54 +00:00
|
|
|
type configTailscale struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
AuthKey string `mapstructure:"authKey"`
|
|
|
|
Hostname string `mapstructure:"Hostname"`
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-07-12 14:19:28 +00:00
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Lang string `mapstructure:"lang"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
Pagination int `mapstructure:"pagination"`
|
|
|
|
DefaultSection string `mapstructure:"defaultsection"`
|
|
|
|
Sections map[string]*configSection `mapstructure:"sections"`
|
|
|
|
Taxonomies []*configTaxonomy `mapstructure:"taxonomies"`
|
|
|
|
Menus map[string]*configMenu `mapstructure:"menus"`
|
|
|
|
Photos *configPhotos `mapstructure:"photos"`
|
|
|
|
Search *configSearch `mapstructure:"search"`
|
|
|
|
BlogStats *configBlogStats `mapstructure:"blogStats"`
|
|
|
|
Blogroll *configBlogroll `mapstructure:"blogroll"`
|
|
|
|
Telegram *configTelegram `mapstructure:"telegram"`
|
|
|
|
PostAsHome bool `mapstructure:"postAsHome"`
|
|
|
|
RandomPost *configRandomPost `mapstructure:"randomPost"`
|
2022-01-26 20:08:46 +00:00
|
|
|
OnThisDay *configOnThisDay `mapstructure:"onThisDay"`
|
2021-07-12 14:19:28 +00:00
|
|
|
Comments *configComments `mapstructure:"comments"`
|
|
|
|
Map *configGeoMap `mapstructure:"map"`
|
2021-07-22 11:41:52 +00:00
|
|
|
Contact *configContact `mapstructure:"contact"`
|
2021-09-09 06:26:35 +00:00
|
|
|
Announcement *configAnnouncement `mapstructure:"announcement"`
|
2020-09-01 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configSection struct {
|
2020-10-06 17:07:48 +00:00
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
PathTemplate string `mapstructure:"pathtemplate"`
|
2021-07-31 11:20:51 +00:00
|
|
|
ShowFull bool `mapstructure:"showFull"`
|
2021-12-28 10:47:52 +00:00
|
|
|
Name string
|
2020-09-01 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configTaxonomy struct {
|
2020-09-01 16:14:49 +00:00
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
2020-07-31 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configMenu struct {
|
|
|
|
Items []*configMenuItem `mapstructure:"items"`
|
2020-09-19 11:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configMenuItem struct {
|
2020-09-19 11:37:58 +00:00
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Link string `mapstructure:"link"`
|
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configPhotos struct {
|
2020-09-21 16:03:05 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configSearch struct {
|
2020-11-15 10:34:48 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
Placeholder string `mapstructure:"placeholder"`
|
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configBlogStats struct {
|
2021-01-04 19:29:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configRandomPost struct {
|
2021-01-20 12:38:24 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:08:46 +00:00
|
|
|
type configOnThisDay struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configComments struct {
|
2021-01-23 16:24:47 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configGeoMap struct {
|
2021-07-06 19:06:39 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:41:52 +00:00
|
|
|
type configContact struct {
|
2021-07-22 12:04:46 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Title string `mapstructure:"title"`
|
|
|
|
Description string `mapstructure:"description"`
|
|
|
|
PrivacyPolicy string `mapstructure:"privacyPolicy"`
|
|
|
|
SMTPHost string `mapstructure:"smtpHost"`
|
|
|
|
SMTPPort int `mapstructure:"smtpPort"`
|
|
|
|
SMTPUser string `mapstructure:"smtpUser"`
|
|
|
|
SMTPPassword string `mapstructure:"smtpPassword"`
|
|
|
|
EmailFrom string `mapstructure:"emailFrom"`
|
|
|
|
EmailTo string `mapstructure:"emailTo"`
|
2021-11-08 15:27:33 +00:00
|
|
|
EmailSubject string `mapstructure:"emailSubject"`
|
2021-07-22 11:41:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 06:26:35 +00:00
|
|
|
type configAnnouncement struct {
|
|
|
|
Text string `mapstructure:"text"`
|
|
|
|
}
|
|
|
|
|
2020-08-01 13:16:21 +00:00
|
|
|
type configUser struct {
|
2021-07-12 14:19:28 +00:00
|
|
|
Nick string `mapstructure:"nick"`
|
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
TOTP string `mapstructure:"totp"`
|
|
|
|
AppPasswords []*configAppPassword `mapstructure:"appPasswords"`
|
|
|
|
Picture string `mapstructure:"picture"`
|
|
|
|
Email string `mapstructure:"email"`
|
|
|
|
Link string `mapstructure:"link"`
|
|
|
|
Identities []string `mapstructure:"identities"`
|
2021-02-28 07:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
type configAppPassword struct {
|
2021-02-28 07:57:15 +00:00
|
|
|
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
|
2022-01-03 12:55:44 +00:00
|
|
|
PostPost []string `mapstructure:"postpost"`
|
|
|
|
PostUpdate []string `mapstructure:"postupdate"`
|
|
|
|
PostDelete []string `mapstructure:"postdelete"`
|
|
|
|
PostUndelete []string `mapstructure:"postundelete"`
|
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"`
|
2021-07-31 11:20:51 +00:00
|
|
|
ReplyTitleParam string `mapstructure:"replyTitleParam"`
|
2020-10-14 19:20:17 +00:00
|
|
|
LikeParam string `mapstructure:"likeParam"`
|
2021-07-31 11:20:51 +00:00
|
|
|
LikeTitleParam string `mapstructure:"likeTitleParam"`
|
2020-10-14 19:20:17 +00:00
|
|
|
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"`
|
|
|
|
// Cloudflare
|
|
|
|
CloudflareCompressionEnabled bool `mapstructure:"cloudflareCompressionEnabled"`
|
2022-03-24 14:19:34 +00:00
|
|
|
// Local
|
|
|
|
LocalCompressionEnabled bool `mapstructure:"localCompressionEnabled"`
|
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"`
|
|
|
|
TagsTaxonomies []string `mapstructure:"tagsTaxonomies"`
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 17:45:31 +00:00
|
|
|
type configNotifications struct {
|
2021-12-29 06:09:42 +00:00
|
|
|
Ntfy *configNtfy `mapstructure:"ntfy"`
|
2020-11-06 17:45:31 +00:00
|
|
|
Telegram *configTelegram `mapstructure:"telegram"`
|
|
|
|
}
|
|
|
|
|
2021-12-29 06:09:42 +00:00
|
|
|
type configNtfy struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Topic string `mapstructure:"topic"`
|
2022-04-04 11:07:36 +00:00
|
|
|
Server string `mapstructure:"server"`
|
|
|
|
User string `mapstructure:"user"`
|
|
|
|
Pass string `mapstructure:"pass"`
|
2021-12-29 06:09:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 17:45:31 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:43:06 +00:00
|
|
|
type configIndexNow struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
}
|
|
|
|
|
2021-07-23 10:59:24 +00:00
|
|
|
type configEasterEgg 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-11-22 15:36:17 +00:00
|
|
|
type configMapTiles struct {
|
|
|
|
Source string `mapstructure:"source"`
|
|
|
|
Attribution string `mapstructure:"attribution"`
|
|
|
|
MinZoom int `mapstructure:"minZoom"`
|
|
|
|
MaxZoom int `mapstructure:"maxZoom"`
|
|
|
|
}
|
|
|
|
|
2021-12-16 19:21:54 +00:00
|
|
|
type configTTS struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
GoogleAPIKey string `mapstructure:"googleApiKey"`
|
|
|
|
}
|
|
|
|
|
2022-02-23 20:33:02 +00:00
|
|
|
type configPprof struct {
|
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
Address string `mapstructure:"address"`
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:38:36 +00:00
|
|
|
func (a *goBlog) loadConfigFile(file string) error {
|
|
|
|
// Use viper to load the config file
|
|
|
|
v := viper.New()
|
2021-11-13 14:54:00 +00:00
|
|
|
if file != "" {
|
|
|
|
// Use config file from the flag
|
2021-12-14 16:38:36 +00:00
|
|
|
v.SetConfigFile(file)
|
2021-11-13 14:54:00 +00:00
|
|
|
} else {
|
2021-12-14 16:38:36 +00:00
|
|
|
// Search in default locations
|
|
|
|
v.SetConfigName("config")
|
|
|
|
v.AddConfigPath("./config/")
|
2021-11-13 14:54:00 +00:00
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Read config
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
2020-07-28 19:17:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-04 17:42:09 +00:00
|
|
|
// Unmarshal config
|
2021-12-14 16:38:36 +00:00
|
|
|
a.cfg = createDefaultConfig()
|
|
|
|
return v.Unmarshal(a.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) initConfig() error {
|
|
|
|
if a.cfg == nil {
|
|
|
|
a.cfg = createDefaultConfig()
|
|
|
|
}
|
|
|
|
if a.cfg.initialized {
|
|
|
|
return nil
|
2020-08-04 17:42:09 +00:00
|
|
|
}
|
2020-10-06 17:07:48 +00:00
|
|
|
// Check config
|
2021-12-14 16:38:36 +00:00
|
|
|
// Parse addresses and hostnames
|
|
|
|
if a.cfg.Server.PublicAddress == "" {
|
|
|
|
return errors.New("no public address configured")
|
|
|
|
}
|
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 {
|
2021-12-14 16:38:36 +00:00
|
|
|
return errors.New("Invalid public address: " + err.Error())
|
2020-12-24 10:00:16 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cfg.Server.publicHostname = publicURL.Hostname()
|
2021-08-02 18:43:24 +00:00
|
|
|
if sa := a.cfg.Server.ShortPublicAddress; sa != "" {
|
|
|
|
shortPublicURL, err := url.Parse(sa)
|
2020-12-24 10:00:16 +00:00
|
|
|
if err != nil {
|
2021-12-14 16:38:36 +00:00
|
|
|
return errors.New("Invalid short public address: " + err.Error())
|
2020-12-24 10:00:16 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cfg.Server.shortPublicHostname = shortPublicURL.Hostname()
|
2020-10-14 16:23:56 +00:00
|
|
|
}
|
2021-08-02 18:43:24 +00:00
|
|
|
if ma := a.cfg.Server.MediaAddress; ma != "" {
|
|
|
|
mediaUrl, err := url.Parse(ma)
|
|
|
|
if err != nil {
|
2021-12-14 16:38:36 +00:00
|
|
|
return errors.New("Invalid media address: " + err.Error())
|
2021-08-02 18:43:24 +00:00
|
|
|
}
|
|
|
|
a.cfg.Server.mediaHostname = mediaUrl.Hostname()
|
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Check if any blog is configured
|
|
|
|
if a.cfg.Blogs == nil || len(a.cfg.Blogs) == 0 {
|
|
|
|
a.cfg.Blogs = map[string]*configBlog{
|
|
|
|
"default": createDefaultBlog(),
|
|
|
|
}
|
2020-12-15 16:40:14 +00:00
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Check if default blog is set
|
|
|
|
if a.cfg.DefaultBlog == "" {
|
|
|
|
if len(a.cfg.Blogs) == 1 {
|
|
|
|
// Set default blog to the only blog that is configured
|
2022-03-16 07:28:03 +00:00
|
|
|
a.cfg.DefaultBlog = lo.Keys(a.cfg.Blogs)[0]
|
2021-12-14 16:38:36 +00:00
|
|
|
} else {
|
|
|
|
return errors.New("no default blog configured")
|
|
|
|
}
|
2020-10-06 17:07:48 +00:00
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Check if default blog exists
|
|
|
|
if a.cfg.Blogs[a.cfg.DefaultBlog] == nil {
|
|
|
|
return errors.New("default blog does not exist")
|
2020-10-06 17:07:48 +00:00
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Check media storage config
|
|
|
|
if ms := a.cfg.Micropub.MediaStorage; ms != nil && ms.MediaURL != "" {
|
|
|
|
ms.MediaURL = strings.TrimSuffix(ms.MediaURL, "/")
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Check if webmention receiving is disabled
|
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-07-12 14:19:28 +00:00
|
|
|
b.Comments = &configComments{Enabled: false}
|
2021-04-23 17:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
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-12-28 10:47:52 +00:00
|
|
|
// Copy sections key to section name
|
|
|
|
for k, s := range blog.Sections {
|
|
|
|
s.Name = k
|
|
|
|
}
|
2021-12-16 19:21:54 +00:00
|
|
|
// Check if language is set
|
|
|
|
if blog.Lang == "" {
|
|
|
|
blog.Lang = "en"
|
|
|
|
}
|
2021-07-17 07:33:44 +00:00
|
|
|
// Blogroll
|
2021-05-08 19:22:48 +00:00
|
|
|
if br := blog.Blogroll; br != nil && br.Enabled && br.Opml == "" {
|
|
|
|
br.Enabled = false
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 16:38:36 +00:00
|
|
|
// Log success
|
|
|
|
a.cfg.initialized = true
|
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-12-14 16:38:36 +00:00
|
|
|
func createDefaultConfig() *config {
|
|
|
|
return &config{
|
|
|
|
Server: &configServer{
|
|
|
|
Port: 8080,
|
|
|
|
PublicAddress: "http://localhost:8080",
|
2021-12-27 20:47:12 +00:00
|
|
|
LogFile: "data/access.log",
|
2021-12-14 16:38:36 +00:00
|
|
|
},
|
|
|
|
Db: &configDb{
|
|
|
|
File: "data/db.sqlite",
|
|
|
|
},
|
|
|
|
Cache: &configCache{
|
|
|
|
Enable: true,
|
|
|
|
Expiration: 600,
|
|
|
|
},
|
|
|
|
User: &configUser{
|
|
|
|
Nick: "admin",
|
|
|
|
Password: "secret",
|
|
|
|
},
|
|
|
|
Hooks: &configHooks{
|
|
|
|
Shell: "/bin/bash",
|
|
|
|
},
|
|
|
|
Micropub: &configMicropub{
|
|
|
|
CategoryParam: "tags",
|
|
|
|
ReplyParam: "replylink",
|
|
|
|
ReplyTitleParam: "replytitle",
|
|
|
|
LikeParam: "likelink",
|
|
|
|
LikeTitleParam: "liketitle",
|
|
|
|
BookmarkParam: "link",
|
|
|
|
AudioParam: "audio",
|
|
|
|
PhotoParam: "images",
|
|
|
|
PhotoDescriptionParam: "imagealts",
|
|
|
|
LocationParam: "location",
|
|
|
|
},
|
|
|
|
ActivityPub: &configActivityPub{
|
|
|
|
TagsTaxonomies: []string{"tags"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDefaultBlog() *configBlog {
|
|
|
|
return &configBlog{
|
|
|
|
Path: "/",
|
|
|
|
Lang: "en",
|
|
|
|
Title: "My Blog",
|
|
|
|
Description: "Welcome to my blog.",
|
|
|
|
Sections: map[string]*configSection{
|
|
|
|
"posts": {
|
|
|
|
Title: "Posts",
|
|
|
|
},
|
|
|
|
},
|
2021-12-28 11:36:42 +00:00
|
|
|
Taxonomies: []*configTaxonomy{
|
|
|
|
{
|
|
|
|
Name: "tags",
|
|
|
|
Title: "Tags",
|
|
|
|
},
|
|
|
|
},
|
2021-12-14 16:38:36 +00:00
|
|
|
DefaultSection: "posts",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:42:00 +00:00
|
|
|
func (a *goBlog) httpsConfigured(checkAddress bool) bool {
|
|
|
|
return a.cfg.Server.PublicHTTPS ||
|
|
|
|
a.cfg.Server.TailscaleHTTPS ||
|
|
|
|
a.cfg.Server.SecurityHeaders ||
|
|
|
|
(checkAddress && strings.HasPrefix(a.cfg.Server.PublicAddress, "https"))
|
2021-02-20 21:45:38 +00:00
|
|
|
}
|
2021-11-04 07:52:16 +00:00
|
|
|
|
|
|
|
func (a *goBlog) getBlog(r *http.Request) (string, *configBlog) {
|
|
|
|
if r == nil {
|
|
|
|
return a.cfg.DefaultBlog, a.cfg.Blogs[a.cfg.DefaultBlog]
|
|
|
|
}
|
2021-12-20 13:00:11 +00:00
|
|
|
blog := a.cfg.DefaultBlog
|
|
|
|
if ctxBlog := r.Context().Value(blogKey); ctxBlog != nil {
|
|
|
|
if ctxBlogString, ok := ctxBlog.(string); ok {
|
|
|
|
blog = ctxBlogString
|
|
|
|
}
|
2021-11-04 07:52:16 +00:00
|
|
|
}
|
|
|
|
return blog, a.cfg.Blogs[blog]
|
|
|
|
}
|