2015-01-13 14:43:45 -05:00
|
|
|
package config
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
import (
|
2015-05-04 07:53:54 -05:00
|
|
|
"io"
|
2015-04-16 00:17:56 -05:00
|
|
|
"log"
|
2015-01-19 01:11:21 -05:00
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
"github.com/mholt/caddy/config/parse"
|
|
|
|
"github.com/mholt/caddy/config/setup"
|
2015-01-19 01:11:21 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-04 07:53:54 -05:00
|
|
|
"github.com/mholt/caddy/server"
|
2015-01-19 01:11:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-29 23:30:03 -05:00
|
|
|
DefaultHost = "0.0.0.0"
|
2015-04-28 23:13:00 -05:00
|
|
|
DefaultPort = "2015"
|
|
|
|
DefaultRoot = "."
|
2015-04-15 15:11:32 -05:00
|
|
|
|
|
|
|
// The default configuration file to load if none is specified
|
|
|
|
DefaultConfigFile = "Caddyfile"
|
2015-01-19 01:11:21 -05:00
|
|
|
)
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
func Load(filename string, input io.Reader) ([]server.Config, error) {
|
|
|
|
var configs []server.Config
|
2015-01-21 15:19:25 -05:00
|
|
|
|
2015-04-16 00:17:56 -05:00
|
|
|
// turn off timestamp for parsing
|
|
|
|
flags := log.Flags()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
serverBlocks, err := parse.ServerBlocks(filename, input)
|
2015-01-21 15:19:25 -05:00
|
|
|
if err != nil {
|
2015-05-04 07:53:54 -05:00
|
|
|
return configs, err
|
2015-01-21 15:19:25 -05:00
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
// Each server block represents a single server/address.
|
|
|
|
// Iterate each server block and make a config for each one,
|
|
|
|
// executing the directives that were parsed.
|
|
|
|
for _, sb := range serverBlocks {
|
|
|
|
config := server.Config{
|
|
|
|
Host: sb.Host,
|
|
|
|
Port: sb.Port,
|
2015-05-05 16:48:40 -05:00
|
|
|
Root: Root,
|
2015-05-04 07:53:54 -05:00
|
|
|
Middleware: make(map[string][]middleware.Middleware),
|
2015-05-04 17:23:16 -05:00
|
|
|
ConfigFile: filename,
|
|
|
|
AppName: AppName,
|
|
|
|
AppVersion: AppVersion,
|
2015-05-04 07:53:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// It is crucial that directives are executed in the proper order.
|
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
// Execute directive if it is in the server block
|
|
|
|
if tokens, ok := sb.Tokens[dir.name]; ok {
|
|
|
|
// Each setup function gets a controller, which is the
|
|
|
|
// server config and the dispenser containing only
|
|
|
|
// this directive's tokens.
|
|
|
|
controller := &setup.Controller{
|
|
|
|
Config: &config,
|
|
|
|
Dispenser: parse.NewDispenserTokens(filename, tokens),
|
|
|
|
}
|
|
|
|
|
|
|
|
midware, err := dir.setup(controller)
|
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
if midware != nil {
|
|
|
|
// TODO: For now, we only support the default path scope /
|
|
|
|
config.Middleware["/"] = append(config.Middleware["/"], midware)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Port == "" {
|
|
|
|
config.Port = Port
|
|
|
|
}
|
|
|
|
|
|
|
|
configs = append(configs, config)
|
2015-04-12 18:44:02 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:17:56 -05:00
|
|
|
// restore logging settings
|
|
|
|
log.SetFlags(flags)
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
return configs, nil
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
// validDirective returns true if d is a valid
|
|
|
|
// directive; false otherwise.
|
|
|
|
func validDirective(d string) bool {
|
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
if dir.name == d {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
// Default makes a default configuration which
|
|
|
|
// is empty except for root, host, and port,
|
|
|
|
// which are essentials for serving the cwd.
|
|
|
|
func Default() server.Config {
|
|
|
|
return server.Config{
|
|
|
|
Root: Root,
|
|
|
|
Host: Host,
|
|
|
|
Port: Port,
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 17:23:16 -05:00
|
|
|
|
|
|
|
// These three defaults are configurable through the command line
|
|
|
|
var (
|
|
|
|
Root = DefaultRoot
|
|
|
|
Host = DefaultHost
|
|
|
|
Port = DefaultPort
|
|
|
|
)
|
|
|
|
|
|
|
|
// The application should set these so that various middlewares
|
|
|
|
// can access the proper information for their own needs.
|
|
|
|
var AppName, AppVersion string
|