forked from LeeJiangWei/go-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_config.go
51 lines (42 loc) · 915 Bytes
/
read_config.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
package util
import (
"github.com/spf13/viper"
"log"
"time"
)
var EnableRedis bool
var JWTConfig jwtConfigS
var ServerPort int
type jwtConfigS struct {
Issuer string
Secret string
Expire time.Duration
}
func ReadConfig() (err error) {
vp := viper.New()
vp.SetConfigFile("config.yaml")
err = vp.ReadInConfig()
if err != nil {
log.Println("Can not find config file. Generating default config.")
EnableRedis = false
JWTConfig = jwtConfigS{
Issuer: "go-message-pusher",
Secret: "GentleComet",
Expire: 7200 * time.Second,
}
ServerPort = 80
vp.Set("EnableRedis", false)
vp.Set("JWT", JWTConfig)
vp.Set("Port", ServerPort)
err = vp.SafeWriteConfigAs("config.yaml")
return err
}
err = vp.UnmarshalKey("JWT", &JWTConfig)
if err != nil {
return err
}
JWTConfig.Expire *= time.Second
EnableRedis = vp.GetBool("EnableRedis")
ServerPort = vp.GetInt("Port")
return nil
}