Ai
1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
subrouter.go 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
package apid
import (
"net/http"
"github.com/gorilla/mux"
"github.com/sensu/sensu-go/backend/apid/middlewares"
)
// WrappedRouter is equivelant to mux.Router with the addition that any time it
// handles a request the given middleware stack is applied first.
type WrappedRouter struct {
*mux.Router
middleware *middlewares.Stack
}
// Match matches registered routes against the request.
func (r *WrappedRouter) Match(req *http.Request, match *mux.RouteMatch) bool {
if !r.Router.Match(req, match) {
return false
}
// wrap the handler that matched the request in our middleware stack
match.Handler = middlewares.Apply(match.Handler, r.middleware)
return true
}
// NewSubrouter returns new router given route and set of middleware. Useful
// when you would like a subset of routes that are wrapped in a specific set of
// middleware. Eg. you would some routes to require authetication while others
// you would not.
func NewSubrouter(r *mux.Route, ms ...middlewares.HTTPMiddleware) *mux.Router {
subRouter := r.Subrouter().UseEncodedPath()
wrapper := WrappedRouter{
Router: subRouter,
middleware: middlewares.NewStack(ms),
}
// Override existing matcher for the subrouter w/ variant wrapped in
// middleware stack.
r.MatcherFunc(wrapper.Match)
return subRouter
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

搜索帮助