yinbangzhong
2024-06-20 a5429b8a83e1b4a48c2a3b9b0b475f08a114837c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package conf
 
import (
    "log"
    "os"
    "speechAnalysis/pkg/logx"
    "speechAnalysis/pkg/mysqlx"
 
    "github.com/spf13/viper"
)
 
var (
    // config file name
    configName = "config"
    // config file paths
    configPaths = []string{
        "./",
        "../",
        "./conf",
        "../conf",
    }
)
 
type (
    webConf struct {
        Host      string // 本机ip地址
        Port      string // 端口号
        APPort    string // 本机作为的Grpc服务端的端口号
        AlHost    string // 算法服务ip地址
        AlPort    string // 算法服务端口号
        NodeId    string // 主账户用户名
        OssType   string // 对象存储类型
        JWTSecret string
    }
 
    nsqConf struct {
        NsqdAddr       string
        NsqlookupdAddr string
    }
 
    localConf struct {
        StorePath   string // 本地文件存储路径
        PreLoadPath string // 本地文件预加载路径
    }
    Analysis struct {
        Url string // 本地文件存储路径
    }
)
 
var (
    WebConf      = &webConf{}
    LogConf      = &logx.LogConf{}
    DbConf       = &mysqlx.Conf{}
    NsqConf      = &nsqConf{}
    LocalConf    = &localConf{}
    AanlysisConf = &Analysis{}
    GrpcPort     string
    Viper        *viper.Viper
)
 
func Init() error {
    Viper = viper.New()
    Viper.SetConfigName(configName)
    for _, path := range configPaths {
        Viper.AddConfigPath(path)
    }
 
    if err := Viper.ReadInConfig(); err != nil {
        log.Fatalf("ReadInConfig err:%v", err)
    }
    read2Conf(Viper)
 
    GrpcPort = os.Getenv("GRPC_PORT") // 只给grpc算法服务使用,本服务不用
    nodeId := os.Getenv("NODE_ID")    // 主账户用户名
    host := os.Getenv("HOST")         // 本机IP地址
    algHost := os.Getenv("AL_HOST")   // 算法服务的IP地址
    nsqdAddr := os.Getenv("NSQD_ADDR")
    if len(GrpcPort) == 0 { // 如果gprcPort为空,那么用配置的APPort
        GrpcPort = WebConf.APPort
    }
    if len(nodeId) > 0 {
        WebConf.NodeId = nodeId
    }
    if len(algHost) > 0 {
        WebConf.AlHost = algHost
    }
    if len(host) > 0 {
        WebConf.Host = host
    }
    if len(nsqdAddr) > 0 {
        NsqConf.NsqdAddr = nsqdAddr
    }
 
    DBHost := os.Getenv("DB_HOST")
    DBName := os.Getenv("DB_NAME")
    DBPort := os.Getenv("DB_PORT")
    DBUser := os.Getenv("DB_USER")
    DBPasswd := os.Getenv("DB_PASSWD")
    if len(DBHost) > 0 &&
        len(DBName) > 0 &&
        len(DBPort) > 0 &&
        len(DBUser) > 0 &&
        len(DBPasswd) > 0 {
        DbConf.Dsn = DBUser + ":" + DBPasswd + "@tcp(" + DBHost + ":" + DBPort + ")/" + DBName + "?charset=utf8&parseTime=True&loc=Local"
    }
 
    return nil
}
 
func read2Conf(v *viper.Viper) {
    _ = v.UnmarshalKey("web", WebConf)
    _ = v.UnmarshalKey("log", LogConf)
    _ = v.UnmarshalKey("db", DbConf)
    _ = v.UnmarshalKey("nsq", NsqConf)
    _ = v.UnmarshalKey("local", LocalConf)
    _ = v.UnmarshalKey("analysis", AanlysisConf)
    showConfig()
}
 
func showConfig() {
    log.Println("......................................................")
    log.Printf("   WebConf:                %+v", WebConf)
    log.Printf("   LogConf:                %+v", LogConf)
    log.Printf("   DbConf:                 %+v", DbConf)
    log.Printf("   NsqConf:                %+v", NsqConf)
    log.Printf("   GrpcPort:               %+v", GrpcPort)
    log.Printf("   LocalConf:               %+v", LocalConf)
    log.Printf("   AanlysisConf:               %+v", AanlysisConf)
    log.Println("......................................................")
}