forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configs.go
214 lines (185 loc) · 6.59 KB
/
configs.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package config
import (
"encoding/json"
"encoding/xml"
"errors"
"github.com/devfeel/dotweb/core"
"github.com/devfeel/dotweb/framework/file"
"io/ioutil"
)
type (
Config struct {
XMLName xml.Name `xml:"config" json:"-"`
App *AppNode `xml:"app"`
AppSets []*AppSetNode `xml:"appset>set"`
Offline *OfflineNode `xml:"offline"`
Server *ServerNode `xml:"server"`
Session *SessionNode `xml:"session"`
Routers []*RouterNode `xml:"routers>router"`
Groups []*GroupNode `xml:"groups>group"`
Middlewares []*MiddlewareNode `xml:"middlewares>middleware"`
AppSetConfig *core.ItemContext
}
OfflineNode struct {
Offline bool `xml:"offline,attr"` //是否维护,默认false
OfflineText string `xml:"offlinetext,attr"` //当设置为维护,默认显示内容,如果设置url,优先url
OfflineUrl string `xml:"offlineurl,attr"` //当设置为维护,默认维护页地址,如果设置url,优先url
}
AppNode struct {
LogPath string `xml:"logpath,attr"` //文件方式日志目录,如果为空,默认当前目录
EnabledLog bool `xml:"enabledlog,attr"` //是否启用日志记录
RunMode string `xml:"runmode,attr"` //运行模式,目前支持development、production
PProfPort int `xml:"pprofport,attr"` //pprof-server 端口,不能与主Server端口相同
EnabledPProf bool `xml:"enabledpprof,attr"` //是否启用pprof server,默认不启用
}
//update for issue #16 配置文件
AppSetNode struct {
Key string `xml:"key,attr"`
Value string `xml:"value,attr"`
}
ServerNode struct {
EnabledListDir bool `xml:"enabledlistdir,attr"` //设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
EnabledGzip bool `xml:"enabledgzip,attr"` //是否启用gzip
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
Port int `xml:"port,attr"` //端口
}
SessionNode struct {
EnabledSession bool `xml:"enabled,attr"` //启用Session
SessionMode string `xml:"mode,attr"` //session模式,目前支持runtime、redis
Timeout int64 `xml:"timeout,attr"` //session超时时间,分为单位
ServerIP string `xml:"serverip,attr"` //远程session serverip
UserName string `xml:"username,attr"` //远程session username
Password string `xml:"password,attr"` //远程session password
}
RouterNode struct {
Method string `xml:"method,attr"`
Path string `xml:"path,attr"`
HandlerName string `xml:"handler,attr"`
Middlewares []*MiddlewareNode `xml:"middleware"`
IsUse bool `xml:"isuse,attr"` //是否启用,默认false
}
GroupNode struct {
Path string `xml:"path,attr"`
Routers []*RouterNode `xml:"router"`
Middlewares []*MiddlewareNode `xml:"middleware"`
IsUse bool `xml:"isuse,attr"` //是否启用,默认false
}
MiddlewareNode struct {
Name string `xml:"name,attr"`
IsUse bool `xml:"isuse,attr"` //是否启用,默认false
}
)
const (
ConfigType_Xml = "xml"
ConfigType_Json = "json"
)
func NewConfig() *Config {
return &Config{
App: NewAppNode(),
Offline: NewOfflineNode(),
Server: NewServerNode(),
Session: NewSessionNode(),
AppSetConfig: core.NewItemContext(),
}
}
func NewAppNode() *AppNode {
config := &AppNode{}
return config
}
func NewOfflineNode() *OfflineNode {
config := &OfflineNode{}
return config
}
func NewServerNode() *ServerNode {
config := &ServerNode{}
return config
}
func NewSessionNode() *SessionNode {
config := &SessionNode{}
return config
}
//init config file
//If an exception occurs, will be panic it
func MustInitConfig(configFile string, confType ...interface{}) *Config {
conf, err := InitConfig(configFile, confType...)
if err != nil {
panic(err)
}
return conf
}
//初始化配置文件
//如果发生异常,返回异常
func InitConfig(configFile string, confType ...interface{}) (config *Config, err error) {
//检查配置文件有效性
//1、按绝对路径检查
//2、尝试在当前进程根目录下寻找
//3、尝试在当前进程根目录/config/ 下寻找
//fixed for issue #15 读取配置文件路径
realFile := configFile
if !file.Exist(realFile) {
realFile = file.GetCurrentDirectory() + "/" + configFile
if !file.Exist(realFile) {
realFile = file.GetCurrentDirectory() + "/config/" + configFile
if !file.Exist(realFile) {
return nil, errors.New("no exists config file => " + configFile)
}
}
}
cType := ConfigType_Xml
if len(confType) > 0 && confType[0] == ConfigType_Json {
cType = ConfigType_Json
}
if cType == ConfigType_Xml {
config, err = initXmlConfig(realFile)
} else {
config, err = initJsonConfig(realFile)
}
if err != nil {
return config, err
}
if config.App == nil {
config.App = NewAppNode()
}
if config.Server == nil {
config.Server = NewServerNode()
}
if config.Session == nil {
config.Session = NewSessionNode()
}
if config.Offline == nil {
config.Offline = NewOfflineNode()
}
tmpAppSetMap := core.NewItemContext()
for _, v := range config.AppSets {
tmpAppSetMap.Set(v.Key, v.Value)
}
config.AppSetConfig = tmpAppSetMap
return config, nil
}
//初始化配置文件(xml)
func initXmlConfig(configFile string) (*Config, error) {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.New("DotWeb:Config:InitXmlConfig 配置文件[" + configFile + "]无法解析 - " + err.Error())
}
var config *Config
err = xml.Unmarshal(content, &config)
if err != nil {
return nil, errors.New("DotWeb:Config:InitXmlConfig 配置文件[" + configFile + "]解析失败 - " + err.Error())
}
return config, nil
}
//初始化配置文件(json)
func initJsonConfig(configFile string) (*Config, error) {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.New("DotWeb:Config:InitJsonConfig 配置文件[" + configFile + "]无法解析 - " + err.Error())
}
var config *Config
err = json.Unmarshal(content, &config)
if err != nil {
return nil, errors.New("DotWeb:Config:InitJsonConfig 配置文件[" + configFile + "]解析失败 - " + err.Error())
}
return config, nil
}