GoBlog/config.go

111 lines
2.9 KiB
Go
Raw Normal View History

2020-07-28 19:17:07 +00:00
package main
import (
"github.com/spf13/viper"
)
type config struct {
server *configServer
db *configDb
2020-07-29 20:45:26 +00:00
cache *configCache
2020-07-31 13:48:01 +00:00
blog *configBlog
user *configUser
2020-07-28 19:17:07 +00:00
}
type configServer struct {
2020-08-01 15:49:46 +00:00
logging bool
port int
domain string
publicHttps bool
letsEncryptMail string
localHttps bool
2020-07-28 19:17:07 +00:00
}
type configDb struct {
file string
}
2020-07-29 20:45:26 +00:00
type configCache struct {
enable bool
expiration int64
}
2020-07-31 13:48:01 +00:00
// exposed to templates via function "blog"
type configBlog struct {
// Language of the blog, e.g. "en" or "de"
Lang string
2020-07-31 13:48:01 +00:00
// Title of the blog, e.g. "My blog"
Title string
}
type configUser struct {
nick string
name string
password string
}
2020-07-28 19:17:07 +00:00
var appConfig = &config{
server: &configServer{},
db: &configDb{},
2020-07-29 20:45:26 +00:00
cache: &configCache{},
2020-07-31 13:48:01 +00:00
blog: &configBlog{},
user: &configUser{},
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
}
// Server
serverLogging := "server.logging"
viper.SetDefault(serverLogging, false)
appConfig.server.logging = viper.GetBool(serverLogging)
serverPort := "server.port"
viper.SetDefault(serverPort, 8080)
appConfig.server.port = viper.GetInt(serverPort)
2020-08-01 15:49:46 +00:00
serverDomain := "server.domain"
viper.SetDefault(serverDomain, "example.com")
appConfig.server.domain = viper.GetString(serverDomain)
serverPublicHttps := "server.publicHttps"
viper.SetDefault(serverPublicHttps, false)
appConfig.server.publicHttps = viper.GetBool(serverPublicHttps)
serverLetsEncryptMail := "server.letsEncryptMail"
viper.SetDefault(serverLetsEncryptMail, "mail@example.com")
appConfig.server.letsEncryptMail = viper.GetString(serverLetsEncryptMail)
serverLocalHttps := "server.localHttps"
viper.SetDefault(serverLocalHttps, false)
appConfig.server.localHttps = viper.GetBool(serverLocalHttps)
2020-07-28 19:17:07 +00:00
// Database
databaseFile := "database.file"
viper.SetDefault(databaseFile, "data/db.sqlite")
appConfig.db.file = viper.GetString(databaseFile)
2020-07-29 20:45:26 +00:00
// Caching
cacheEnable := "cache.enable"
viper.SetDefault(cacheEnable, true)
appConfig.cache.enable = viper.GetBool(cacheEnable)
cacheExpiration := "cache.expiration"
viper.SetDefault(cacheExpiration, 600)
appConfig.cache.expiration = viper.GetInt64(cacheExpiration)
2020-07-31 13:48:01 +00:00
// Blog meta
blogLang := "blog.lang"
viper.SetDefault(blogLang, "en")
appConfig.blog.Lang = viper.GetString(blogLang)
blogTitle := "blog.title"
viper.SetDefault(blogTitle, "My blog")
appConfig.blog.Title = viper.GetString(blogTitle)
// User
userNick := "user.nick"
viper.SetDefault(userNick, "admin")
appConfig.user.nick = viper.GetString(userNick)
userName := "user.name"
viper.SetDefault(userName, "Admin")
appConfig.user.name = viper.GetString(userName)
userPassword := "user.password"
viper.SetDefault(userPassword, "secret")
appConfig.user.password = viper.GetString(userPassword)
2020-07-28 19:17:07 +00:00
return nil
}