Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
143 lines (126 loc) · 2.66 KB
/
Copy pathconfig.go
File metadata and controls
143 lines (126 loc) · 2.66 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* This is NOT a freeware, use is subject to license terms
*
* path go-push/config
* date 2018/9/7 10:14
* author chenjingxiu
*/
package config
import (
"encoding/json"
"io/ioutil"
"sync"
"fmt"
"os"
"os/signal"
"syscall"
"path/filepath"
)
type GlobalConfig struct {
Database DB `json:"database"`
Redis RD `json:"redis"`
RabbitMQ MQ `json:"rabbitMQ"`
Mongodb Mog `json:"mongodb"`
Push Secret `json:"push"`
Api Inter `json:"api"`
}
type DB struct {
Default DBItem `json:"default"`
}
type DBItem struct {
Driver string `json:"driver"`
Dsn string `json:"dsn"`
Prefix string `json:"prefix"`
MaxOpen int `json:"maxOpen"`
MaxIdle int `json:"maxIdle"`
ShowSQl bool `json:"showSQL"`
}
type RD struct {
Default RDItem `json:"default"`
Master RDItem `json:"master"`
Slave RDItem `json:"slave"`
}
type RDItem struct {
Url string `json:"url"`
}
type MQ struct {
Default MQItem `json:"default"`
}
type MQItem struct {
Name string `json:"name"`
Url string `json:"url"`
RoutingKey string `json:"routingKey"`
Dsn string `json:"dsn"`
VirtualHost string `json:"virtualHost"`
}
type Secret struct {
HuaWei SecretItem `json:"HW"`
Mi SecretItem `json:"MI"`
}
type SecretItem struct {
Name string `json:"name"`
AppId string `json:"appId"`
AppSecret string `json:"appSecret"`
Icon string `json:"icon"`
IntentHead string `json:"intentHead"`
IntentTail string `json:"intentTail"`
Package string `json:"package"`
}
type Mog struct {
Default MogItem `json:"default"`
}
type MogItem struct {
Url string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
DBName string `json:"dbName"`
Dsn string `json:"dsn"`
}
type Inter struct {
Url map[string]string `json:"url"`
}
var (
GConfig *GlobalConfig
configLock = new(sync.Mutex)
)
func init() {
fileName, err := filepath.Abs("config.json")
if err != nil {
panic(err)
}
err = loadConfig(fileName)
if err != nil {
panic(err)
}
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT)
go func() {
for {
<-s
loadConfig(fileName)
fmt.Println("reload the config file")
}
}()
}
func GetConfig() *GlobalConfig {
configLock.Lock()
defer configLock.Unlock()
return GConfig
}
func loadConfig(file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("read config file error: ", err)
return err
}
config := new(GlobalConfig)
err = json.Unmarshal(data, &config)
if err != nil {
fmt.Println("Unmarshal the config file to struct error: ", err)
return err
}
configLock.Lock()
GConfig = config
configLock.Unlock()
return nil
}