view config/config.go @ 338:dabe189369ad

Configuration: Port numbers are integers. Mail port defaults to 465 (SSL).
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 03 Aug 2018 22:20:06 +0200
parents bd292a554b6e
children 3cfab707f909
line wrap: on
line source

package config

import (
	"log"

	homedir "github.com/mitchellh/go-homedir"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// 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 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") }

var RootCmd = &cobra.Command{
	Use:   "gemma",
	Short: "gemma is a server for waterway monitoring and management",
}

func init() {
	cobra.OnInitialize(initConfig)
	fl := RootCmd.PersistentFlags()
	fl.StringVarP(&configFile, "config", "c", "", "config file (default is $HOME/.gemma.toml)")

	fl.StringP("dbhost", "H", "localhost", "host of the database")
	fl.UintP("dbport", "P", 5432, "port of the database")
	fl.StringP("dbname", "d", "gemma", "name of the database")
	fl.StringP("dbssl", "S", "prefer", "SSL mode of the database")

	fl.StringP("sessions", "s", "", "path to the sessions file")

	fl.StringP("web", "w", "", "path to the web files")
	fl.StringP("host", "o", "localhost", "host of the web app")
	fl.UintP("port", "p", 8000, "port of the web app")

	fl.String("service-user", "postgres", "user to do service tasks")
	fl.String("service-password", "", "password of user to do service tasks")

	fl.String("mail-host", "localhost", "server to send mail with")
	fl.Uint("mail-port", 465, "port of server to send mail with")
	fl.String("mail-user", "gemma", "user to send mail with")
	fl.String("mail-password", "", "password of user to send mail with")
	fl.String("mail-from", "noreplay@localhost", "from line of mails")
	fl.String("mail-helo", "localhost", "name of server to send mail from.")

	fl.StringSlice("allowed-origins", []string{"foo.org"}, "allow access for remote origins")

	vbind := func(name string) { viper.BindPFlag(name, fl.Lookup(name)) }

	vbind("dbhost")
	vbind("dbport")
	vbind("dbname")
	vbind("dbssl")

	vbind("sessions")

	vbind("web")
	vbind("host")
	vbind("port")

	vbind("service-user")
	vbind("service-password")

	vbind("mail-host")
	vbind("mail-port")
	vbind("mail-user")
	vbind("mail-password")
	vbind("mail-from")
	vbind("mail-helo")

	vbind("allowed-origins")
}

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)
	}
}