liuxiaolong
2020-05-27 0b026a39029cf04954e2fc2ffe52e40720a7faa7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package config
 
import (
    "log"
    "github.com/spf13/viper"
)
 
type Db struct {
    User string `mapstructure:"user"`
    Shop string `mapstructure:"shop"`
    Device string `mapstructure:"device"`
    Log string     `mapstructure:"log"`
}
 
type LogConfig struct {
    Path string `mapstructure:"path"`
    Level int `mapstructure:"level"`
}
 
type ServiceConfig struct {
    User string `mapstructure:"user"`
    Shop string `mapstructure:"shop"`
    Device string `mapstructure:"device"`
    Log  string   `mapstructure:"log"`
    Version string `mapstructure:"latest"`
}
 
type RegistryConfig struct {
    Addrs []string `mapstructure:"addrs"`
}
 
type BrokerConfig struct {
    Addrs []string `mapstructure:"addrs"`
}
 
type TraceConfig struct {
    Addr string `mapstructure:"addr"`
}
 
type RedisConfig struct {
    Ip string `mapstructure:"ip"`
    Port int `mapstructure:"port"`
}
 
var DbConf = &Db{}
var LogConf = &LogConfig{}
var ServiceConf = &ServiceConfig{}
var RegistryConf = &RegistryConfig{}
var BrokerConf = &BrokerConfig{}
var TracingConf = &TraceConfig{}
var RedisConf = &RedisConfig{}
 
func init() {
    var err error
    viper.SetConfigType("yaml")
    viper.SetConfigName("config")
    viper.AddConfigPath("./config/")
    err = viper.ReadInConfig()
    if err != nil {
        log.Fatal("error on parsing configuration file:", err)
    }
    viper.UnmarshalKey("db", DbConf)
    viper.UnmarshalKey("log", LogConf)
    viper.UnmarshalKey("service", ServiceConf)
    viper.UnmarshalKey("registry", RegistryConf)
    viper.UnmarshalKey("broker", BrokerConf)
    viper.UnmarshalKey("trace", TracingConf)
    viper.UnmarshalKey("redis", RedisConf)
}