Skip to content

Commit cc2f468

Browse files
committed
优化代码结构
1 parent fd28ca0 commit cc2f468

6 files changed

Lines changed: 119 additions & 105 deletions

File tree

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ go get -u github.com/devfeel/dotweb
88
```
99

1010
## 快速开始:
11-
1211
```go
1312
func StartServer() error {
1413
//初始化DotServer
@@ -25,6 +24,8 @@ func StartServer() error {
2524
}
2625

2726
```
27+
#### 详细示例 - https://github.com/devfeel/dotweb-example
28+
2829
## 特性
2930
* 支持静态路由、参数路由
3031
* 路由支持文件/目录服务,支持设置是否允许目录浏览
@@ -159,10 +160,10 @@ ctx.Session().Set(key, value)
159160
```
160161

161162
## Server Config
162-
目前支持三个选项:Debug、Session、Gzip
163-
* SetEnabledDebug 设置是否开启debug模式,会输出server端的debug日志,默认不开启
164-
* SetEnabledSession 设置是否开启Session支持,目前支持runtime、redis两种模式,默认不开启
165-
* SetEnabledGzip 设置是否开启Gzip支持,默认不开启
163+
目前支持5个选项:
164+
* EnabledDebug 设置是否开启debug模式,会输出server端的debug日志,默认不开启
165+
* EnabledSession 设置是否开启Session支持,目前支持runtime、redis两种模式,默认不开启
166+
* EnabledGzip 设置是否开启Gzip支持,默认不开启
166167
* EnabledListDir 设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
167168
* EnabledAutoHEAD 设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
168169

context.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"fmt"
1212
"github.com/devfeel/dotweb/cache"
13+
"github.com/devfeel/dotweb/core"
1314
"github.com/devfeel/dotweb/routers"
1415
"github.com/devfeel/dotweb/session"
1516
)
@@ -29,8 +30,8 @@ type HttpContext struct {
2930
isEnd bool //表示当前处理流程是否需要终止
3031
HttpServer *HttpServer
3132
SessionID string
32-
items *ItemContext
33-
viewData *ItemContext
33+
items *core.ItemContext
34+
viewData *core.ItemContext
3435
}
3536

3637
//reset response attr
@@ -59,11 +60,11 @@ func (ctx *HttpContext) release() {
5960

6061
//get application's global appcontext
6162
//issue #3
62-
func (ctx *HttpContext) AppContext() *ItemContext {
63+
func (ctx *HttpContext) AppContext() *core.ItemContext {
6364
if ctx.HttpServer != nil {
6465
return ctx.HttpServer.DotApp.AppContext
6566
} else {
66-
return NewItemContext()
67+
return core.NewItemContext()
6768
}
6869
}
6970

@@ -74,18 +75,18 @@ func (ctx *HttpContext) Cache() cache.Cache {
7475

7576
//get request's tem context
7677
//lazy init when first use
77-
func (ctx *HttpContext) Items() *ItemContext {
78+
func (ctx *HttpContext) Items() *core.ItemContext {
7879
if ctx.items == nil {
79-
ctx.items = NewItemContext()
80+
ctx.items = core.NewItemContext()
8081
}
8182
return ctx.items
8283
}
8384

8485
//get view data context
8586
//lazy init when first use
86-
func (ctx *HttpContext) ViewData() *ItemContext {
87+
func (ctx *HttpContext) ViewData() *core.ItemContext {
8788
if ctx.viewData == nil {
88-
ctx.viewData = NewItemContext()
89+
ctx.viewData = core.NewItemContext()
8990
}
9091
return ctx.viewData
9192
}
@@ -363,7 +364,7 @@ func (ctx *HttpContext) ReadCookieObj(name string) (*http.Cookie, error) {
363364

364365
// write view content to response
365366
func (ctx *HttpContext) View(name string) error {
366-
err := ctx.HttpServer.Renderer().Render(ctx.Response.Writer(), name, ctx.ViewData().contextMap, ctx)
367+
err := ctx.HttpServer.Renderer().Render(ctx.Response.Writer(), name, ctx.ViewData().GetCurrentMap(), ctx)
367368
return err
368369
}
369370

core/context.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
//自带锁,并发安全的Map
9+
type ItemContext struct {
10+
contextMap map[string]interface{}
11+
*sync.RWMutex
12+
}
13+
14+
func NewItemContext() *ItemContext {
15+
return &ItemContext{
16+
contextMap: make(map[string]interface{}),
17+
RWMutex: new(sync.RWMutex),
18+
}
19+
}
20+
21+
/*
22+
* 以key、value置入AppContext
23+
*/
24+
func (ctx *ItemContext) Set(key string, value interface{}) error {
25+
ctx.Lock()
26+
ctx.contextMap[key] = value
27+
ctx.Unlock()
28+
return nil
29+
}
30+
31+
/*
32+
* 读取指定key在AppContext中的内容
33+
*/
34+
func (ctx *ItemContext) Get(key string) (value interface{}, exists bool) {
35+
ctx.RLock()
36+
value, exists = ctx.contextMap[key]
37+
ctx.RUnlock()
38+
return value, exists
39+
}
40+
41+
//remove item by gived key
42+
//if not exists key, do nothing...
43+
func (ctx *ItemContext) Remove(key string) {
44+
ctx.Lock()
45+
delete(ctx.contextMap, key)
46+
ctx.Unlock()
47+
}
48+
49+
//get item by gived key, and remove it
50+
//only can be read once, it will be locked
51+
func (ctx *ItemContext) Once(key string) (value interface{}, exists bool) {
52+
ctx.Lock()
53+
defer ctx.Unlock()
54+
value, exists = ctx.contextMap[key]
55+
if exists {
56+
delete(ctx.contextMap, key)
57+
}
58+
return value, exists
59+
}
60+
61+
/*
62+
* 读取指定key在AppContext中的内容,以string格式输出
63+
*/
64+
func (ctx *ItemContext) GetString(key string) string {
65+
value, exists := ctx.Get(key)
66+
if !exists {
67+
return ""
68+
}
69+
return fmt.Sprint(value)
70+
}
71+
72+
/*
73+
* 读取指定key在AppContext中的内容,以int格式输出
74+
*/
75+
func (ctx *ItemContext) GetInt(key string) int {
76+
value, exists := ctx.Get(key)
77+
if !exists {
78+
return 0
79+
}
80+
return value.(int)
81+
}
82+
83+
//check exists key
84+
func (ctx *ItemContext) Exists(key string) bool {
85+
_, exists := ctx.contextMap[key]
86+
return exists
87+
}
88+
89+
//get current map, returns map[string]interface{}
90+
func (ctx *ItemContext) GetCurrentMap() map[string]interface{} {
91+
return ctx.contextMap
92+
}
93+
94+
//get context length
95+
func (ctx *ItemContext) Len() int {
96+
return len(ctx.contextMap)
97+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package file
1+
package core
22

33
import (
44
"net/http"

dotweb.go

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/devfeel/dotweb/cache"
66
"github.com/devfeel/dotweb/config"
7+
"github.com/devfeel/dotweb/core"
78
"github.com/devfeel/dotweb/framework/json"
89
"github.com/devfeel/dotweb/framework/log"
910
"github.com/devfeel/dotweb/servers"
@@ -15,7 +16,6 @@ import (
1516
"runtime/pprof"
1617
"strconv"
1718
"strings"
18-
"sync"
1919
)
2020

2121
type (
@@ -27,12 +27,7 @@ type (
2727
Modules []*HttpModule
2828
logpath string
2929
ExceptionHandler ExceptionHandle
30-
AppContext *ItemContext
31-
}
32-
33-
ItemContext struct {
34-
contextMap map[string]interface{}
35-
contextMutex *sync.RWMutex
30+
AppContext *core.ItemContext
3631
}
3732

3833
ExceptionHandle func(*HttpContext, interface{})
@@ -50,92 +45,12 @@ func New() *DotWeb {
5045
HttpServer: NewHttpServer(),
5146
OfflineServer: servers.NewOfflineServer(),
5247
Modules: make([]*HttpModule, 0, 10),
53-
AppContext: NewItemContext(),
48+
AppContext: core.NewItemContext(),
5449
}
5550
app.HttpServer.setDotApp(app)
5651
return app
5752
}
5853

59-
func NewItemContext() *ItemContext {
60-
return &ItemContext{
61-
contextMap: make(map[string]interface{}),
62-
contextMutex: new(sync.RWMutex),
63-
}
64-
}
65-
66-
/*
67-
* 以key、value置入AppContext
68-
*/
69-
func (ctx *ItemContext) Set(key string, value interface{}) error {
70-
ctx.contextMutex.Lock()
71-
ctx.contextMap[key] = value
72-
ctx.contextMutex.Unlock()
73-
return nil
74-
}
75-
76-
/*
77-
* 读取指定key在AppContext中的内容
78-
*/
79-
func (ctx *ItemContext) Get(key string) (value interface{}, exists bool) {
80-
ctx.contextMutex.RLock()
81-
value, exists = ctx.contextMap[key]
82-
ctx.contextMutex.RUnlock()
83-
return value, exists
84-
}
85-
86-
//remove item by gived key
87-
//if not exists key, do nothing...
88-
func (ctx *ItemContext) Remove(key string) {
89-
ctx.contextMutex.Lock()
90-
delete(ctx.contextMap, key)
91-
ctx.contextMutex.Unlock()
92-
}
93-
94-
//get item by gived key, and remove it
95-
//only can be read once, it will be locked
96-
func (ctx *ItemContext) Once(key string) (value interface{}, exists bool) {
97-
ctx.contextMutex.Lock()
98-
defer ctx.contextMutex.Unlock()
99-
value, exists = ctx.contextMap[key]
100-
if exists {
101-
delete(ctx.contextMap, key)
102-
}
103-
return value, exists
104-
}
105-
106-
/*
107-
* 读取指定key在AppContext中的内容,以string格式输出
108-
*/
109-
func (ctx *ItemContext) GetString(key string) string {
110-
value, exists := ctx.Get(key)
111-
if !exists {
112-
return ""
113-
}
114-
return fmt.Sprint(value)
115-
}
116-
117-
/*
118-
* 读取指定key在AppContext中的内容,以int格式输出
119-
*/
120-
func (ctx *ItemContext) GetInt(key string) int {
121-
value, exists := ctx.Get(key)
122-
if !exists {
123-
return 0
124-
}
125-
return value.(int)
126-
}
127-
128-
//check exists key
129-
func (ctx *ItemContext) Exists(key string) bool {
130-
_, exists := ctx.contextMap[key]
131-
return exists
132-
}
133-
134-
//get context length
135-
func (ctx *ItemContext) Len() int {
136-
return len(ctx.contextMap)
137-
}
138-
13954
/*
14055
* return cache interface
14156
*/

router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotweb
22

33
import (
4-
"github.com/devfeel/dotweb/framework/file"
4+
"github.com/devfeel/dotweb/core"
55
"github.com/devfeel/dotweb/framework/log"
66
"github.com/devfeel/dotweb/routers"
77
"golang.org/x/net/websocket"
@@ -191,7 +191,7 @@ func (r *router) ServerFile(path string, fileroot string) {
191191
var root http.FileSystem
192192
root = http.Dir(fileroot)
193193
if !r.server.ServerConfig.EnabledListDir {
194-
root = &file.HideReaddirFS{root}
194+
root = &core.HideReaddirFS{root}
195195
}
196196
fileServer := http.FileServer(root)
197197
r.router.Handle(RouteMethod_GET, path, r.server.wrapFileHandle(fileServer))

0 commit comments

Comments
 (0)