Skip to content

Commit 242fc7b

Browse files
author
pzrr@qq.com
committed
Version 0.3.11
* 新增Group支持,可通过HttpServer.Group()或者NewGroup()创建,目前Group支持HEAD\GET\POST\PUT\OPTIONS\PATCH\DELETE路由方法 * Middleware支持三个级别Use:DotWeb.Use\Group.Use\RouterNode.Use * 优化Router实现,移除router目录,整合xRouter和router.Router * 废弃:移除RouterNode级别的Feature支持,原RouterNode.Features.SetEnabledCROS 可通过自实现Middleware实现 * 更新 example/middleware 目录 ```go func InitRoute(server *dotweb.HttpServer) { server.Router().GET("/", Index) server.Router().GET("/use", Index).Use(NewAccessFmtLog("Router-use")) g := server.Group("/group").Use(NewAccessFmtLog("group")) g.GET("/", Index) g.GET("/use", Index).Use(NewAccessFmtLog("group-use")) } ``` * 2017-05-6 00:30
1 parent 74beaf1 commit 242fc7b

19 files changed

Lines changed: 470 additions & 526 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ dotweb.json.conf
9797
* 支持静态路由、参数路由
9898
* 路由支持文件/目录服务,支持设置是否允许目录浏览
9999
* 中间件支持(Middleware\HttpModule双重支持)
100-
* Feature支持,可绑定HttpServer全局启用,绑定RouterNode路由级别启用
100+
* Feature支持,可绑定HttpServer全局启用
101101
* 支持STRING/JSON/JSONP/HTML格式输出
102102
* 统一的HTTP错误处理
103103
* 统一的日志处理

context.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"github.com/devfeel/dotweb/cache"
1111
"github.com/devfeel/dotweb/core"
12-
"github.com/devfeel/dotweb/routers"
1312
"github.com/devfeel/dotweb/session"
1413
"time"
1514
)
@@ -21,8 +20,8 @@ const (
2120

2221
type HttpContext struct {
2322
Request *Request
24-
RouterNode *RouterNode
25-
RouterParams routers.Params
23+
RouterNode RouterNode
24+
RouterParams Params
2625
Response *Response
2726
WebSocket *WebSocket
2827
HijackConn *HijackConn
@@ -39,7 +38,7 @@ type HttpContext struct {
3938
}
4039

4140
//reset response attr
42-
func (ctx *HttpContext) Reset(res *Response, r *Request, server *HttpServer, node *RouterNode, params routers.Params, handler HttpHandle) {
41+
func (ctx *HttpContext) Reset(res *Response, r *Request, server *HttpServer, node RouterNode, params Params, handler HttpHandle) {
4342
ctx.Request = r
4443
ctx.Response = res
4544
ctx.RouterNode = node

dotweb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ type (
2828
Modules []*HttpModule
2929
Middlewares []Middleware
3030
ExceptionHandler ExceptionHandle
31+
NotFoundHandler NotFoundHandle
3132
AppContext *core.ItemContext
3233
}
3334

3435
ExceptionHandle func(*HttpContext, interface{})
35-
NotFoundHandle HttpHandle
36+
NotFoundHandle http.Handler
3637

3738
// Handle is a function that can be registered to a route to handle HTTP
3839
// requests. Like http.HandlerFunc, but has a special parameter *HttpContext contain all request and response data.

example/config/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4-
"datapipe/datapipe.web/framework/json"
54
"fmt"
65
"github.com/devfeel/dotweb"
76
"github.com/devfeel/dotweb/config"
7+
"github.com/devfeel/dotweb/framework/json"
88
)
99

1010
func main() {

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func Redirect(ctx *dotweb.HttpContext) {
8686
}
8787

8888
func InitRoute(server *dotweb.HttpServer) {
89-
server.Router().GET("/", Index).SetEnabledCROS().SetOrigin("*")
89+
server.Router().GET("/", Index)
9090
server.Router().POST("/keypost", KeyPost)
9191
server.Router().POST("/jsonpost", JsonPost)
9292
server.Router().GET("/error", DefaultError)

example/middleware/main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ func main() {
2525
//设置路由
2626
InitRoute(app.HttpServer)
2727

28-
InitModule(app)
28+
//InitModule(app)
2929

3030
app.UseRequestLog()
3131
app.Use(
32-
NewAccessFmtLog(1),
33-
NewSimpleAuth("admin"),
32+
NewAccessFmtLog("app"),
33+
//NewSimpleAuth("admin"),
3434
)
3535

3636
//启动 监控服务
@@ -62,9 +62,12 @@ func Redirect(ctx *dotweb.HttpContext) {
6262
}
6363

6464
func InitRoute(server *dotweb.HttpServer) {
65-
server.Router().GET("/", Index).SetEnabledCROS().SetOrigin("*")
66-
server.Router().GET("/error", DefaultError)
67-
server.Router().GET("/redirect", Redirect)
65+
server.Router().GET("/", Index)
66+
server.Router().GET("/use", Index).Use(NewAccessFmtLog("Router-use"))
67+
68+
g := server.Group("/group").Use(NewAccessFmtLog("group"))
69+
g.GET("/", Index)
70+
g.GET("/use", Index).Use(NewAccessFmtLog("group-use"))
6871
}
6972

7073
func InitModule(dotserver *dotweb.DotWeb) {
@@ -91,7 +94,7 @@ func InitModule(dotserver *dotweb.DotWeb) {
9194

9295
type AccessFmtLog struct {
9396
dotweb.BaseMiddlware
94-
Index int
97+
Index string
9598
}
9699

97100
func (m *AccessFmtLog) Handle(ctx *dotweb.HttpContext) error {
@@ -101,7 +104,7 @@ func (m *AccessFmtLog) Handle(ctx *dotweb.HttpContext) error {
101104
return err
102105
}
103106

104-
func NewAccessFmtLog(index int) *AccessFmtLog {
107+
func NewAccessFmtLog(index string) *AccessFmtLog {
105108
return &AccessFmtLog{Index: index}
106109
}
107110

routers/path.go renamed to framework/file/path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found
44
// in the LICENSE file.
55

6-
package routers
6+
package file
77

88
// CleanPath is the URL version of path.Clean, it returns a canonical URL path
99
// for p, eliminating . and .. elements.

framework/file/path_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package file

group.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dotweb
2+
3+
type (
4+
Group interface {
5+
Use(m ...Middleware) Group
6+
Group(prefix string, m ...Middleware) Group
7+
DELETE(path string, h HttpHandle) RouterNode
8+
GET(path string, h HttpHandle) RouterNode
9+
HEAD(path string, h HttpHandle) RouterNode
10+
OPTIONS(path string, h HttpHandle) RouterNode
11+
PATCH(path string, h HttpHandle) RouterNode
12+
POST(path string, h HttpHandle) RouterNode
13+
PUT(path string, h HttpHandle) RouterNode
14+
}
15+
xGroup struct {
16+
prefix string
17+
middlewares []Middleware
18+
server *HttpServer
19+
}
20+
)
21+
22+
func NewGroup(prefix string, server *HttpServer) Group {
23+
g := &xGroup{prefix: prefix, server: server}
24+
return g
25+
}
26+
27+
// Use implements `Router#Use()` for sub-routes within the Group.
28+
func (g *xGroup) Use(m ...Middleware) Group {
29+
g.middlewares = append(g.middlewares, m...)
30+
return g
31+
}
32+
33+
// DELETE implements `Router#DELETE()` for sub-routes within the Group.
34+
func (g *xGroup) DELETE(path string, h HttpHandle) RouterNode {
35+
return g.add(RouteMethod_DELETE, path, h)
36+
}
37+
38+
// GET implements `Router#GET()` for sub-routes within the Group.
39+
func (g *xGroup) GET(path string, h HttpHandle) RouterNode {
40+
return g.add(RouteMethod_GET, path, h)
41+
}
42+
43+
// HEAD implements `Router#HEAD()` for sub-routes within the Group.
44+
func (g *xGroup) HEAD(path string, h HttpHandle) RouterNode {
45+
return g.add(RouteMethod_HEAD, path, h)
46+
}
47+
48+
// OPTIONS implements `Router#OPTIONS()` for sub-routes within the Group.
49+
func (g *xGroup) OPTIONS(path string, h HttpHandle) RouterNode {
50+
return g.add(RouteMethod_OPTIONS, path, h)
51+
}
52+
53+
// PATCH implements `Router#PATCH()` for sub-routes within the Group.
54+
func (g *xGroup) PATCH(path string, h HttpHandle) RouterNode {
55+
return g.add(RouteMethod_PATCH, path, h)
56+
}
57+
58+
// POST implements `Router#POST()` for sub-routes within the Group.
59+
func (g *xGroup) POST(path string, h HttpHandle) RouterNode {
60+
return g.add(RouteMethod_POST, path, h)
61+
}
62+
63+
// PUT implements `Router#PUT()` for sub-routes within the Group.
64+
func (g *xGroup) PUT(path string, h HttpHandle) RouterNode {
65+
return g.add(RouteMethod_PUT, path, h)
66+
}
67+
68+
// Group creates a new sub-group with prefix and optional sub-group-level middleware.
69+
func (g *xGroup) Group(prefix string, m ...Middleware) Group {
70+
return NewGroup(g.prefix+prefix, g.server).Use(g.middlewares...).Use(m...)
71+
}
72+
73+
func (g *xGroup) add(method, path string, handler HttpHandle) RouterNode {
74+
return g.server.Router().RegisterRoute(method, g.prefix+path, handler).Use(g.middlewares...)
75+
}

middleware.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,27 @@ func (bm *BaseMiddlware) Next(ctx *HttpContext) error {
2828

2929
type xMiddleware struct {
3030
BaseMiddlware
31+
IsEnd bool
3132
}
3233

3334
func (x *xMiddleware) Handle(ctx *HttpContext) error {
34-
if x.next == nil {
35+
if x.IsEnd {
3536
ctx.handle(ctx)
3637
return nil
3738
} else {
38-
return x.Next(ctx)
39+
if x.next == nil {
40+
if len(ctx.RouterNode.Middlewares()) <= 0 {
41+
ctx.handle(ctx)
42+
} else {
43+
ctx.RouterNode.Use(&xMiddleware{IsEnd: true})
44+
ctx.RouterNode.Middlewares()[0].Handle(ctx)
45+
}
46+
return nil
47+
} else {
48+
return x.Next(ctx)
49+
}
3950
}
51+
4052
}
4153

4254
//请求日志中间件

0 commit comments

Comments
 (0)