add user middleware chain function (#1913)

* add user middleware chain function

* fix staticcheck SA4006

* chang code Implementation style

Co-authored-by: kemq1 <kemq1@spdb.com.cn>
This commit is contained in:
magickeha
2022-06-18 18:45:47 +08:00
committed by GitHub
parent 9b6e4c440c
commit 6976ba7e13
3 changed files with 90 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ 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"
@@ -242,6 +243,17 @@ 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) {