2015-01-13 14:43:45 -05:00
|
|
|
// Package config contains utilities and types necessary for
|
|
|
|
// launching specially-configured server instances.
|
|
|
|
package config
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
import (
|
2015-04-16 00:17:56 -05:00
|
|
|
"log"
|
2015-04-15 15:11:32 -05:00
|
|
|
"net"
|
2015-01-19 01:11:21 -05:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-04-29 23:30:03 -05:00
|
|
|
// Host and Port are configurable via command line flag
|
|
|
|
var (
|
|
|
|
Host = DefaultHost
|
|
|
|
Port = DefaultPort
|
|
|
|
)
|
2015-04-28 23:13:00 -05:00
|
|
|
|
2015-03-26 10:52:03 -05:00
|
|
|
// config represents a server configuration. It
|
|
|
|
// is populated by parsing a config file (via the
|
|
|
|
// Load function).
|
|
|
|
type Config struct {
|
2015-04-15 15:11:32 -05:00
|
|
|
// The hostname or IP on which to serve
|
2015-03-26 10:52:03 -05:00
|
|
|
Host string
|
|
|
|
|
|
|
|
// The port to listen on
|
|
|
|
Port string
|
|
|
|
|
|
|
|
// The directory from which to serve files
|
|
|
|
Root string
|
|
|
|
|
|
|
|
// HTTPS configuration
|
|
|
|
TLS TLSConfig
|
|
|
|
|
|
|
|
// Middleware stack
|
|
|
|
Middleware map[string][]middleware.Middleware
|
|
|
|
|
|
|
|
// Functions (or methods) to execute at server start; these
|
|
|
|
// are executed before any parts of the server are configured,
|
|
|
|
// and the functions are blocking
|
|
|
|
Startup []func() error
|
|
|
|
|
|
|
|
// Functions (or methods) to execute when the server quits;
|
|
|
|
// these are executed in response to SIGINT and are blocking
|
|
|
|
Shutdown []func() error
|
|
|
|
|
2015-04-12 18:44:02 -05:00
|
|
|
// The path to the configuration file from which this was loaded
|
|
|
|
ConfigFile string
|
2015-03-26 10:52:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the host:port of c as a string.
|
|
|
|
func (c Config) Address() string {
|
2015-04-15 15:11:32 -05:00
|
|
|
return net.JoinHostPort(c.Host, c.Port)
|
2015-03-26 10:52:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TLSConfig describes how TLS should be configured and used,
|
|
|
|
// if at all. A certificate and key are both required.
|
|
|
|
type TLSConfig struct {
|
|
|
|
Enabled bool
|
|
|
|
Certificate string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2015-01-13 14:43:45 -05:00
|
|
|
// Load loads a configuration file, parses it,
|
|
|
|
// and returns a slice of Config structs which
|
|
|
|
// can be used to create and configure server
|
|
|
|
// instances.
|
|
|
|
func Load(filename string) ([]Config, error) {
|
2015-01-19 01:11:21 -05:00
|
|
|
file, err := os.Open(filename)
|
2015-01-13 14:43:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-19 01:11:21 -05:00
|
|
|
defer file.Close()
|
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-01-21 15:19:25 -05:00
|
|
|
p, err := newParser(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-12 18:44:02 -05:00
|
|
|
cfgs, err := p.parse()
|
|
|
|
if err != nil {
|
|
|
|
return []Config{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(cfgs); i++ {
|
|
|
|
cfgs[i].ConfigFile = filename
|
|
|
|
}
|
|
|
|
|
2015-04-16 00:17:56 -05:00
|
|
|
// restore logging settings
|
|
|
|
log.SetFlags(flags)
|
|
|
|
|
2015-04-12 18:44:02 -05:00
|
|
|
return cfgs, nil
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotFound returns whether or not the error is
|
|
|
|
// one which indicates that the configuration file
|
|
|
|
// was not found. (Useful for checking the error
|
|
|
|
// returned from Load).
|
|
|
|
func IsNotFound(err error) bool {
|
|
|
|
return os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default makes a default configuration
|
|
|
|
// that's empty except for root, host, and port,
|
|
|
|
// which are essential for serving the cwd.
|
|
|
|
func Default() []Config {
|
|
|
|
cfg := []Config{
|
|
|
|
Config{
|
2015-04-28 23:13:00 -05:00
|
|
|
Root: DefaultRoot,
|
2015-04-29 23:30:03 -05:00
|
|
|
Host: Host,
|
2015-04-28 23:13:00 -05:00
|
|
|
Port: Port,
|
2015-01-13 14:43:45 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|