core: replace GetMiddleware (#885)

* core: replace GetMiddleware

See the discussion in #881. GetMiddleware would add a `nil` middleware
to the callstack thereby breaking functionality.

This PR drops it in favor of RegisterHandler which is a completely
standalone registry for middleware that want to let it self know to
other middleware.

Currenly *autopath* uses this to call *kubernetes*'s AutoPath method
for dynamic autopathing.

* Drop GetMiddleware

* Register metrics

* drop the panic
This commit is contained in:
Miek Gieben
2017-08-10 21:31:36 +01:00
committed by GitHub
parent 10681c6bf0
commit ea77f2a2ca
7 changed files with 53 additions and 28 deletions

View File

@@ -38,6 +38,11 @@ type Config struct {
// Compiled middleware stack.
middlewareChain middleware.Handler
// Middleware interested in announcing that they exist, so other middleware can call methods
// on them should register themselves here. The name should be the name as return by the
// Handler's Name method.
Registry map[string]middleware.Handler
}
// GetConfig gets the Config that corresponds to c.
@@ -53,18 +58,3 @@ func GetConfig(c *caddy.Controller) *Config {
ctx.saveConfig(c.Key, &Config{})
return GetConfig(c)
}
// GetMiddleware returns the middleware handler that has been added to the config under name.
// This is useful to inspect if a certain middleware is active in this server.
// Note that this is order dependent and the order is defined in directives.go, i.e. if your middleware
// comes before the middleware you are checking; it will not be there (yet).
func GetMiddleware(c *caddy.Controller, name string) middleware.Handler {
conf := GetConfig(c)
for _, h := range conf.Middleware {
x := h(nil)
if name == x.Name() {
return x
}
}
return nil
}