-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
62 lines (58 loc) · 1.66 KB
/
conf.go
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
package utils
import (
"github.com/spf13/viper"
"path/filepath"
"strings"
)
type Conf struct {
Addr string
MaxCli uint32
MaxWorker uint32
MaxHandleQueue uint32
MaxSendQueue uint32
SendTimeOut uint32
LogPath string
LogLevel uint32
}
func (c *Conf) Load(file string) {
viper.SetConfigFile(strings.Split(filepath.Base(file), ".")[0])
viper.AddConfigPath(filepath.Dir(file))
if err := viper.ReadInConfig(); err != nil {
c.SetDefalt()
}
cfg := viper.GetViper()
if c.Addr = cfg.GetString("tcpServer.addr"); c.Addr == "" {
c.Addr = DefaultAddr
}
if c.MaxCli = cfg.GetUint32("tcpServer.max_cli"); c.MaxCli == 0 {
c.MaxCli = DefaultMaxCli
}
if c.MaxWorker = cfg.GetUint32("tcpServer.max_worker"); c.MaxWorker == 0 {
c.MaxWorker = DefaultMaxWorker
}
if c.MaxHandleQueue = cfg.GetUint32("tcpServer.max_handle_queue"); c.MaxHandleQueue == 0 {
c.MaxHandleQueue = DefaultMaxHandleQueue
}
if c.MaxSendQueue = cfg.GetUint32("tcpServer.max_send_queue"); c.MaxSendQueue == 0 {
c.MaxSendQueue = DefaultMaxSendQueue
}
if c.SendTimeOut = cfg.GetUint32("tcpServer.send_timeout"); c.SendTimeOut == 0 {
c.SendTimeOut = DefaultSendTimeOut
}
if c.LogPath = cfg.GetString("tcpServer.logpath"); c.LogPath == "" {
c.LogPath = DefaultLogPath
}
if c.LogLevel = cfg.GetUint32("tcpServer.loglevel"); c.LogLevel == 0 {
c.LogLevel = DefaultLogLevel
}
}
func (c *Conf) SetDefalt() {
c.Addr = DefaultAddr
c.MaxCli = DefaultMaxCli
c.MaxWorker = DefaultMaxWorker
c.MaxHandleQueue = DefaultMaxHandleQueue
c.MaxSendQueue = DefaultMaxSendQueue
c.SendTimeOut = DefaultSendTimeOut
c.LogPath = DefaultLogPath
c.LogLevel = DefaultLogLevel
}