Skip to content

Commit ba4777b

Browse files
author
pzrr@qq.com
committed
#### Version 1.4.5
* 新增yaml格式配置文件支持,具体参考 example/config/dotweb.yaml * config新增UnmarshalYaml\MarshalYaml\MarshalYamlString,提供针对Yaml的常规处理 * config新增UnmarshalXml\MarshalXml\MarshalXmlString,提供针对Xml的常规处理 * config新增UnmarshalJson\MarshalJson\MarshalJsonString,提供针对Json的常规处理 * UploadFile新增Bytes接口,用于返回上传文件本身 * 完善 example/config * 移除 example/session,查看更多示例,请移步https://github.com/devfeel/dotweb-example * 2018-01-20 23:40
1 parent a31b7df commit ba4777b

File tree

12 files changed

+387
-271
lines changed

12 files changed

+387
-271
lines changed

config/config_json.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ package config
22

33
import "encoding/json"
44

5-
func fromJson(content []byte,v interface{}) error {
5+
// UnmarshalJson parses the JSON-encoded data and stores the result
6+
// in the value pointed to by v.
7+
func UnmarshalJson(content []byte, v interface{}) error {
68
return json.Unmarshal(content, v)
79
}
10+
11+
// MarshalJson returns the JSON encoding of v.
12+
func MarshalJson(v interface{}) (out []byte, err error) {
13+
return json.Marshal(v)
14+
}
15+
16+
// MarshalJsonString returns the JSON encoding string format of v.
17+
func MarshalJsonString(v interface{}) (out string) {
18+
marshal, err := json.Marshal(v)
19+
if err != nil {
20+
return ""
21+
}
22+
return string(marshal)
23+
}

config/config_xml.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ import (
44
"encoding/xml"
55
)
66

7-
func fromXml(content []byte,v interface{}) error {
7+
// UnmarshalXml parses the XML-encoded data and stores the result in
8+
// the value pointed to by v, which must be an arbitrary struct,
9+
// slice, or string. Well-formed data that does not fit into v is
10+
// discarded.
11+
func UnmarshalXml(content []byte, v interface{}) error {
812
return xml.Unmarshal(content, v)
9-
}
13+
}
14+
15+
// MarshalXml returns the XML encoding of v.
16+
func MarshalXml(v interface{}) (out []byte, err error) {
17+
return xml.Marshal(v)
18+
}
19+
20+
// MarshalXmlString returns the XML encoding string format of v.
21+
func MarshalXmlString(v interface{}) (out string) {
22+
marshal, err := xml.Marshal(v)
23+
if err != nil {
24+
return ""
25+
}
26+
return string(marshal)
27+
}

config/config_yaml.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package config
2+
3+
import (
4+
"gopkg.in/yaml.v2"
5+
)
6+
7+
// UnmarshalYaml decodes the first document found within the in byte slice
8+
// and assigns decoded values into the out value.
9+
// For example:
10+
//
11+
// type T struct {
12+
// F int `yaml:"a,omitempty"`
13+
// B int
14+
// }
15+
// var t T
16+
// yaml.Unmarshal([]byte("a: 1\nb: 2"), &t)
17+
func UnmarshalYaml(content []byte, v interface{}) error {
18+
return yaml.Unmarshal(content, v)
19+
}
20+
21+
// MarshalYaml Marshal serializes the value provided into a YAML document.
22+
// For example:
23+
//
24+
// type T struct {
25+
// F int "a,omitempty"
26+
// B int
27+
// }
28+
// yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
29+
// yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
30+
func MarshalYaml(v interface{}) (out []byte, err error) {
31+
return yaml.Marshal(v)
32+
}
33+
34+
// MarshalYamlString returns the Ymal encoding string format of v.
35+
func MarshalYamlString(v interface{}) (out string) {
36+
marshal, err := yaml.Marshal(v)
37+
if err != nil {
38+
return ""
39+
}
40+
return string(marshal)
41+
}

config/configs.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"github.com/devfeel/dotweb/core"
77
"github.com/devfeel/dotweb/framework/file"
88
"io/ioutil"
9-
//"time"
109
)
1110

1211
type (
1312
Config struct {
14-
XMLName xml.Name `xml:"config" json:"-"`
13+
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
1514
App *AppNode `xml:"app"`
1615
AppSets []*AppSetNode `xml:"appset>set"`
1716
Offline *OfflineNode `xml:"offline"`
@@ -20,7 +19,7 @@ type (
2019
Routers []*RouterNode `xml:"routers>router"`
2120
Groups []*GroupNode `xml:"groups>group"`
2221
Middlewares []*MiddlewareNode `xml:"middlewares>middleware"`
23-
AppSetConfig *core.ItemContext
22+
AppSetConfig *core.ItemContext `json:"-" yaml:"-"`
2423
}
2524
OfflineNode struct {
2625
Offline bool `xml:"offline,attr"` //是否维护,默认false
@@ -47,7 +46,7 @@ type (
4746
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
4847
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
4948
EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` //设置是否忽略favicon.ico请求,若设置,网站将把所有favicon.ico请求直接空返回
50-
EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
49+
EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
5150
Port int `xml:"port,attr"` //端口
5251
EnabledTLS bool `xml:"enabledtls,attr"` //是否启用TLS模式
5352
TLSCertFile string `xml:"tlscertfile,attr"` //TLS模式下Certificate证书文件地址
@@ -89,6 +88,7 @@ type (
8988
const (
9089
ConfigType_Xml = "xml"
9190
ConfigType_Json = "json"
91+
ConfigType_Yaml = "yaml"
9292
)
9393

9494
func NewConfig() *Config {
@@ -155,11 +155,16 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
155155
if len(confType) > 0 && confType[0] == ConfigType_Json {
156156
cType = ConfigType_Json
157157
}
158+
if len(confType) > 0 && confType[0] == ConfigType_Yaml {
159+
cType = ConfigType_Yaml
160+
}
158161

159162
if cType == ConfigType_Xml {
160-
config, err = initConfig(realFile, cType, fromXml)
163+
config, err = initConfig(realFile, cType, UnmarshalXml)
164+
} else if cType == ConfigType_Yaml {
165+
config, err = initConfig(realFile, cType, UnmarshalYaml)
161166
} else {
162-
config, err = initConfig(realFile, cType, fromJson)
167+
config, err = initConfig(realFile, cType, UnmarshalJson)
163168
}
164169

165170
if err != nil {
@@ -198,14 +203,14 @@ func dealConfigDefaultSet(c *Config) {
198203

199204
}
200205

201-
func initConfig(configFile string, ctType string, f func([]byte, interface{}) error) (*Config, error) {
206+
func initConfig(configFile string, ctType string, parser func([]byte, interface{}) error) (*Config, error) {
202207
content, err := ioutil.ReadFile(configFile)
203208
if err != nil {
204209
return nil, errors.New("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]无法解析 - " + err.Error())
205210
}
206211

207212
var config *Config
208-
err = f(content, &config)
213+
err = parser(content, &config)
209214
if err != nil {
210215
return nil, errors.New("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]解析失败 - " + err.Error())
211216
}

example/config/dotweb.conf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<config>
33
<app logpath="d:/gotmp/" enabledlog="true" runmode="development"/>
4-
<offline offline="false" offlinetext="server is offline!" offlineurl="" />
5-
<server isrun="true" indexpage="index.html" port="8080" enabledgzip="false" enabledlistdir="false" enabledautohead="true" requesttimeout="30000/>
6-
<session enabled="true" mode="runtime" timeout="20"/>
4+
<server isrun="true" indexpage="index.html" port="8080" enabledgzip="false" enabledlistdir="false" enabledautohead="true" requesttimeout="30000"/>
5+
<session enabled="true" mode="runtime" timeout="20" />
76
<appset>
87
<set key="set1" value="1" />
98
<set key="set2" value="2" />

example/config/dotweb.json

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"App": {
3+
"LogPath": "d:/gotmp/",
4+
"EnabledLog": true,
5+
"RunMode": "development",
6+
"PProfPort": 0,
7+
"EnabledPProf": false
8+
},
9+
"AppSets": [{
10+
"Key": "set1",
11+
"Value": "1"
12+
}, {
13+
"Key": "set2",
14+
"Value": "2"
15+
}, {
16+
"Key": "set3",
17+
"Value": "3"
18+
}, {
19+
"Key": "set4",
20+
"Value": "4"
21+
}],
22+
"Offline": {
23+
"Offline": false,
24+
"OfflineText": "",
25+
"OfflineUrl": ""
26+
},
27+
"Server": {
28+
"EnabledListDir": false,
29+
"EnabledRequestID": false,
30+
"EnabledGzip": false,
31+
"EnabledAutoHEAD": true,
32+
"EnabledAutoCORS": false,
33+
"EnabledIgnoreFavicon": false,
34+
"EnabledBindUseJsonTag": false,
35+
"Port": 8080,
36+
"EnabledTLS": false,
37+
"TLSCertFile": "",
38+
"TLSKeyFile": "",
39+
"IndexPage": "index.html",
40+
"EnabledDetailRequestData": false
41+
},
42+
"Session": {
43+
"EnabledSession": true,
44+
"SessionMode": "runtime",
45+
"Timeout": 20,
46+
"ServerIP": "",
47+
"UserName": "",
48+
"Password": ""
49+
},
50+
"Routers": [{
51+
"Method": "GET",
52+
"Path": "/index",
53+
"HandlerName": "Index",
54+
"Middlewares": [{
55+
"Name": "urllog",
56+
"IsUse": true
57+
}],
58+
"IsUse": true
59+
}, {
60+
"Method": "GET",
61+
"Path": "/index2",
62+
"HandlerName": "Index",
63+
"Middlewares": [{
64+
"Name": "urllog",
65+
"IsUse": true
66+
}],
67+
"IsUse": true
68+
}, {
69+
"Method": "GET",
70+
"Path": "/index3",
71+
"HandlerName": "Index",
72+
"Middlewares": [{
73+
"Name": "urllog",
74+
"IsUse": true
75+
}],
76+
"IsUse": true
77+
}, {
78+
"Method": "GET",
79+
"Path": "/redirect",
80+
"HandlerName": "Redirect",
81+
"Middlewares": null,
82+
"IsUse": true
83+
}, {
84+
"Method": "GET",
85+
"Path": "/error",
86+
"HandlerName": "Error",
87+
"Middlewares": null,
88+
"IsUse": true
89+
}, {
90+
"Method": "GET",
91+
"Path": "/panic",
92+
"HandlerName": "Panic",
93+
"Middlewares": null,
94+
"IsUse": true
95+
}, {
96+
"Method": "GET",
97+
"Path": "/appset",
98+
"HandlerName": "appset",
99+
"Middlewares": null,
100+
"IsUse": true
101+
}],
102+
"Groups": [{
103+
"Path": "/admin",
104+
"Routers": [{
105+
"Method": "GET",
106+
"Path": "/login",
107+
"HandlerName": "Login",
108+
"Middlewares": [{
109+
"Name": "urllog",
110+
"IsUse": true
111+
}],
112+
"IsUse": true
113+
}, {
114+
"Method": "GET",
115+
"Path": "/login3",
116+
"HandlerName": "Login",
117+
"Middlewares": null,
118+
"IsUse": true
119+
}, {
120+
"Method": "GET",
121+
"Path": "/logout",
122+
"HandlerName": "Logout",
123+
"Middlewares": null,
124+
"IsUse": true
125+
}, {
126+
"Method": "GET",
127+
"Path": "/login2",
128+
"HandlerName": "Login",
129+
"Middlewares": null,
130+
"IsUse": true
131+
}],
132+
"Middlewares": [{
133+
"Name": "grouplog",
134+
"IsUse": true
135+
}, {
136+
"Name": "simpleauth",
137+
"IsUse": true
138+
}],
139+
"IsUse": true
140+
}],
141+
"Middlewares": [{
142+
"Name": "applog",
143+
"IsUse": true
144+
}]
145+
}

0 commit comments

Comments
 (0)