2019-08-03 15:54:12 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp
|
2019-08-03 18:08:10 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
const (
|
|
|
|
gDEFAULT_MIDDLEWARE_PATTERN = "/*"
|
|
|
|
)
|
|
|
|
|
2019-08-03 18:08:10 +08:00
|
|
|
// 注册中间件,绑定到指定的路由规则上,中间件参数支持多个。
|
2019-08-06 20:40:04 +08:00
|
|
|
func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
2019-08-03 18:08:10 +08:00
|
|
|
for _, handler := range handlers {
|
|
|
|
s.setHandler(pattern, &handlerItem{
|
|
|
|
itemType: gHANDLER_TYPE_MIDDLEWARE,
|
|
|
|
itemName: runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name(),
|
|
|
|
itemFunc: handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 注册中间件,绑定到全局路由规则("/*")上,中间件参数支持多个。
|
2019-08-06 20:40:04 +08:00
|
|
|
func (s *Server) AddMiddleware(handlers ...HandlerFunc) {
|
2019-08-03 18:08:10 +08:00
|
|
|
for _, handler := range handlers {
|
2019-08-06 20:40:04 +08:00
|
|
|
s.setHandler(gDEFAULT_MIDDLEWARE_PATTERN, &handlerItem{
|
2019-08-03 18:08:10 +08:00
|
|
|
itemType: gHANDLER_TYPE_MIDDLEWARE,
|
|
|
|
itemName: runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name(),
|
|
|
|
itemFunc: handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|