chore: refactor to simplify disabling builtin middlewares (#2031)
* chore: refactor to simplify disabling builtin middlewares * chore: rename methods
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/justinas/alice"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/handler"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
@@ -96,6 +95,13 @@ func (s *Server) Use(middleware Middleware) {
|
||||
s.ngin.use(middleware)
|
||||
}
|
||||
|
||||
// DisableDefaultMiddlewares returns a RunOption that disables the builtin middlewares.
|
||||
func DisableDefaultMiddlewares() RunOption {
|
||||
return func(svr *Server) {
|
||||
svr.ngin.disableDefaultMiddlewares = true
|
||||
}
|
||||
}
|
||||
|
||||
// ToMiddleware converts the given handler to a Middleware.
|
||||
func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware {
|
||||
return func(handle http.HandlerFunc) http.HandlerFunc {
|
||||
@@ -243,17 +249,6 @@ func WithTLSConfig(cfg *tls.Config) RunOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithChain returns a RunOption that with given chain config.
|
||||
func WithChain(middlewares ...func(http.Handler) http.Handler) RunOption {
|
||||
return func(svr *Server) {
|
||||
chain := alice.New()
|
||||
for _, middleware := range middlewares {
|
||||
chain = chain.Append(middleware)
|
||||
}
|
||||
svr.ngin.setChainConfig(&chain)
|
||||
}
|
||||
}
|
||||
|
||||
// WithUnauthorizedCallback returns a RunOption that with given unauthorized callback set.
|
||||
func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption {
|
||||
return func(svr *Server) {
|
||||
|
||||
Reference in New Issue
Block a user