-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
131 lines (114 loc) · 3.29 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
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
129
130
131
package config
import (
"encoding/json"
"flag"
"os"
"github.com/kelseyhightower/envconfig"
"github.com/zekroTJA/seiteki/pkg/seiteki"
)
// configFile defines the default config file location
const configFile = "/etc/seiteki/config.json"
var flagConfigFile = flag.String("config", configFile, "config file location")
// Get returns a merged seiteki.Config instance
// with configuration values collected from
// flags, environment variables and default
// configuration file.
func Get() (*seiteki.Config, error) {
c := getFromFlags()
cEnv, err := getFromEnv()
if err != nil {
return nil, err
}
merge(cEnv, c)
cFile, err := getFromFile()
if err != nil {
return nil, err
}
merge(cFile, c)
return c, nil
}
// getFromFlags crteates a seiteki.Config instance
// with data passed by flags.
// This is used as base config instance because defaults
// are set by non-set flag values.
func getFromFlags() *seiteki.Config {
addr := flag.String("addr", "localhost:80", "expose address and port")
cacheDuration := flag.String("cd", "720h", "cache duration (for time format see https://golang.org/pkg/time/#ParseDuration)")
certFile := flag.String("cert", "", "ssl cert file location")
compress := flag.Bool("compress", false, "whether or not to gzip compress static files")
indexFile := flag.String("index", "index.html", "default index file location")
routeMode := flag.String("mode", "regex", "route mode used for static file determination [regex, stat, static]")
keyFile := flag.String("key", "", "ssl key file location")
staticDir := flag.String("dir", "web", "static file location")
flag.Parse()
return &seiteki.Config{
Addr: *addr,
CacheDuration: *cacheDuration,
CertFile: *certFile,
Compress: *compress,
IndexFile: *indexFile,
RouteMode: seiteki.RouteMode(*routeMode),
KeyFile: *keyFile,
StaticDir: *staticDir,
}
}
// getFromEnv creates a seiteki.Config instance
// from environment variable configuration
// prefixed with 'STK_'.
func getFromEnv() (*seiteki.Config, error) {
c := new(seiteki.Config)
return c, envconfig.Process("STK", c)
}
// getFromFile tries to read the content of the
// default config file and creates a seiteki.Config
// instance if the file exists.
// If IO errors occur, this will be returned as
// error.
func getFromFile() (*seiteki.Config, error) {
_, err := os.Stat(*flagConfigFile)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
f, err := os.Open(*flagConfigFile)
if err != nil {
return nil, err
}
defer f.Close()
c := new(seiteki.Config)
dec := json.NewDecoder(f)
return c, dec.Decode(c)
}
// merge sets or replaces data in target fields
// if the fields are not empty in source.
func merge(source, target *seiteki.Config) {
if source == nil {
return
}
if source.Addr != "" {
target.Addr = source.Addr
}
if source.CacheDuration != "" {
target.CacheDuration = source.CacheDuration
}
if source.CertFile != "" {
target.CertFile = source.CertFile
}
if source.Compress {
target.Compress = source.Compress
}
if source.IndexFile != "" {
target.IndexFile = source.IndexFile
}
if source.KeyFile != "" {
target.KeyFile = source.KeyFile
}
if source.StaticDir != "" {
target.StaticDir = source.StaticDir
}
if source.RouteMode != "" {
target.RouteMode = source.RouteMode
}
}