/
config.go
114 lines (103 loc) · 2.65 KB
/
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
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
package models
import (
"sync"
"log"
"encoding/json"
"os"
"io/ioutil"
"strings"
)
type HttpConfig struct {
Port int `json:"port"`
Token string `json:"token"`
AuthDisable string `json:"auth_disable"`
}
type CacheConfig struct {
WorkspaceDir string `json:"workspace_dir"`
CleanInterval int `json:"clean_interval"`
}
type MonitorConfig struct {
Enable bool `json:"enable"`
BaseUrl string `json:"base_url"`
TokenKey string `json:"token_key"`
TokenValue string `json:"token_value"`
}
type DataSourceConfig struct {
Monitor MonitorConfig `json:"monitor"`
}
type MysqlConfig struct {
Type string `json:"type"`
Server string `json:"server"`
Port string `json:"port"`
User string `json:"user"`
Password string `json:"password"`
DataBase string `json:"database"`
MaxOpen int `json:"maxOpen"`
MaxIdle int `json:"maxIdle"`
Timeout int `json:"timeout"`
}
type LogConfig struct {
Level string `json:"level"`
File string `json:"file"`
AccessFile string `json:"access_file"`
ArchiveMaxSize int `json:"archive_max_size"`
ArchiveMaxBackup int `json:"archive_max_backup"`
ArchiveMaxDay int `json:"archive_max_day"`
Compress bool `json:"compress"`
}
type GlobalConfig struct {
Http HttpConfig `json:"http"`
Log LogConfig `json:"log"`
Mysql MysqlConfig `json:"mysql"`
Cache CacheConfig `json:"cache"`
DataSource DataSourceConfig `json:"data_source"`
}
var (
config *GlobalConfig
lock = new(sync.RWMutex)
CoreJwtKey string
SubSystemCode string
SubSystemKey string
)
func Config() *GlobalConfig {
lock.RLock()
defer lock.RUnlock()
return config
}
func InitConfig(cfg string) error {
if cfg == "" {
log.Println("use -c to specify configuration file")
}
_, err := os.Stat(cfg)
if os.IsExist(err) {
log.Println("config file not found")
return err
}
b,err := ioutil.ReadFile(cfg)
if err != nil {
log.Printf("read file %s error %v \n", cfg, err)
return err
}
configContent := strings.TrimSpace(string(b))
var c GlobalConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Println("parse config file:", cfg, "fail:", err)
return err
}
c.Mysql.Password = DecryptRsa(c.Mysql.Password)
lock.Lock()
config = &c
log.Println("read config file:", cfg, "successfully")
lock.Unlock()
initWorkspaceDir()
CoreJwtKey = DecryptRsa(os.Getenv("JWT_SIGNING_KEY"))
SubSystemCode = os.Getenv("SUB_SYSTEM_CODE")
SubSystemKey = os.Getenv("SUB_SYSTEM_KEY")
if config.DataSource.Monitor.BaseUrl != "" && SubSystemCode != "" && SubSystemKey != "" {
InitCoreToken()
}else{
log.Printf("Init core token fail,coreUrl & subSystemCode & subSystemKey can not empty")
}
return nil
}