Better config handling with constants #83

Merged
konrad merged 8 commits from feature/better-config into master 2019-07-06 20:12:28 +00:00
1 changed files with 68 additions and 5 deletions
Showing only changes of commit bdc68b6149 - Show all commits

View File

@ -29,10 +29,73 @@ import (
// Key is used as a config key
type Key string
// These constants hold all config value keys
const (
ServiceJWTSecret Key = `service.JWTSecret`
ServiceJWTSecret Key = `service.JWTSecret`
ServiceInterface Key = `service.interface`
ServiceFrontendurl Key = `service.frontendurl`
ServiceEnableCaldav Key = `service.enablecaldav`
ServiceRootpath Key = ``
ServicePageCount Key = ``
ServiceEnableMetrics Key = ``
DatabaseType Key = ``
DatabaseHost Key = ``
DatabaseUser Key = ``
DatabasePassword Key = ``
DatabaseDatabase Key = ``
DatabasePath Key = ``
DatabaseMaxOpenConnections Key = ``
DatabaseMaxIdleConnections Key = ``
DatabaseMaxConnectionLifetime Key = ``
CacheEnabled Key = ``
CacheType Key = ``
CacheMaxElementSize Key = ``
MailerEnabled Key = ``
MailerHost Key = ``
MailerPort Key = ``
MailerUser Key = ``
MailerPassword Key = ``
MailerSkipTLSVerify Key = ``
MailerFromEmail Key = ``
MailerQueuelength Key = ``
MailerQueueTimeout Key = ``
RedisEnabled Key = ``
RedisHost Key = ``
RedisPassword Key = ``
RedisDB Key = ``
LogEnabled Key = ``
LogErrors Key = ``
LogStandard Key = ``
LogHttp Key = ``
LogEcho Key = ``
LogPath Key = ``
)
// GetString returns a string config value
func (k Key) GetString() string {
return viper.GetString(string(k))
}
// GetBool returns a bool config value
func (k Key) GetBool() bool {
return viper.GetBool(string(k))
}
// GetInt returns an int64 config value
func (k Key) GetInt() int64 {
return viper.GetInt64(string(k))
}
// sets the default config value
func (k Key) setDefault(i interface{}) {
viper.SetDefault(string(k), i)
}
// InitConfig initializes the config, sets defaults etc.
func InitConfig() {
@ -44,10 +107,10 @@ func InitConfig() {
}
// Service
viper.SetDefault("service.JWTSecret", random)
viper.SetDefault("service.interface", ":3456")
viper.SetDefault("service.frontendurl", "")
viper.SetDefault("service.enablecaldav", true)
ServiceJWTSecret.setDefault(random)
ServiceInterface.setDefault(":3456")
ServiceFrontendurl.setDefault("")
ServiceEnableCaldav.setDefault(true)
ex, err := os.Executable()
if err != nil {