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.

50 lines
1.1 KiB

  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. }
  15. }
  16. var C Config
  17. func MustRead(c *cli.Context) error {
  18. viper.SetConfigType("yaml")
  19. viper.SetConfigName("config")
  20. viper.AddConfigPath(".") // adding home directory as first search path
  21. viper.SetEnvPrefix("gogame") // so viper.AutomaticEnv will get matching envvars starting with O2M_
  22. viper.AutomaticEnv() // read in environment variables that match
  23. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  24. if c.GlobalString("config") != "" {
  25. viper.SetConfigFile(c.GlobalString("config"))
  26. }
  27. if err := viper.ReadInConfig(); err != nil {
  28. return err
  29. }
  30. if err := viper.Unmarshal(&C); err != nil {
  31. return err
  32. }
  33. var err error
  34. if C.Id, err = core.IDFromString(C.IdRaw); err != nil {
  35. return err
  36. }
  37. if err := C.KeyStoreBaby.PubKey.UnmarshalText([]byte(C.KeyStoreBaby.PubKeyRaw)); err != nil {
  38. return err
  39. }
  40. C.KeyStoreBaby.PubKeyComp = C.KeyStoreBaby.PubKey.Compress()
  41. return nil
  42. }