Skip to content

Commit 5057111

Browse files
committed
#### Version 1.4.8
* 调整:Dotweb.AppContext变更为Dotweb.Items * 调整:HttpContext.AppContext变更为HttpContext.AppItems * 新增config.ConfigSet\config.ParseConfigSetXML\ParseConfigSetJSON\ParseConfigSetYaml,用于解析常规Key\Value格式的配置文件 * 新增Dotweb.IncludeConfigSetXML\IncludeConfigSetJSON\IncludeConfigSetYaml,解析配置后置入Dotweb.Items,可通过HttpContext.AppItems().Get()获取 * Include\ParseConfigSetXML:支持xml格式文件解析 * Include\ParseConfigSetJSON:支持json格式文件解析 * Include\ParseConfigSetYaml:支持yaml格式文件解析 * 具体配置文件格式可参考example/configset * 新增示例代码 example/configset * 2018-01-23 19:00
1 parent 54a72a1 commit 5057111

File tree

10 files changed

+186
-73
lines changed

10 files changed

+186
-73
lines changed

config/configs.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ type (
8686
)
8787

8888
const (
89-
ConfigType_Xml = "xml"
90-
ConfigType_Json = "json"
89+
ConfigType_XML = "xml"
90+
ConfigType_JSON = "json"
9191
ConfigType_Yaml = "yaml"
9292
)
9393

@@ -151,15 +151,15 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
151151
}
152152
}
153153

154-
cType := ConfigType_Xml
155-
if len(confType) > 0 && confType[0] == ConfigType_Json {
156-
cType = ConfigType_Json
154+
cType := ConfigType_XML
155+
if len(confType) > 0 && confType[0] == ConfigType_JSON {
156+
cType = ConfigType_JSON
157157
}
158158
if len(confType) > 0 && confType[0] == ConfigType_Yaml {
159159
cType = ConfigType_Yaml
160160
}
161161

162-
if cType == ConfigType_Xml {
162+
if cType == ConfigType_XML {
163163
config, err = initConfig(realFile, cType, UnmarshalXML)
164164
} else if cType == ConfigType_Yaml {
165165
config, err = initConfig(realFile, cType, UnmarshalYaml)

config/configset.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package config
2+
3+
import (
4+
"encoding/xml"
5+
"github.com/devfeel/dotweb/core"
6+
"io/ioutil"
7+
"errors"
8+
)
9+
10+
type(
11+
//单元配置节点
12+
ConfigSetNode struct{
13+
Key string `xml:"key,attr"`
14+
Value string `xml:"value,attr"`
15+
}
16+
17+
//单元配置组,包含一系列单元配置节点
18+
ConfigSet struct{
19+
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
20+
Name string `xml:"name,attr"`
21+
ConfigSetNodes []*ConfigSetNode `xml:"set"`
22+
}
23+
)
24+
25+
// ParseConfigSetXML include ConfigSet xml file
26+
func ParseConfigSetXML(configFile string) (*core.ItemContext, error){
27+
return parseConfigSetFile(configFile, ConfigType_XML)
28+
}
29+
30+
// ParseConfigSetJSON include ConfigSet json file
31+
func ParseConfigSetJSON(configFile string) (*core.ItemContext, error){
32+
return parseConfigSetFile(configFile, ConfigType_JSON)
33+
}
34+
35+
// ParseConfigSetYaml include ConfigSet yaml file
36+
func ParseConfigSetYaml(configFile string) (*core.ItemContext, error){
37+
return parseConfigSetFile(configFile, ConfigType_Yaml)
38+
}
39+
40+
func parseConfigSetFile(configFile string, confType string) (*core.ItemContext, error){
41+
content, err := ioutil.ReadFile(configFile)
42+
if err != nil {
43+
return nil, errors.New("DotWeb:Config:parseConfigSetFile 配置文件[" + configFile + ", "+confType+"]无法解析 - " + err.Error())
44+
}
45+
set :=new(ConfigSet)
46+
if confType == ConfigType_XML {
47+
err = UnmarshalXML(content, set)
48+
}
49+
if confType == ConfigType_JSON{
50+
err = UnmarshalJSON(content, set)
51+
}
52+
if confType == ConfigType_Yaml{
53+
err = UnmarshalYaml(content, set)
54+
}
55+
if err!=nil{
56+
return nil, errors.New("DotWeb:Config:parseConfigSetFile 配置文件[" + configFile + ", "+confType+"]无法解析 - " + err.Error())
57+
}
58+
item := core.NewItemContext()
59+
for _,s:=range set.ConfigSetNodes{
60+
item.Set(s.Key, s.Value)
61+
}
62+
return item, nil
63+
}

const/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package _const
22

33
//dotweb const
44
const (
5-
Version = "1.4.7"
5+
Version = "1.4.8"
66
)

context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type (
3939
RouterNode() RouterNode
4040
RouterParams() Params
4141
Handler() HttpHandle
42-
AppContext() *core.ItemContext
42+
AppItems() *core.ItemContext
4343
Cache() cache.Cache
4444
Items() *core.ItemContext
4545
AppSetConfig() *core.ItemContext
@@ -225,9 +225,9 @@ func (ctx *HttpContext) Features() *xFeatureTools {
225225

226226
// AppContext get application's global appcontext
227227
// issue #3
228-
func (ctx *HttpContext) AppContext() *core.ItemContext {
228+
func (ctx *HttpContext) AppItems() *core.ItemContext {
229229
if ctx.HttpServer != nil {
230-
return ctx.httpServer.DotApp.AppContext
230+
return ctx.httpServer.DotApp.Items
231231
} else {
232232
return core.NewItemContext()
233233
}

dotweb.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type (
3333
ExceptionHandler ExceptionHandle
3434
NotFoundHandler StandardHandle // NotFoundHandler 支持自定义404处理代码能力
3535
MethodNotAllowedHandler StandardHandle // MethodNotAllowedHandler fixed for #64 增加MethodNotAllowed自定义处理
36-
AppContext *core.ItemContext
36+
Items *core.ItemContext
3737
middlewareMap map[string]MiddlewareFunc
3838
middlewareMutex *sync.RWMutex
3939
StartMode string
@@ -52,6 +52,7 @@ type (
5252

5353
const (
5454
DefaultHTTPPort = 8080 //DefaultHTTPPort default http port; fixed for #70 UPDATE default http port 80 to 8080
55+
DefaultConfigSetGroupName = "default"
5556
RunMode_Development = "development"
5657
RunMode_Production = "production"
5758

@@ -65,7 +66,7 @@ func New() *DotWeb {
6566
HttpServer: NewHttpServer(),
6667
OfflineServer: servers.NewOfflineServer(),
6768
Middlewares: make([]Middleware, 0),
68-
AppContext: core.NewItemContext(),
69+
Items: core.NewItemContext(),
6970
Config: config.NewConfig(),
7071
middlewareMap: make(map[string]MiddlewareFunc),
7172
middlewareMutex: new(sync.RWMutex),
@@ -122,6 +123,46 @@ func (app *DotWeb) SetCache(ca cache.Cache) {
122123
app.cache = ca
123124
}
124125

126+
// IncludeConfigSetXML include ConfigSet xml file to Dotweb.Items
127+
// same key will cover oldest value
128+
func (app *DotWeb) IncludeConfigSetXML(configFile string) error{
129+
item, err:=config.ParseConfigSetXML(configFile)
130+
if err!= nil{
131+
return err
132+
}
133+
for k,v:=range item.GetCurrentMap(){
134+
app.Items.Set(k, v)
135+
}
136+
return nil
137+
}
138+
139+
// IncludeConfigSetJSON include ConfigSet json file to Dotweb.Items
140+
// same key will cover oldest value
141+
func (app *DotWeb) IncludeConfigSetJSON(configFile string) error{
142+
item, err:=config.ParseConfigSetXML(configFile)
143+
if err!= nil{
144+
return err
145+
}
146+
for k,v:=range item.GetCurrentMap(){
147+
app.Items.Set(k, v)
148+
}
149+
return nil
150+
}
151+
152+
// IncludeConfigSetYaml include ConfigSet ymal file to Dotweb.Items
153+
// same key will cover oldest value
154+
func (app *DotWeb) IncludeConfigSetYaml(configFile string) error{
155+
item, err:=config.ParseConfigSetXML(configFile)
156+
if err!= nil{
157+
return err
158+
}
159+
for k,v:=range item.GetCurrentMap(){
160+
app.Items.Set(k, v)
161+
}
162+
return nil
163+
}
164+
165+
125166
// RunMode current app run mode, if not set, default set RunMode_Development
126167
func (app *DotWeb) RunMode() string {
127168
if app.Config.App.RunMode != RunMode_Development && app.Config.App.RunMode != RunMode_Production {

example/appcontext/main.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

example/configset/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/devfeel/dotweb"
6+
"github.com/devfeel/dotweb/framework/file"
7+
"strconv"
8+
)
9+
10+
func main() {
11+
//初始化DotServer
12+
app := dotweb.New()
13+
14+
//设置dotserver日志目录
15+
app.SetLogPath(file.GetCurrentDirectory())
16+
17+
app.SetDevelopmentMode()
18+
19+
app.HttpServer.SetEnabledIgnoreFavicon(true)
20+
21+
22+
//引入自定义ConfigSet
23+
err := app.IncludeConfigSetXML("d:/gotmp/userconf.xml")
24+
if err!=nil{
25+
fmt.Println(err.Error())
26+
return
27+
}
28+
29+
//设置路由
30+
InitRoute(app.HttpServer)
31+
32+
// 开始服务
33+
port := 8080
34+
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
35+
err = app.StartServer(port)
36+
fmt.Println("dotweb.StartServer error => ", err)
37+
}
38+
39+
func ConfigSet(ctx dotweb.Context) error {
40+
vkey1 := ctx.AppItems().GetString("set1")
41+
vkey2 := ctx.AppItems().GetString("set2")
42+
ctx.WriteString(ctx.Request().Path(), "key1=", vkey1, "key2=", vkey2)
43+
return ctx.WriteString("\r\n")
44+
}
45+
46+
47+
func InitRoute(server *dotweb.HttpServer) {
48+
server.GET("/c", ConfigSet)
49+
}

example/configset/userconf.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config>
3+
<set key="set1" value="1" />
4+
<set key="set2" value="2" />
5+
<set key="set3" value="3" />
6+
<set key="set4" value="4" />
7+
</config>

example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func main() {
6565
app.SetPProfConfig(true, 8081)
6666

6767
//全局容器
68-
app.AppContext.Set("gstring", "gvalue")
69-
app.AppContext.Set("gint", 1)
68+
app.Items.Set("gstring", "gvalue")
69+
app.Items.Set("gint", 1)
7070

7171
// 开始服务
7272
port := 8080

version.MD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
## dotweb版本记录:
22

3+
#### Version 1.4.8
4+
* 调整:Dotweb.AppContext变更为Dotweb.Items
5+
* 调整:HttpContext.AppContext变更为HttpContext.AppItems
6+
* 新增config.ConfigSet\config.ParseConfigSetXML\ParseConfigSetJSON\ParseConfigSetYaml,用于解析常规Key\Value格式的配置文件
7+
* 新增Dotweb.IncludeConfigSetXML\IncludeConfigSetJSON\IncludeConfigSetYaml,解析配置后置入Dotweb.Items,可通过HttpContext.AppItems().Get()获取
8+
* Include\ParseConfigSetXML:支持xml格式文件解析
9+
* Include\ParseConfigSetJSON:支持json格式文件解析
10+
* Include\ParseConfigSetYaml:支持yaml格式文件解析
11+
* 具体配置文件格式可参考example/configset
12+
* 新增示例代码 example/configset
13+
* 2018-01-23 19:00
14+
315
#### Version 1.4.7
416
* BUG Fixed: 修复Middleware特定场景下无效问题
517
* 新增dotweb.IncludeDotwebGroup,用于自动集成dotweb相关路由

0 commit comments

Comments
 (0)