You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
878 B

  1. package config
  2. import (
  3. "strings"
  4. "github.com/spf13/viper"
  5. "github.com/urfave/cli"
  6. )
  7. type Config struct {
  8. Server struct {
  9. ServiceApi string
  10. AdminApi string
  11. }
  12. Mongodb struct {
  13. Url string
  14. Database string
  15. }
  16. }
  17. var C Config
  18. func MustRead(c *cli.Context) error {
  19. viper.SetConfigType("yaml")
  20. viper.SetConfigName("config")
  21. viper.AddConfigPath(".") // adding home directory as first search path
  22. viper.SetEnvPrefix("gogame") // so viper.AutomaticEnv will get matching envvars starting with O2M_
  23. viper.AutomaticEnv() // read in environment variables that match
  24. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  25. if c.GlobalString("config") != "" {
  26. viper.SetConfigFile(c.GlobalString("config"))
  27. }
  28. if err := viper.ReadInConfig(); err != nil {
  29. return err
  30. }
  31. if err := viper.Unmarshal(&C); err != nil {
  32. return err
  33. }
  34. return nil
  35. }