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.

44 lines
922 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. CorsOriginsAllowed string
  12. }
  13. Mongodb struct {
  14. Url string
  15. Database string
  16. }
  17. }
  18. var C Config
  19. func MustRead(c *cli.Context) error {
  20. viper.SetConfigType("yaml")
  21. viper.SetConfigName("config")
  22. viper.AddConfigPath(".") // adding home directory as first search path
  23. viper.SetEnvPrefix("gogame") // so viper.AutomaticEnv will get matching envvars starting with O2M_
  24. viper.AutomaticEnv() // read in environment variables that match
  25. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  26. if c.GlobalString("config") != "" {
  27. viper.SetConfigFile(c.GlobalString("config"))
  28. }
  29. if err := viper.ReadInConfig(); err != nil {
  30. return err
  31. }
  32. if err := viper.Unmarshal(&C); err != nil {
  33. return err
  34. }
  35. return nil
  36. }