github.com/glide-im/glide@v1.6.0/config/viper.go (about)

     1  package config
     2  
     3  import "github.com/spf13/viper"
     4  
     5  var (
     6  	Common    *CommonConf
     7  	MySql     *MySqlConf
     8  	WsServer  *WsServerConf
     9  	IMService *IMRpcServerConf
    10  	Redis     *RedisConf
    11  	Kafka     *KafkaConf
    12  )
    13  
    14  type CommonConf struct {
    15  	StoreOfflineMessage bool
    16  	StoreMessageHistory bool
    17  	SecretKey           string
    18  }
    19  
    20  type WsServerConf struct {
    21  	ID        string
    22  	Addr      string
    23  	Port      int
    24  	JwtSecret string
    25  }
    26  
    27  type ApiHttpConf struct {
    28  	Addr string
    29  	Port int
    30  }
    31  
    32  type IMRpcServerConf struct {
    33  	Addr    string
    34  	Port    int
    35  	Network string
    36  	Etcd    []string
    37  	Name    string
    38  }
    39  
    40  type KafkaConf struct {
    41  	Address []string
    42  }
    43  
    44  type MySqlConf struct {
    45  	Host     string
    46  	Port     int
    47  	Username string
    48  	Password string
    49  	Db       string
    50  	Charset  string
    51  }
    52  
    53  type RedisConf struct {
    54  	Host     string
    55  	Port     int
    56  	Password string
    57  	Db       int
    58  }
    59  
    60  func MustLoad() {
    61  
    62  	viper.SetConfigName("config.toml")
    63  	viper.SetConfigType("toml")
    64  	viper.AddConfigPath(".")
    65  	viper.AddConfigPath("./_config_local")
    66  	viper.AddConfigPath("./config")
    67  	viper.AddConfigPath("/etc/")
    68  	viper.AddConfigPath("$HOME/.config/")
    69  
    70  	err := viper.ReadInConfig()
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  	c := struct {
    75  		MySql       *MySqlConf
    76  		Redis       *RedisConf
    77  		WsServer    *WsServerConf
    78  		IMRpcServer *IMRpcServerConf
    79  		CommonConf  *CommonConf
    80  		Kafka       *KafkaConf
    81  	}{}
    82  
    83  	err = viper.Unmarshal(&c)
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	MySql = c.MySql
    88  	WsServer = c.WsServer
    89  	IMService = c.IMRpcServer
    90  	Common = c.CommonConf
    91  	Redis = c.Redis
    92  	Kafka = c.Kafka
    93  
    94  	if Common == nil {
    95  		panic("CommonConf is nil")
    96  	}
    97  	if c.MySql == nil {
    98  		panic("mysql config is nil")
    99  	}
   100  	if c.WsServer == nil {
   101  		panic("ws server config is nil")
   102  	}
   103  	if c.IMRpcServer == nil {
   104  		panic("im rpc server config is nil")
   105  	}
   106  }