1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Package config contains utilities and types necessary for
// launching specially-configured server instances.
package config
import (
"log"
"net"
"os"
"github.com/mholt/caddy/middleware"
)
const (
DefaultHost = "0.0.0.0"
DefaultPort = "2015"
DefaultRoot = "."
// The default configuration file to load if none is specified
DefaultConfigFile = "Caddyfile"
)
// Host and Port are configurable via command line flag
var (
Host = DefaultHost
Port = DefaultPort
)
// config represents a server configuration. It
// is populated by parsing a config file (via the
// Load function).
type Config struct {
// The hostname or IP on which to serve
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
// The path to the configuration file from which this was loaded
ConfigFile string
}
// Address returns the host:port of c as a string.
func (c Config) Address() string {
return net.JoinHostPort(c.Host, c.Port)
}
// 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
}
// 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) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
// turn off timestamp for parsing
flags := log.Flags()
log.SetFlags(0)
p, err := newParser(file)
if err != nil {
return nil, err
}
cfgs, err := p.parse()
if err != nil {
return []Config{}, err
}
for i := 0; i < len(cfgs); i++ {
cfgs[i].ConfigFile = filename
}
// restore logging settings
log.SetFlags(flags)
return cfgs, nil
}
// 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{
Root: DefaultRoot,
Host: Host,
Port: Port,
},
}
return cfg
}
|