Mercurial > gemma
diff pkg/config/config.go @ 414:c1047fd04a3a
Moved project specific Go packages to new pkg folder.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 15 Aug 2018 17:30:50 +0200 |
parents | config/config.go@ac23905e64b1 |
children | c37457f12b8e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/config/config.go Wed Aug 15 17:30:50 2018 +0200 @@ -0,0 +1,186 @@ +package config + +import ( + "crypto/sha256" + "fmt" + "log" + "sync" + + homedir "github.com/mitchellh/go-homedir" + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "gemma.intevation.de/gemma/pkg/common" +) + +// This is not part of the persistent config. +var configFile string + +func ConfigFile() string { return configFile } + +func DBHost() string { return viper.GetString("dbhost") } +func DBPort() uint { return uint(viper.GetInt32("dbport")) } +func DBName() string { return viper.GetString("dbname") } +func DBSSLMode() string { return viper.GetString("dbssl") } +func SessionStore() string { return viper.GetString("sessions") } +func Web() string { return viper.GetString("web") } +func WebHost() string { return viper.GetString("host") } +func WebPort() uint { return uint(viper.GetInt32("port")) } + +func ServiceUser() string { return viper.GetString("service-user") } +func ServicePassword() string { return viper.GetString("service-password") } + +func SysAdmin() string { return viper.GetString("sys-admin") } +func SysAdminPassword() string { return viper.GetString("sys-admin-password") } + +func MailHost() string { return viper.GetString("mail-host") } +func MailPort() uint { return uint(viper.GetInt32("mail-port")) } +func MailUser() string { return viper.GetString("mail-user") } +func MailPassword() string { return viper.GetString("mail-password") } +func MailFrom() string { return viper.GetString("mail-from") } +func MailHelo() string { return viper.GetString("mail-helo") } + +func AllowedOrigins() []string { return viper.GetStringSlice("allowed-origins") } + +func ExternalWFSs() map[string]interface{} { return viper.GetStringMap("external-wfs") } + +func GeoServerURL() string { return viper.GetString("geoserver-url") } +func GeoServerUser() string { return viper.GetString("geoserver-user") } +func GeoServerPassword() string { return viper.GetString("geoserver-password") } +func GeoServerTables() []string { return viper.GetStringSlice("geoserver-tables") } + +var ( + proxyKeyOnce sync.Once + proxyKey []byte + + proxyPrefixOnce sync.Once + proxyPrefix string +) + +func ProxyKey() []byte { + fetchKey := func() { + if proxyKey == nil { + key := []byte(viper.GetString("proxy-key")) + if len(key) == 0 { + key = common.GenerateRandomKey(64) + } + hash := sha256.New() + hash.Write(key) + proxyKey = hash.Sum(nil) + } + } + proxyKeyOnce.Do(fetchKey) + return proxyKey +} + +func ProxyPrefix() string { + fetchPrefix := func() { + if proxyPrefix == "" { + proxyPrefix = fmt.Sprintf("http://%s:%d", WebHost(), WebPort()) + } + } + proxyPrefixOnce.Do(fetchPrefix) + return proxyPrefix +} + +var RootCmd = &cobra.Command{ + Use: "gemma", + Short: "gemma is a server for waterway monitoring and management", +} + +var allowedOrigins = []string{ + // TODO: Fill me! +} + +var geoTables = []string{ + "fairway_dimensions", +} + +func init() { + cobra.OnInitialize(initConfig) + fl := RootCmd.PersistentFlags() + fl.StringVarP(&configFile, "config", "c", "", "config file (default is $HOME/.gemma.toml)") + + vbind := func(name string) { viper.BindPFlag(name, fl.Lookup(name)) } + + str := func(name, value, usage string) { + fl.String(name, value, usage) + vbind(name) + } + strP := func(name, shorthand, value, usage string) { + fl.StringP(name, shorthand, value, usage) + vbind(name) + } + ui := func(name string, value uint, usage string) { + fl.Uint(name, value, usage) + vbind(name) + } + uiP := func(name, shorthand string, value uint, usage string) { + fl.UintP(name, shorthand, value, usage) + vbind(name) + } + strSl := func(name string, value []string, usage string) { + fl.StringSlice(name, value, usage) + vbind(name) + } + + strP("dbhost", "H", "localhost", "host of the database") + uiP("dbport", "P", 5432, "port of the database") + strP("dbname", "d", "gemma", "name of the database") + strP("dbssl", "S", "prefer", "SSL mode of the database") + + strP("sessions", "s", "", "path to the sessions file") + + strP("web", "w", "./web", "path to the web files") + strP("host", "o", "localhost", "host of the web app") + uiP("port", "p", 8000, "port of the web app") + + str("service-user", "postgres", "user to do service tasks") + str("service-password", "", "password of user to do service tasks") + + str("sys-admin", "postgres", "user to do admin tasks") + str("sys-admin-password", "", "password of user to do admin tasks") + + str("mail-host", "localhost", "server to send mail with") + ui("mail-port", 465, "port of server to send mail with") + str("mail-user", "gemma", "user to send mail with") + str("mail-password", "", "password of user to send mail with") + str("mail-from", "noreplay@localhost", "from line of mails") + str("mail-helo", "localhost", "name of server to send mail from.") + + strSl("allowed-origins", allowedOrigins, "allow access for remote origins") + + str("geoserver-url", "http://localhost:8080/geoserver", "URL to GeoServer") + str("geoserver-user", "admin", "GeoServer user") + str("geoserver-password", "geoserver", "GeoServer password") + strSl("geoserver-tables", geoTables, "tables to publish with GeoServer") + + str("proxy-key", "", `signing key for proxy URLs. Defaults to random key.`) + str("proxy-prefix", "", `URL prefix of proxy. Defaults to "http://${web-host}:${web-port}"`) + +} + +func initConfig() { + // Don't forget to read config either from cfgFile or from home directory! + if configFile != "" { + // Use config file from the flag. + viper.SetConfigFile(configFile) + } else { + // Find home directory. + home, err := homedir.Dir() + if err != nil { + log.Fatalf("error: %v\n", err) + } + + // Search config in home directory with name ".cobra" (without extension). + viper.AddConfigPath(home) + viper.SetConfigName(".gemma") + } + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok && configFile == "" { + // Don't bother if not found. + return + } + log.Fatalf("Can't read config: %v\n", err) + } +}