2015-01-14 03:43:45 +08:00
|
|
|
package config
|
|
|
|
|
2015-01-22 05:10:52 +08:00
|
|
|
import (
|
2015-05-04 20:53:54 +08:00
|
|
|
"github.com/mholt/caddy/config/parse"
|
|
|
|
"github.com/mholt/caddy/config/setup"
|
2015-03-26 23:52:03 +08:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-01-22 05:10:52 +08:00
|
|
|
)
|
2015-01-19 14:11:21 +08:00
|
|
|
|
2015-01-14 03:43:45 +08:00
|
|
|
func init() {
|
2015-05-04 20:53:54 +08:00
|
|
|
// The parse package must know which directives
|
|
|
|
// are valid, but it must not import the setup
|
2015-05-05 01:49:49 +08:00
|
|
|
// or config package. To solve this problem, we
|
|
|
|
// fill up this map in our init function here.
|
|
|
|
// The parse package does not need to know the
|
|
|
|
// ordering of the directives.
|
2015-05-04 20:53:54 +08:00
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
parse.ValidDirectives[dir.name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 23:52:03 +08:00
|
|
|
|
2015-05-05 01:49:49 +08:00
|
|
|
// Directives are registered in the order they should be
|
|
|
|
// executed. Middleware (directives that inject a handler)
|
|
|
|
// are executed in the order A-B-C-*-C-B-A, assuming
|
|
|
|
// they all call the Next handler in the chain.
|
|
|
|
//
|
|
|
|
// Ordering is VERY important. Every middleware will
|
|
|
|
// feel the effects of all other middleware below
|
|
|
|
// (after) them during a request, but they 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-05-04 20:53:54 +08:00
|
|
|
var directiveOrder = []directive{
|
2015-05-05 01:49:49 +08:00
|
|
|
// Essential directives that initialize vital configuration settings
|
2015-05-04 20:53:54 +08:00
|
|
|
{"root", setup.Root},
|
|
|
|
{"tls", setup.TLS},
|
2015-05-05 13:58:08 +08:00
|
|
|
{"bind", setup.BindHost},
|
2015-05-05 01:49:49 +08:00
|
|
|
|
|
|
|
// Other directives that don't create HTTP handlers
|
2015-05-04 20:53:54 +08:00
|
|
|
{"startup", setup.Startup},
|
|
|
|
{"shutdown", setup.Shutdown},
|
|
|
|
|
2015-05-05 01:49:49 +08:00
|
|
|
// Directives that inject handlers (middleware)
|
2015-05-04 20:53:54 +08:00
|
|
|
{"log", setup.Log},
|
|
|
|
{"gzip", setup.Gzip},
|
|
|
|
{"errors", setup.Errors},
|
|
|
|
{"header", setup.Headers},
|
|
|
|
{"rewrite", setup.Rewrite},
|
|
|
|
{"redir", setup.Redir},
|
|
|
|
{"ext", setup.Ext},
|
2015-10-01 01:37:10 +08:00
|
|
|
{"mime", setup.Mime},
|
2015-05-04 20:53:54 +08:00
|
|
|
{"basicauth", setup.BasicAuth},
|
2015-05-07 04:44:37 +08:00
|
|
|
{"internal", setup.Internal},
|
2015-05-05 01:04:17 +08:00
|
|
|
{"proxy", setup.Proxy},
|
|
|
|
{"fastcgi", setup.FastCGI},
|
2015-05-05 01:49:49 +08:00
|
|
|
{"websocket", setup.WebSocket},
|
|
|
|
{"markdown", setup.Markdown},
|
|
|
|
{"templates", setup.Templates},
|
|
|
|
{"browse", setup.Browse},
|
2015-05-04 20:53:54 +08:00
|
|
|
}
|
2015-03-26 23:52:03 +08:00
|
|
|
|
2015-05-05 01:49:49 +08:00
|
|
|
// directive ties together a directive name with its setup function.
|
2015-05-04 20:53:54 +08:00
|
|
|
type directive struct {
|
|
|
|
name string
|
2015-06-20 21:59:33 +08:00
|
|
|
setup SetupFunc
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
2015-05-04 20:53:54 +08:00
|
|
|
|
2015-10-10 06:35:34 +08:00
|
|
|
// SetupFunc takes a controller and may optionally return a middleware.
|
|
|
|
// If the resulting middleware is not nil, it will be chained into
|
2015-05-05 01:49:49 +08:00
|
|
|
// the HTTP handlers in the order specified in this package.
|
2015-06-20 21:59:33 +08:00
|
|
|
type SetupFunc func(c *setup.Controller) (middleware.Middleware, error)
|