Skip to content

Commit bbf8023

Browse files
author
pzrr@qq.com
committed
#### Version 1.4.8
* 调整:ItemContext更名为ItemMap,新增ConcurrenceMap、ReadonlyMap接口 * 调整:Dotweb.AppContext变更为Dotweb.Items * 调整:HttpContext.AppContext变更为HttpContext.AppItems * 调整:HttpContext.AppSetConfig变更为HttpContext.ConfigSet * 调整:config.AppSet变更为config.ConfigSet * 新增: config.ParseConfigSetXML\ParseConfigSetJSON\ParseConfigSetYaml,用于解析常规Key\Value格式的配置文件 * 新增:config.Config.IncludeConfigSet,用于向config.ConfigSet中导入Key\Value格式的配置文件,通过HttpContext.ConfigSet获取相关设置信息 * ParseConfigSetXML:支持xml格式文件解析,返回core.ConcurrenceMap * ParseConfigSetJSON:支持json格式文件解析,返回core.ConcurrenceMap * ParseConfigSetYaml:支持yaml格式文件解析,返回core.ConcurrenceMap * 具体配置文件格式可参考example/configset * 新增示例代码 example/configset * 2018-01-24 22:00
1 parent 5057111 commit bbf8023

File tree

10 files changed

+300
-280
lines changed

10 files changed

+300
-280
lines changed

config/configs.go

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010

1111
type (
1212
Config struct {
13-
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
14-
App *AppNode `xml:"app"`
15-
AppSets []*AppSetNode `xml:"appset>set"`
16-
Offline *OfflineNode `xml:"offline"`
17-
Server *ServerNode `xml:"server"`
18-
Session *SessionNode `xml:"session"`
19-
Routers []*RouterNode `xml:"routers>router"`
20-
Groups []*GroupNode `xml:"groups>group"`
21-
Middlewares []*MiddlewareNode `xml:"middlewares>middleware"`
22-
AppSetConfig *core.ItemContext `json:"-" yaml:"-"`
13+
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
14+
App *AppNode `xml:"app"`
15+
ConfigSetNodes []*ConfigSetNode `xml:"configset>set"`
16+
Offline *OfflineNode `xml:"offline"`
17+
Server *ServerNode `xml:"server"`
18+
Session *SessionNode `xml:"session"`
19+
Routers []*RouterNode `xml:"routers>router"`
20+
Groups []*GroupNode `xml:"groups>group"`
21+
Middlewares []*MiddlewareNode `xml:"middlewares>middleware"`
22+
ConfigSet core.ReadonlyMap `json:"-" yaml:"-"`
2323
}
2424
OfflineNode struct {
2525
Offline bool `xml:"offline,attr"` //是否维护,默认false
@@ -33,11 +33,6 @@ type (
3333
PProfPort int `xml:"pprofport,attr"` //pprof-server 端口,不能与主Server端口相同
3434
EnabledPProf bool `xml:"enabledpprof,attr"` //是否启用pprof server,默认不启用
3535
}
36-
//update for issue #16 配置文件
37-
AppSetNode struct {
38-
Key string `xml:"key,attr"`
39-
Value string `xml:"value,attr"`
40-
}
4136

4237
ServerNode struct {
4338
EnabledListDir bool `xml:"enabledlistdir,attr"` //设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
@@ -93,12 +88,40 @@ const (
9388

9489
func NewConfig() *Config {
9590
return &Config{
96-
App: NewAppNode(),
97-
Offline: NewOfflineNode(),
98-
Server: NewServerNode(),
99-
Session: NewSessionNode(),
100-
AppSetConfig: core.NewItemContext(),
91+
App: NewAppNode(),
92+
Offline: NewOfflineNode(),
93+
Server: NewServerNode(),
94+
Session: NewSessionNode(),
95+
ConfigSet: core.NewReadonlyMap(),
96+
}
97+
}
98+
99+
// IncludeConfigSetXML include ConfigSet file to Dotweb.Items
100+
// same key will cover oldest value
101+
// support xml\json\yaml
102+
func (conf *Config) IncludeConfigSet(configFile string, confType string) error {
103+
var parseItem core.ConcurrenceMap
104+
var err error
105+
if confType == ConfigType_XML {
106+
parseItem, err = ParseConfigSetXML(configFile)
107+
}
108+
if confType == ConfigType_JSON {
109+
parseItem, err = ParseConfigSetJSON(configFile)
110+
}
111+
if confType == ConfigType_Yaml {
112+
parseItem, err = ParseConfigSetYaml(configFile)
113+
}
114+
if err != nil {
115+
return err
116+
}
117+
items := conf.ConfigSet.(*core.ItemMap)
118+
if items == nil {
119+
return errors.New("init config items error")
120+
}
121+
for k, v := range parseItem.GetCurrentMap() {
122+
items.Set(k, v)
101123
}
124+
return nil
102125
}
103126

104127
func NewAppNode() *AppNode {
@@ -187,11 +210,11 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
187210
config.Offline = NewOfflineNode()
188211
}
189212

190-
tmpAppSetMap := core.NewItemContext()
191-
for _, v := range config.AppSets {
192-
tmpAppSetMap.Set(v.Key, v.Value)
213+
tmpConfigSetMap := core.NewConcurrenceMap()
214+
for _, v := range config.ConfigSetNodes {
215+
tmpConfigSetMap.Set(v.Key, v.Value)
193216
}
194-
config.AppSetConfig = tmpAppSetMap
217+
config.ConfigSet = tmpConfigSetMap
195218

196219
//deal config default value
197220
dealConfigDefaultSet(config)

config/configset.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,62 @@ package config
22

33
import (
44
"encoding/xml"
5+
"errors"
56
"github.com/devfeel/dotweb/core"
67
"io/ioutil"
7-
"errors"
88
)
99

10-
type(
11-
//单元配置节点
12-
ConfigSetNode struct{
13-
Key string `xml:"key,attr"`
14-
Value string `xml:"value,attr"`
15-
}
16-
10+
type (
1711
//单元配置组,包含一系列单元配置节点
18-
ConfigSet struct{
19-
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
20-
Name string `xml:"name,attr"`
12+
ConfigSet struct {
13+
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
14+
Name string `xml:"name,attr"`
2115
ConfigSetNodes []*ConfigSetNode `xml:"set"`
2216
}
17+
18+
//update for issue #16 配置文件
19+
ConfigSetNode struct {
20+
Key string `xml:"key,attr"`
21+
Value string `xml:"value,attr"`
22+
}
2323
)
2424

2525
// ParseConfigSetXML include ConfigSet xml file
26-
func ParseConfigSetXML(configFile string) (*core.ItemContext, error){
26+
func ParseConfigSetXML(configFile string) (core.ConcurrenceMap, error) {
2727
return parseConfigSetFile(configFile, ConfigType_XML)
2828
}
2929

3030
// ParseConfigSetJSON include ConfigSet json file
31-
func ParseConfigSetJSON(configFile string) (*core.ItemContext, error){
31+
func ParseConfigSetJSON(configFile string) (core.ConcurrenceMap, error) {
3232
return parseConfigSetFile(configFile, ConfigType_JSON)
3333
}
3434

3535
// ParseConfigSetYaml include ConfigSet yaml file
36-
func ParseConfigSetYaml(configFile string) (*core.ItemContext, error){
36+
func ParseConfigSetYaml(configFile string) (core.ConcurrenceMap, error) {
3737
return parseConfigSetFile(configFile, ConfigType_Yaml)
3838
}
3939

40-
func parseConfigSetFile(configFile string, confType string) (*core.ItemContext, error){
40+
func parseConfigSetFile(configFile string, confType string) (core.ConcurrenceMap, error) {
4141
content, err := ioutil.ReadFile(configFile)
4242
if err != nil {
43-
return nil, errors.New("DotWeb:Config:parseConfigSetFile 配置文件[" + configFile + ", "+confType+"]无法解析 - " + err.Error())
43+
return nil, errors.New("DotWeb:Config:parseConfigSetFile 配置文件[" + configFile + ", " + confType + "]无法解析 - " + err.Error())
4444
}
45-
set :=new(ConfigSet)
45+
set := new(ConfigSet)
4646
if confType == ConfigType_XML {
4747
err = UnmarshalXML(content, set)
4848
}
49-
if confType == ConfigType_JSON{
49+
if confType == ConfigType_JSON {
5050
err = UnmarshalJSON(content, set)
5151
}
52-
if confType == ConfigType_Yaml{
52+
if confType == ConfigType_Yaml {
5353
err = UnmarshalYaml(content, set)
5454
}
55-
if err!=nil{
56-
return nil, errors.New("DotWeb:Config:parseConfigSetFile 配置文件[" + configFile + ", "+confType+"]无法解析 - " + err.Error())
55+
if err != nil {
56+
return nil, errors.New("DotWeb:Config:parseConfigSetFile 配置文件[" + configFile + ", " + confType + "]无法解析 - " + err.Error())
5757
}
58-
item := core.NewItemContext()
59-
for _,s:=range set.ConfigSetNodes{
58+
item := core.NewConcurrenceMap()
59+
for _, s := range set.ConfigSetNodes {
6060
item.Set(s.Key, s.Value)
6161
}
6262
return item, nil
63-
}
63+
}

context.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"github.com/devfeel/dotweb/session"
1414
"os"
1515
"path/filepath"
16-
"time"
1716
"strconv"
17+
"time"
1818
)
1919

2020
const (
@@ -39,11 +39,11 @@ type (
3939
RouterNode() RouterNode
4040
RouterParams() Params
4141
Handler() HttpHandle
42-
AppItems() *core.ItemContext
42+
AppItems() core.ConcurrenceMap
4343
Cache() cache.Cache
44-
Items() *core.ItemContext
45-
AppSetConfig() *core.ItemContext
46-
ViewData() *core.ItemContext
44+
Items() core.ConcurrenceMap
45+
ConfigSet() core.ReadonlyMap
46+
ViewData() core.ConcurrenceMap
4747
SessionID() string
4848
Session() (state *session.SessionState)
4949
Hijack() (*HijackConn, error)
@@ -103,9 +103,9 @@ type (
103103
isEnd bool //表示当前处理流程是否需要终止
104104
httpServer *HttpServer
105105
sessionID string
106-
innerItems *core.ItemContext
107-
items *core.ItemContext
108-
viewData *core.ItemContext
106+
innerItems core.ConcurrenceMap
107+
items core.ConcurrenceMap
108+
viewData core.ConcurrenceMap
109109
features *xFeatureTools
110110
handler HttpHandle
111111
startTime time.Time
@@ -225,11 +225,11 @@ func (ctx *HttpContext) Features() *xFeatureTools {
225225

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

@@ -240,33 +240,33 @@ func (ctx *HttpContext) Cache() cache.Cache {
240240

241241
// getInnerItems get request's inner item context
242242
// lazy init when first use
243-
func (ctx *HttpContext) getInnerItems() *core.ItemContext {
243+
func (ctx *HttpContext) getInnerItems() core.ConcurrenceMap {
244244
if ctx.innerItems == nil {
245-
ctx.innerItems = core.NewItemContext()
245+
ctx.innerItems = core.NewConcurrenceMap()
246246
}
247247
return ctx.innerItems
248248
}
249249

250250
// Items get request's item context
251251
// lazy init when first use
252-
func (ctx *HttpContext) Items() *core.ItemContext {
252+
func (ctx *HttpContext) Items() core.ConcurrenceMap {
253253
if ctx.items == nil {
254-
ctx.items = core.NewItemContext()
254+
ctx.items = core.NewConcurrenceMap()
255255
}
256256
return ctx.items
257257
}
258258

259259
// AppSetConfig get appset from config file
260260
// update for issue #16 配置文件
261-
func (ctx *HttpContext) AppSetConfig() *core.ItemContext {
262-
return ctx.HttpServer().DotApp.Config.AppSetConfig
261+
func (ctx *HttpContext) ConfigSet() core.ReadonlyMap {
262+
return ctx.HttpServer().DotApp.Config.ConfigSet
263263
}
264264

265265
// ViewData get view data context
266266
// lazy init when first use
267-
func (ctx *HttpContext) ViewData() *core.ItemContext {
267+
func (ctx *HttpContext) ViewData() core.ConcurrenceMap {
268268
if ctx.viewData == nil {
269-
ctx.viewData = core.NewItemContext()
269+
ctx.viewData = core.NewConcurrenceMap()
270270
}
271271
return ctx.viewData
272272
}
@@ -330,8 +330,8 @@ func (ctx *HttpContext) QueryInt(key string) int {
330330
if param == "" {
331331
return 0
332332
}
333-
val, err:=strconv.Atoi(param)
334-
if err != nil{
333+
val, err := strconv.Atoi(param)
334+
if err != nil {
335335
return 0
336336
}
337337
return val
@@ -344,14 +344,13 @@ func (ctx *HttpContext) QueryInt64(key string) int64 {
344344
if param == "" {
345345
return 0
346346
}
347-
val, err:=strconv.ParseInt(param, 10, 64)
348-
if err != nil{
347+
val, err := strconv.ParseInt(param, 10, 64)
348+
if err != nil {
349349
return 0
350350
}
351351
return val
352352
}
353353

354-
355354
/*
356355
* 根据指定key获取包括在post、put和get内的值
357356
*/
@@ -551,7 +550,7 @@ func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) error
551550
_, err := ctx.hijackConn.WriteBlob(b)
552551
return err
553552
} else {
554-
_, err := ctx.response.Write(code, b)
553+
_, err := ctx.response.Write(code, b)
555554
return err
556555
}
557556
}
@@ -564,7 +563,7 @@ func (ctx *HttpContext) WriteJson(i interface{}) error {
564563

565564
// WriteJsonC write (httpCode, json string) to response
566565
// auto convert interface{} to json string
567-
func (ctx *HttpContext) WriteJsonC(code int, i interface{}) error{
566+
func (ctx *HttpContext) WriteJsonC(code int, i interface{}) error {
568567
b, err := json.Marshal(i)
569568
if err != nil {
570569
return err

0 commit comments

Comments
 (0)