GoBlog/config.go

205 lines
6.9 KiB
Go
Raw Normal View History

2020-07-28 19:17:07 +00:00
package main
import (
2020-10-06 17:07:48 +00:00
"errors"
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"`
Hugo *configHugo `mapstructure:"hugo"`
Micropub *configMicropub `mapstructure:"micropub"`
PathRedirects []*configRegexRedirect `mapstructure:"pathRedirects"`
2020-07-28 19:17:07 +00:00
}
type configServer struct {
2020-08-04 17:42:09 +00:00
Logging bool `mapstructure:"logging"`
2020-10-12 18:23:21 +00:00
Debug bool `mapstructure:"Debug"`
2020-08-04 17:42:09 +00:00
Port int `mapstructure:"port"`
Domain string `mapstructure:"domain"`
PublicAddress string `mapstructure:"publicAddress"`
2020-10-06 17:07:48 +00:00
PublicHTTPS bool `mapstructure:"publicHttps"`
2020-08-04 17:42:09 +00:00
LetsEncryptMail string `mapstructure:"letsEncryptMail"`
2020-10-06 17:07:48 +00:00
LocalHTTPS bool `mapstructure:"localHttps"`
2020-07-28 19:17:07 +00:00
}
type configDb struct {
2020-08-04 17:42:09 +00:00
File string `mapstructure:"file"`
2020-07-28 19:17:07 +00:00
}
2020-07-29 20:45:26 +00:00
type configCache struct {
2020-08-04 17:42:09 +00:00
Enable bool `mapstructure:"enable"`
Expiration int64 `mapstructure:"expiration"`
2020-07-29 20:45:26 +00:00
}
2020-07-31 13:48:01 +00:00
type configBlog struct {
2020-10-06 17:07:48 +00:00
Path string `mapstructure:"path"`
Lang string `mapstructure:"lang"`
Title string `mapstructure:"title"`
Description string `mapstructure:"description"`
Pagination int `mapstructure:"pagination"`
Sections map[string]*section `mapstructure:"sections"`
Taxonomies []*taxonomy `mapstructure:"taxonomies"`
Menus map[string]*menu `mapstructure:"menus"`
Photos *photos `mapstructure:"photos"`
ActivityStreams *activityStreams `mapstructure:"activitystreams"`
DefaultSection string `mapstructure:"defaultsection"`
2020-10-13 11:40:16 +00:00
CustomPages []*customPage `mapstructure:"custompages"`
}
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"`
}
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-09-24 15:13:03 +00:00
type activityStreams struct {
Enabled bool `mapstructure:"enabled"`
ReplyParameter string `mapstructure:"replyParameter"`
ImagesParameter string `mapstructure:"imagesParameter"`
}
2020-10-13 11:40:16 +00:00
type customPage struct {
Path string `mapstructure:"path"`
Template string `mapstructure:"template"`
Cache bool `mapstructure:"cache"`
Data *interface{} `mapstructure:"data"`
}
type configUser struct {
2020-08-04 17:42:09 +00:00
Nick string `mapstructure:"nick"`
Name string `mapstructure:"name"`
Password string `mapstructure:"password"`
}
type configHooks struct {
Shell string `mapstructure:"shell"`
Hourly []string `mapstructure:"hourly"`
PreStart []string `mapstructure:"prestart"`
2020-10-19 18:25:30 +00:00
// Can use template
PostPost []string `mapstructure:"postpost"`
PostDelete []string `mapstructure:"postdelete"`
}
type configHugo struct {
Frontmatter []*frontmatter `mapstructure:"frontmatter"`
}
type frontmatter struct {
Meta string `mapstructure:"meta"`
Parameter string `mapstructure:"parameter"`
}
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"`
MediaStorage *configMicropubMedia `mapstructure:"mediaStorage"`
}
type configMicropubMedia struct {
MediaURL string `mapstructure:"mediaUrl"`
BunnyStorageKey string `mapstructure:"bunnyStorageKey"`
BunnyStorageName string `mapstructure:"bunnyStorageName"`
TinifyKey string `mapstructure:"tinifyKey"`
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-08-04 17:42:09 +00:00
var appConfig = &config{}
2020-07-28 19:17:07 +00:00
func initConfig() error {
viper.SetConfigName("config")
viper.AddConfigPath("./config/")
err := viper.ReadInConfig()
if err != nil {
return err
}
// Defaults
2020-08-04 17:42:09 +00:00
viper.SetDefault("server.logging", false)
2020-10-12 18:23:21 +00:00
viper.SetDefault("server.debug", false)
2020-08-04 17:42:09 +00:00
viper.SetDefault("server.port", 8080)
viper.SetDefault("server.publicAddress", "http://localhost:8080")
2020-08-04 17:42:09 +00:00
viper.SetDefault("server.publicHttps", false)
viper.SetDefault("server.localHttps", 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.name", "Admin")
viper.SetDefault("user.password", "secret")
viper.SetDefault("hooks.shell", "/bin/bash")
viper.SetDefault("hugo.frontmatter", []*frontmatter{{Meta: "title", Parameter: "title"}, {Meta: "tags", Parameter: "tags"}})
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")
2020-08-04 17:42:09 +00:00
// Unmarshal config
err = viper.Unmarshal(appConfig)
if err != nil {
return err
}
2020-10-06 17:07:48 +00:00
// Check config
if appConfig.Server.Domain == "" {
return errors.New("no domain configured")
}
2020-10-06 17:07:48 +00:00
if len(appConfig.Blogs) == 0 {
return errors.New("no blog configured")
}
if len(appConfig.DefaultBlog) == 0 || appConfig.Blogs[appConfig.DefaultBlog] == nil {
return errors.New("no default blog or default blog not present")
}
2020-10-14 19:20:17 +00:00
if appConfig.Micropub.MediaStorage != nil {
if appConfig.Micropub.MediaStorage.MediaURL == "" ||
appConfig.Micropub.MediaStorage.BunnyStorageKey == "" ||
appConfig.Micropub.MediaStorage.BunnyStorageName == "" {
appConfig.Micropub.MediaStorage = nil
} else {
appConfig.Micropub.MediaStorage.MediaURL = strings.TrimSuffix(appConfig.Micropub.MediaStorage.MediaURL, "/")
}
}
2020-07-28 19:17:07 +00:00
return nil
}