Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供 path 匹配的中间件注入 #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/net/http/blademaster/routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (group *RouterGroup) BasePath() string {
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
absolutePath := group.calculateAbsolutePath(relativePath)
injections := group.injections(relativePath)
handlers = group.combineHandlers(injections, handlers)
pathMiddlewares := group.pathMiddlewares(relativePath)
handlers = group.combineHandlers(injections, pathMiddlewares, handlers)
group.engine.addRoute(httpMethod, absolutePath, handlers...)
if group.baseConfig != nil {
group.engine.SetMethodConfig(absolutePath, group.baseConfig)
Expand Down Expand Up @@ -175,6 +176,14 @@ func (group *RouterGroup) injections(relativePath string) []HandlerFunc {
return nil
}

func (group *RouterGroup) pathMiddlewares(relativePath string) []HandlerFunc {
absPath := group.calculateAbsolutePath(relativePath)
if handlers, has := group.engine.pathMiddlewares[absPath]; has {
return handlers
}
return nil
}

// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
Expand Down
13 changes: 12 additions & 1 deletion pkg/net/http/blademaster/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ type Engine struct {
pcLock sync.RWMutex
methodConfigs map[string]*MethodConfig

injections []injection
injections []injection
pathMiddlewares map[string][]HandlerFunc

// If enabled, the url.RawPath will be used to find parameters.
UseRawPath bool
Expand Down Expand Up @@ -161,6 +162,11 @@ type injection struct {
handlers []HandlerFunc
}

type pathMiddleware struct {
path string
handlers []HandlerFunc
}

// NewServer returns a new blank Engine instance without any middleware attached.
func NewServer(conf *ServerConfig) *Engine {
if conf == nil {
Expand All @@ -181,6 +187,7 @@ func NewServer(conf *ServerConfig) *Engine {
methodConfigs: make(map[string]*MethodConfig),
HandleMethodNotAllowed: true,
injections: make([]injection, 0),
pathMiddlewares: make(map[string][]HandlerFunc, 512),
}
if err := engine.SetConfig(conf); err != nil {
panic(err)
Expand Down Expand Up @@ -482,6 +489,10 @@ func (engine *Engine) Inject(pattern string, handlers ...HandlerFunc) {
})
}

func (engine *Engine) PathMatch(path string, handlers ...HandlerFunc) {
engine.pathMiddlewares[path] = append(engine.pathMiddlewares[path], handlers...)
}

// ServeHTTP conforms to the http.Handler interface.
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := engine.pool.Get().(*Context)
Expand Down