sunty
2024-09-03 b0f374409775bd30ed8f0dc8d47d015d3edc0abb
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
package config
 
import (
    "basic.com/valib/logger.git"
    "github.com/spf13/viper"
    "log"
)
 
type LogConfig struct {
    Path       string `mapstructure:"path"`       //日志存储路径
    Level      int    `mapstructure:"level"`      //日志等级
    MaxSize    int    `mapstructure:"maxSize"`    //日志文件大小上限
    MaxBackups int    `mapstructure:"maxBackups"` //日志压缩包个数
    MaxAge     int    `mapstructure:"maxAge"`     //保留压缩包天数
}
 
type database struct {
    Driver   string `mapstructure: "driver"`
    Host     string `mapstructure: "host"`
    Port     string `mapstructure: "port"`
    Name     string `mapstructure: "name"`
    Username string `mapstructure: "username"`
    Password string `mapstructure: "password"`
}
 
type elastic struct {
    Host         string `mapstructure: "host"`
    Port         string `mapstructure: "port"`
    Index        string `mapstructure: "index"`
    Type         string `mapstructure: "type"`
    DocumentSize int    `mapstructure: "documentSize"`
    TopHitsSize  int    `mapstructure: "topHitsSize"`
    CameraSize   int    `mapstructure: "cameraSize"`
    TimeInterval int    `mapstructure: "timeInterval"`
    BatchSize    int    `mapstructure: "batchSize"`
}
 
type app struct {
    Name     string `mapstructure: "Annotation Service Application"`
    Port     string `mapstructure: "port"`
    LogLevel string `mapstructure: "logLevel"`
}
 
type api struct {
    Host          string `mapstructure: "host"`
    Port          string `mapstructure: "port"`
    TimeThreshold int    `mapstructure:"timeThreshold"`
    CsTimes       int    `mapstructure:"csTimes"`
    CsHours       int    `mapstructure:"csHours"`
    AInterval     int    `mapstructure:"aInterval"`
}
 
var LogConf = &LogConfig{}
var DataBase = &database{}
var Elastic = &elastic{}
var App = &app{}
var Api = &api{}
var LogBasePath string
var LogLevel int
 
func Init(env string) {
    var err error
    viper.SetConfigType("yaml")
    viper.SetConfigName(env)
    viper.AddConfigPath("./")
    viper.AddConfigPath("./config")
    viper.AddConfigPath("../config")
    err = viper.ReadInConfig()
    if err != nil {
        log.Fatal("error on parsing configuration file", err)
    }
    viper.UnmarshalKey("elastic", Elastic)
    viper.UnmarshalKey("database", DataBase)
    viper.UnmarshalKey("app", App)
    viper.UnmarshalKey("api", Api)
    viper.UnmarshalKey("log", LogConf)
    logger.SetLevel(LogConf.Level)
    if viper.GetString("LogBasePath") != "" {
        LogBasePath = viper.GetString("LogBasePath")
    } else {
        LogBasePath = "./logger/"
    }
 
    if viper.IsSet("LogLevel") && viper.GetInt("LogLevel") >= logger.PanicLevel && viper.GetInt("LogLevel") <= logger.DebugLevel {
        LogLevel = viper.GetInt("LogLevel")
    } else {
        LogLevel = logger.DebugLevel
    }
}