gogame-old/config/config.go
config/config.goRaw
1package config
2
3import (
4 "strings"
5
6 "github.com/spf13/viper"
7 "github.com/urfave/cli"
8)
9
10type Config struct {
11 Server struct {
12 ServiceApi string
13 AdminApi string
14 }
15 Mongodb struct {
16 Url string
17 }
18}
19
20var C Config
21
22func MustRead(c *cli.Context) error {
23
24 viper.SetConfigType("yaml")
25 viper.SetConfigName("config")
26 viper.AddConfigPath(".") // adding home directory as first search path
27 viper.SetEnvPrefix("gogame") // so viper.AutomaticEnv will get matching envvars starting with O2M_
28 viper.AutomaticEnv() // read in environment variables that match
29 viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
30
31 if c.GlobalString("config") != "" {
32 viper.SetConfigFile(c.GlobalString("config"))
33 }
34
35 if err := viper.ReadInConfig(); err != nil {
36 return err
37 }
38 if err := viper.Unmarshal(&C); err != nil {
39 return err
40 }
41 var err error
42 if C.Id, err = core.IDFromString(C.IdRaw); err != nil {
43 return err
44 }
45 if err := C.KeyStoreBaby.PubKey.UnmarshalText([]byte(C.KeyStoreBaby.PubKeyRaw)); err != nil {
46 return err
47 }
48 C.KeyStoreBaby.PubKeyComp = C.KeyStoreBaby.PubKey.Compress()
49 return nil
50}