Skip to content

Commit 990bdfc

Browse files
author
pzrr@qq.com
committed
Version 0.3.9.5
* 新增Feature模块,目前支持CROS特性,可通过HttpServer全局设置、RouterNode路由级别设置 * 新增CROS设置,设置是否允许跨域访问,并可设置AllowedOrigins、、AllowedMethods、AllowedHeaders、AllowCredentials、AllowedP3P属性 * PProfServer调整为属性设置,通过SetPProfConfig设置,移除StartPProf方法 * HttpContext新增RouterNode属性,可通过该属性获取路由信息 * 迁移:原DotWeb中SetSessionConfig函数迁移至HttpServer * 废弃:DotWeb.SetSessionConfig、DotWeb.StartPProfServer * 配置App节点新增EnabledPProf\PProfPort设置,用于设置PProfServer * 完善配置文件与代码,完善代码,完善example * 2017-04-09 20:00
1 parent 01dfaab commit 990bdfc

4 files changed

Lines changed: 15 additions & 43 deletions

File tree

‎example/main.go‎

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

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

‎router.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,34 @@ type (
7474
}
7575

7676
RouterNode struct {
77-
node interface{}
77+
Node interface{}
78+
Method string
7879
}
7980
)
8081

81-
func NewRouterNode(n interface{}) *RouterNode {
82+
func NewRouterNode(n interface{}, method string) *RouterNode {
8283
return &RouterNode{
83-
node: n,
84+
Node: n,
85+
Method: method,
8486
}
8587
}
8688

8789
func (n *RouterNode) SetEnabledCROS() *feature.CROSConfig {
8890
var f *feature.Feature
8991
var isok bool
9092
lock_feature.RLock()
91-
f, isok = featuresMap[n.node]
93+
f, isok = featuresMap[n.Node]
9294
lock_feature.RUnlock()
9395
if !isok {
9496
f = feature.NewFeature()
9597
lock_feature.Lock()
96-
featuresMap[n.node] = f
98+
featuresMap[n.Node] = f
9799
lock_feature.Unlock()
98100
}
99101
f.CROSConfig.EnabledCROS = true
100102
f.CROSConfig.UseDefault()
103+
//special set method use current router's http method
104+
f.CROSConfig.SetMethod(n.Method)
101105
return f.CROSConfig
102106
}
103107

‎routers/router.go‎

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ func init() {
9494

9595
type ValueNode struct {
9696
Params
97-
Node *Node
97+
Method string
98+
Node *Node
9899
}
99100

100101
// Handle is a function that can be registered to a route to handle HTTP
@@ -197,41 +198,6 @@ func (r *Router) ANY(path string, handle Handle) {
197198
r.Handle("DELETE", path, handle)
198199
}
199200

200-
// GET is a shortcut for router.Handle("GET", path, handle)
201-
func (r *Router) GET(path string, handle Handle) {
202-
r.Handle("GET", path, handle)
203-
}
204-
205-
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
206-
func (r *Router) HEAD(path string, handle Handle) {
207-
r.Handle("HEAD", path, handle)
208-
}
209-
210-
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
211-
func (r *Router) OPTIONS(path string, handle Handle) {
212-
r.Handle("OPTIONS", path, handle)
213-
}
214-
215-
// POST is a shortcut for router.Handle("POST", path, handle)
216-
func (r *Router) POST(path string, handle Handle) {
217-
r.Handle("POST", path, handle)
218-
}
219-
220-
// PUT is a shortcut for router.Handle("PUT", path, handle)
221-
func (r *Router) PUT(path string, handle Handle) {
222-
r.Handle("PUT", path, handle)
223-
}
224-
225-
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
226-
func (r *Router) PATCH(path string, handle Handle) {
227-
r.Handle("PATCH", path, handle)
228-
}
229-
230-
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
231-
func (r *Router) DELETE(path string, handle Handle) {
232-
r.Handle("DELETE", path, handle)
233-
}
234-
235201
// Handle registers a new request handle with the given path and method.
236202
//
237203
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
@@ -342,10 +308,12 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
342308
vn := valueNodePool.Get().(*ValueNode)
343309
vn.Params = ps
344310
vn.Node = node
311+
vn.Method = req.Method
345312
//user handle
346313
handle(w, req, vn)
347314
vn.Params = nil
348315
vn.Node = nil
316+
vn.Method = ""
349317
valueNodePool.Put(vn)
350318
return
351319
} else if req.Method != "CONNECT" && path != "/" {

‎server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (server *HttpServer) wrapRouterHandle(handle HttpHandle, isHijack bool) rou
233233
res := server.pool.response.Get().(*Response)
234234
res.Reset(w)
235235
httpCtx := server.pool.context.Get().(*HttpContext)
236-
httpCtx.Reset(res, r, server, NewRouterNode(vnode.Node), vnode.Params)
236+
httpCtx.Reset(res, r, server, NewRouterNode(vnode.Node, vnode.Method), vnode.Params)
237237

238238
//gzip
239239
if server.ServerConfig.EnabledGzip {

0 commit comments

Comments
 (0)