2015-01-14 03:43:45 +08:00
|
|
|
package config
|
|
|
|
|
2015-01-19 14:11:21 +08:00
|
|
|
import (
|
2015-05-21 14:06:53 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-05-04 20:53:54 +08:00
|
|
|
"io"
|
2015-04-16 13:17:56 +08:00
|
|
|
"log"
|
2015-05-21 14:06:53 +08:00
|
|
|
"net"
|
2015-01-19 14:11:21 +08:00
|
|
|
|
2015-05-21 14:06:53 +08:00
|
|
|
"github.com/mholt/caddy/app"
|
2015-05-04 20:53:54 +08:00
|
|
|
"github.com/mholt/caddy/config/parse"
|
|
|
|
"github.com/mholt/caddy/config/setup"
|
2015-01-19 14:11:21 +08:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-04 20:53:54 +08:00
|
|
|
"github.com/mholt/caddy/server"
|
2015-01-19 14:11:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-30 12:30:03 +08:00
|
|
|
DefaultHost = "0.0.0.0"
|
2015-04-29 12:13:00 +08:00
|
|
|
DefaultPort = "2015"
|
|
|
|
DefaultRoot = "."
|
2015-04-16 04:11:32 +08:00
|
|
|
|
|
|
|
// The default configuration file to load if none is specified
|
|
|
|
DefaultConfigFile = "Caddyfile"
|
2015-01-19 14:11:21 +08:00
|
|
|
)
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-05-04 20:53:54 +08:00
|
|
|
func Load(filename string, input io.Reader) ([]server.Config, error) {
|
|
|
|
var configs []server.Config
|
2015-01-22 04:19:25 +08:00
|
|
|
|
2015-04-16 13:17:56 +08:00
|
|
|
// turn off timestamp for parsing
|
|
|
|
flags := log.Flags()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
2015-05-04 20:53:54 +08:00
|
|
|
serverBlocks, err := parse.ServerBlocks(filename, input)
|
2015-01-22 04:19:25 +08:00
|
|
|
if err != nil {
|
2015-05-04 20:53:54 +08:00
|
|
|
return configs, err
|
2015-01-22 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
2015-05-04 20:53:54 +08: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-06 05:48:40 +08:00
|
|
|
Root: Root,
|
2015-05-04 20:53:54 +08:00
|
|
|
Middleware: make(map[string][]middleware.Middleware),
|
2015-05-05 06:23:16 +08:00
|
|
|
ConfigFile: filename,
|
2015-05-21 14:06:53 +08:00
|
|
|
AppName: app.Name,
|
|
|
|
AppVersion: app.Version,
|
2015-05-04 20:53:54 +08: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-13 07:44:02 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:17:56 +08:00
|
|
|
// restore logging settings
|
|
|
|
log.SetFlags(flags)
|
|
|
|
|
2015-05-04 20:53:54 +08:00
|
|
|
return configs, nil
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:06:53 +08:00
|
|
|
// ArrangeBindings groups configurations by their bind address. For example,
|
|
|
|
// a server that should listen on localhost and another on 127.0.0.1 will
|
|
|
|
// be grouped into the same address: 127.0.0.1. It will return an error
|
|
|
|
// if the address lookup fails or if a TLS listener is configured on the
|
|
|
|
// same address as a plaintext HTTP listener. The return value is a map of
|
|
|
|
// bind address to list of configs that would become VirtualHosts on that
|
|
|
|
// server.
|
|
|
|
func ArrangeBindings(allConfigs []server.Config) (map[*net.TCPAddr][]server.Config, error) {
|
|
|
|
addresses := make(map[*net.TCPAddr][]server.Config)
|
|
|
|
|
|
|
|
// Group configs by bind address
|
|
|
|
for _, conf := range allConfigs {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", conf.Address())
|
|
|
|
if err != nil {
|
|
|
|
return addresses, errors.New("Could not serve " + conf.Address() + " - " + err.Error())
|
|
|
|
}
|
|
|
|
addresses[addr] = append(addresses[addr], conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow HTTP and HTTPS to be served on the same address
|
|
|
|
for _, configs := range addresses {
|
|
|
|
isTLS := configs[0].TLS.Enabled
|
|
|
|
for _, config := range configs {
|
|
|
|
if config.TLS.Enabled != isTLS {
|
|
|
|
thisConfigProto, otherConfigProto := "HTTP", "HTTP"
|
|
|
|
if config.TLS.Enabled {
|
|
|
|
thisConfigProto = "HTTPS"
|
|
|
|
}
|
|
|
|
if configs[0].TLS.Enabled {
|
|
|
|
otherConfigProto = "HTTPS"
|
|
|
|
}
|
|
|
|
return addresses, fmt.Errorf("Configuration error: Cannot multiplex %s (%s) and %s (%s) on same address",
|
|
|
|
configs[0].Address(), otherConfigProto, config.Address(), thisConfigProto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
|
|
|
}
|
|
|
|
|
2015-05-04 20:53:54 +08: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-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-04 20:53:54 +08: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-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 06:23:16 +08:00
|
|
|
|
|
|
|
// These three defaults are configurable through the command line
|
|
|
|
var (
|
|
|
|
Root = DefaultRoot
|
|
|
|
Host = DefaultHost
|
|
|
|
Port = DefaultPort
|
|
|
|
)
|