-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
42 lines (34 loc) · 1.01 KB
/
config.go
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
package main
import (
"flag"
"fmt"
"os"
)
type config struct {
Address string
CORS bool
CacheTime int
}
var fs = &flag.FlagSet{}
func printErrorUsageAndExit(err string, code int) {
fmt.Fprintf(os.Stderr, "\nWARN: %s\n\n", err)
os.Exit(code)
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nFlags:\n")
fs.PrintDefaults()
}
// Parse command line and load config file. The order of precedence, from more important to less important, is:
// cmd line flag > environment variable > config file
func loadConfig() *config {
cfg := &config{}
fs.StringVar(&cfg.Address, "a", ":8080", "Address to start listening for HTTP connections")
fs.IntVar(&cfg.CacheTime, "c", -1, "Set cache time, in seconds, for cache-control max-age header")
fs.BoolVar(&cfg.CORS, "cors", false, "Enable CORS via the 'Access-Control-Allow-Origin' header")
fs.Usage = printUsage
if err := fs.Parse(os.Args[1:]); err != nil {
printErrorUsageAndExit(err.Error(), 1)
}
return cfg
}