|
| 1 | +// Package config contains utilities and types necessary for |
| 2 | +// launching specially-configured server instances. |
| 3 | +package config |
| 4 | + |
| 5 | +import "os" |
| 6 | + |
| 7 | +// Load loads a configuration file, parses it, |
| 8 | +// and returns a slice of Config structs which |
| 9 | +// can be used to create and configure server |
| 10 | +// instances. |
| 11 | +func Load(filename string) ([]Config, error) { |
| 12 | + p := parser{} |
| 13 | + err := p.lexer.Load(filename) |
| 14 | + if err != nil { |
| 15 | + return nil, err |
| 16 | + } |
| 17 | + defer p.lexer.Close() |
| 18 | + return p.Parse() |
| 19 | +} |
| 20 | + |
| 21 | +// IsNotFound returns whether or not the error is |
| 22 | +// one which indicates that the configuration file |
| 23 | +// was not found. (Useful for checking the error |
| 24 | +// returned from Load). |
| 25 | +func IsNotFound(err error) bool { |
| 26 | + return os.IsNotExist(err) |
| 27 | +} |
| 28 | + |
| 29 | +// Default makes a default configuration |
| 30 | +// that's empty except for root, host, and port, |
| 31 | +// which are essential for serving the cwd. |
| 32 | +func Default() []Config { |
| 33 | + cfg := []Config{ |
| 34 | + Config{ |
| 35 | + Root: defaultRoot, |
| 36 | + Host: defaultHost, |
| 37 | + Port: defaultPort, |
| 38 | + }, |
| 39 | + } |
| 40 | + return cfg |
| 41 | +} |
| 42 | + |
| 43 | +// config represents a server configuration. It |
| 44 | +// is populated by parsing a config file. (Use |
| 45 | +// the Load function.) |
| 46 | +type Config struct { |
| 47 | + Host string |
| 48 | + Port string |
| 49 | + Root string |
| 50 | + Gzip bool |
| 51 | + RequestLog Log |
| 52 | + ErrorLog Log |
| 53 | + Rewrites []Rewrite |
| 54 | + Redirects []Redirect |
| 55 | + Extensions []string |
| 56 | + ErrorPages map[int]string // Map of HTTP status code to filename |
| 57 | + Headers []Headers |
| 58 | + TLS TLSConfig |
| 59 | +} |
| 60 | + |
| 61 | +// Address returns the host:port of c as a string. |
| 62 | +func (c Config) Address() string { |
| 63 | + return c.Host + ":" + c.Port |
| 64 | +} |
| 65 | + |
| 66 | +// Rewrite describes an internal location rewrite. |
| 67 | +type Rewrite struct { |
| 68 | + From string |
| 69 | + To string |
| 70 | +} |
| 71 | + |
| 72 | +// Redirect describes an HTTP redirect. |
| 73 | +type Redirect struct { |
| 74 | + From string |
| 75 | + To string |
| 76 | + Code int |
| 77 | +} |
| 78 | + |
| 79 | +// Log represents the settings for a log. |
| 80 | +type Log struct { |
| 81 | + Enabled bool |
| 82 | + OutputFile string |
| 83 | + Format string |
| 84 | +} |
| 85 | + |
| 86 | +// Headers groups a slice of HTTP headers by a URL pattern. |
| 87 | +type Headers struct { |
| 88 | + Url string |
| 89 | + Headers []Header |
| 90 | +} |
| 91 | + |
| 92 | +// Header represents a single HTTP header, simply a name and value. |
| 93 | +type Header struct { |
| 94 | + Name string |
| 95 | + Value string |
| 96 | +} |
| 97 | + |
| 98 | +// TLSConfig describes how TLS should be configured and used, |
| 99 | +// if at all. At least a certificate and key are required. |
| 100 | +type TLSConfig struct { |
| 101 | + Enabled bool |
| 102 | + Certificate string |
| 103 | + Key string |
| 104 | +} |
| 105 | + |
| 106 | +// httpRedirs is a list of supported HTTP redirect codes. |
| 107 | +var httpRedirs = map[string]int{ |
| 108 | + "300": 300, |
| 109 | + "301": 301, |
| 110 | + "302": 302, |
| 111 | + "303": 303, |
| 112 | + "304": 304, |
| 113 | + "305": 305, |
| 114 | + "306": 306, |
| 115 | + "307": 307, |
| 116 | + "308": 308, |
| 117 | +} |
| 118 | + |
| 119 | +// httpErrors is a list of supported HTTP error codes. |
| 120 | +var httpErrors = map[string]int{ |
| 121 | + "400": 400, |
| 122 | + "401": 401, |
| 123 | + "402": 402, |
| 124 | + "403": 403, |
| 125 | + "404": 404, |
| 126 | + "405": 405, |
| 127 | + "406": 406, |
| 128 | + "407": 407, |
| 129 | + "408": 408, |
| 130 | + "409": 409, |
| 131 | + "410": 410, |
| 132 | + "411": 411, |
| 133 | + "412": 412, |
| 134 | + "413": 413, |
| 135 | + "414": 414, |
| 136 | + "415": 415, |
| 137 | + "416": 416, |
| 138 | + "417": 417, |
| 139 | + "418": 418, |
| 140 | + "419": 419, |
| 141 | + "420": 420, |
| 142 | + "422": 422, |
| 143 | + "423": 423, |
| 144 | + "424": 424, |
| 145 | + "426": 426, |
| 146 | + "428": 428, |
| 147 | + "429": 429, |
| 148 | + "431": 431, |
| 149 | + "440": 440, |
| 150 | + "444": 444, |
| 151 | + "449": 449, |
| 152 | + "450": 450, |
| 153 | + "451": 451, |
| 154 | + "494": 494, |
| 155 | + "495": 495, |
| 156 | + "496": 496, |
| 157 | + "497": 497, |
| 158 | + "498": 498, |
| 159 | + "499": 499, |
| 160 | + "500": 500, |
| 161 | + "501": 501, |
| 162 | + "502": 502, |
| 163 | + "503": 503, |
| 164 | + "504": 504, |
| 165 | + "505": 505, |
| 166 | + "506": 506, |
| 167 | + "507": 507, |
| 168 | + "508": 508, |
| 169 | + "509": 509, |
| 170 | + "510": 510, |
| 171 | + "511": 511, |
| 172 | + "520": 520, |
| 173 | + "521": 521, |
| 174 | + "522": 522, |
| 175 | + "523": 523, |
| 176 | + "524": 524, |
| 177 | + "598": 598, |
| 178 | + "599": 599, |
| 179 | +} |
| 180 | + |
| 181 | +const ( |
| 182 | + defaultHost = "localhost" |
| 183 | + defaultPort = "8080" |
| 184 | + defaultRoot = "." |
| 185 | +) |
| 186 | + |
| 187 | +const ( |
| 188 | + DefaultRequestsLog = "requests.log" |
| 189 | + DefaultErrorsLog = "errors.log" |
| 190 | +) |
0 commit comments