2015-01-29 22:02:17 -07:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-04-23 14:57:07 -06:00
|
|
|
"github.com/mholt/caddy/middleware/basicauth"
|
2015-03-24 20:12:48 -06:00
|
|
|
"github.com/mholt/caddy/middleware/browse"
|
2015-03-28 16:45:12 -06:00
|
|
|
"github.com/mholt/caddy/middleware/errors"
|
2015-04-18 09:54:35 -06:00
|
|
|
"github.com/mholt/caddy/middleware/extensions"
|
2015-01-29 22:02:17 -07:00
|
|
|
"github.com/mholt/caddy/middleware/fastcgi"
|
|
|
|
"github.com/mholt/caddy/middleware/gzip"
|
|
|
|
"github.com/mholt/caddy/middleware/headers"
|
|
|
|
"github.com/mholt/caddy/middleware/log"
|
2015-03-16 11:45:51 -06:00
|
|
|
"github.com/mholt/caddy/middleware/markdown"
|
2015-01-29 22:02:17 -07:00
|
|
|
"github.com/mholt/caddy/middleware/proxy"
|
|
|
|
"github.com/mholt/caddy/middleware/redirect"
|
|
|
|
"github.com/mholt/caddy/middleware/rewrite"
|
2015-04-18 09:57:51 -06:00
|
|
|
"github.com/mholt/caddy/middleware/templates"
|
2015-03-03 09:49:45 -07:00
|
|
|
"github.com/mholt/caddy/middleware/websockets"
|
2015-01-29 22:02:17 -07:00
|
|
|
)
|
|
|
|
|
2015-03-28 16:45:12 -06:00
|
|
|
// This init function registers middleware. Register
|
|
|
|
// middleware in the order they should be executed
|
|
|
|
// during a request (A, B, C...). Middleware execute
|
|
|
|
// in the order A-B-C-*-C-B-A, assuming they call
|
|
|
|
// the Next handler in the chain.
|
|
|
|
//
|
|
|
|
// Note: Ordering is VERY important. Every middleware
|
|
|
|
// will feel the effects of all other middleware below
|
|
|
|
// (after) them, but must not care what middleware above
|
|
|
|
// them are doing.
|
|
|
|
//
|
|
|
|
// For example, log needs to know the status code and exactly
|
|
|
|
// how many bytes were written to the client, which every
|
|
|
|
// other middleware can affect, so it gets registered first.
|
|
|
|
// The errors middleware does not care if gzip or log modifies
|
|
|
|
// its response, so it gets registered below them. Gzip, on the
|
|
|
|
// other hand, DOES care what errors does to the response since
|
|
|
|
// it must compress every output to the client, even error pages,
|
|
|
|
// so it must be registered before the errors middleware and any
|
|
|
|
// others that would write to the response.
|
2015-01-29 22:02:17 -07:00
|
|
|
func init() {
|
2015-03-28 16:45:12 -06:00
|
|
|
register("log", log.New)
|
2015-01-29 22:02:17 -07:00
|
|
|
register("gzip", gzip.New)
|
2015-03-28 16:45:12 -06:00
|
|
|
register("errors", errors.New)
|
2015-01-29 22:02:17 -07:00
|
|
|
register("header", headers.New)
|
|
|
|
register("rewrite", rewrite.New)
|
2015-03-31 23:57:09 -06:00
|
|
|
register("redir", redirect.New)
|
2015-04-18 09:54:35 -06:00
|
|
|
register("ext", extensions.New)
|
2015-04-23 14:57:07 -06:00
|
|
|
register("basicauth", basicauth.New)
|
2015-01-29 22:02:17 -07:00
|
|
|
register("proxy", proxy.New)
|
|
|
|
register("fastcgi", fastcgi.New)
|
2015-03-03 09:49:45 -07:00
|
|
|
register("websocket", websockets.New)
|
2015-03-16 11:45:51 -06:00
|
|
|
register("markdown", markdown.New)
|
2015-04-18 09:57:51 -06:00
|
|
|
register("templates", templates.New)
|
2015-03-24 20:12:48 -06:00
|
|
|
register("browse", browse.New)
|
2015-01-29 22:02:17 -07:00
|
|
|
}
|
|
|
|
|
2015-03-20 00:03:41 -06:00
|
|
|
// registry stores the registered middleware:
|
|
|
|
// both the order and the directives to which they
|
|
|
|
// are bound.
|
|
|
|
var registry = struct {
|
|
|
|
directiveMap map[string]middleware.Generator
|
|
|
|
ordered []string
|
|
|
|
}{
|
|
|
|
directiveMap: make(map[string]middleware.Generator),
|
|
|
|
}
|
2015-01-29 22:02:17 -07:00
|
|
|
|
|
|
|
// register binds a middleware generator (outer function)
|
|
|
|
// to a directive. Upon each request, middleware will be
|
|
|
|
// executed in the order they are registered.
|
|
|
|
func register(directive string, generator middleware.Generator) {
|
|
|
|
registry.directiveMap[directive] = generator
|
2015-02-07 22:17:15 -07:00
|
|
|
registry.ordered = append(registry.ordered, directive)
|
2015-01-29 22:02:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// middlewareRegistered returns whether or not a directive is registered.
|
|
|
|
func middlewareRegistered(directive string) bool {
|
|
|
|
_, ok := registry.directiveMap[directive]
|
|
|
|
return ok
|
|
|
|
}
|