2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-08-03 15:54:12 +08:00
|
|
|
//
|
|
|
|
// 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 (
|
2021-09-27 21:27:24 +08:00
|
|
|
"context"
|
2021-07-13 23:01:31 +08:00
|
|
|
"reflect"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/debug/gdebug"
|
2019-08-03 18:08:10 +08:00
|
|
|
)
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
const (
|
2019-12-04 13:55:08 +08:00
|
|
|
// The default route pattern for global middleware.
|
2021-07-13 23:01:31 +08:00
|
|
|
defaultMiddlewarePattern = "/*"
|
2019-08-06 20:40:04 +08:00
|
|
|
)
|
|
|
|
|
2019-12-04 13:55:08 +08:00
|
|
|
// BindMiddleware registers one or more global middleware to the server.
|
|
|
|
// Global middleware can be used standalone without service handler, which intercepts all dynamic requests
|
2021-10-21 18:22:47 +08:00
|
|
|
// before or after service handler. The parameter `pattern` specifies what route pattern the middleware intercepts,
|
2019-12-04 13:55:08 +08:00
|
|
|
// which is usually a "fuzzy" pattern like "/:name", "/*any" or "/{field}".
|
2019-08-06 20:40:04 +08:00
|
|
|
func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
2021-09-27 21:27:24 +08:00
|
|
|
var (
|
|
|
|
ctx = context.TODO()
|
|
|
|
)
|
2019-08-03 18:08:10 +08:00
|
|
|
for _, handler := range handlers {
|
2021-11-07 21:31:33 +08:00
|
|
|
s.setHandler(ctx, setHandlerInput{
|
|
|
|
Prefix: "",
|
|
|
|
Pattern: pattern,
|
2022-05-06 20:25:21 +08:00
|
|
|
HandlerItem: &HandlerItem{
|
2021-11-07 21:31:33 +08:00
|
|
|
Type: HandlerTypeMiddleware,
|
|
|
|
Name: gdebug.FuncPath(handler),
|
|
|
|
Info: handlerFuncInfo{
|
|
|
|
Func: handler,
|
|
|
|
Type: reflect.TypeOf(handler),
|
|
|
|
},
|
2021-07-13 23:01:31 +08:00
|
|
|
},
|
2019-08-03 18:08:10 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:55:08 +08:00
|
|
|
// BindMiddlewareDefault registers one or more global middleware to the server using default pattern "/*".
|
|
|
|
// Global middleware can be used standalone without service handler, which intercepts all dynamic requests
|
|
|
|
// before or after service handler.
|
2019-09-19 19:44:46 +08:00
|
|
|
func (s *Server) BindMiddlewareDefault(handlers ...HandlerFunc) {
|
2021-09-27 21:27:24 +08:00
|
|
|
var (
|
|
|
|
ctx = context.TODO()
|
|
|
|
)
|
2019-08-03 18:08:10 +08:00
|
|
|
for _, handler := range handlers {
|
2021-11-07 21:31:33 +08:00
|
|
|
s.setHandler(ctx, setHandlerInput{
|
|
|
|
Prefix: "",
|
|
|
|
Pattern: defaultMiddlewarePattern,
|
2022-05-06 20:25:21 +08:00
|
|
|
HandlerItem: &HandlerItem{
|
2021-11-07 21:31:33 +08:00
|
|
|
Type: HandlerTypeMiddleware,
|
|
|
|
Name: gdebug.FuncPath(handler),
|
|
|
|
Info: handlerFuncInfo{
|
|
|
|
Func: handler,
|
|
|
|
Type: reflect.TypeOf(handler),
|
|
|
|
},
|
2021-07-13 23:01:31 +08:00
|
|
|
},
|
2019-08-03 18:08:10 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 19:45:41 +08:00
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// Use is the alias of BindMiddlewareDefault.
|
2020-01-02 19:45:41 +08:00
|
|
|
// See BindMiddlewareDefault.
|
|
|
|
func (s *Server) Use(handlers ...HandlerFunc) {
|
|
|
|
s.BindMiddlewareDefault(handlers...)
|
|
|
|
}
|