This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
211 lines (187 loc) · 4.94 KB
/
file.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package config
import (
"crypto/tls"
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/c2h5oh/datasize"
"github.com/pkg/errors"
)
// File represents a TOML encoded configuration file
type File struct {
Mode Mode `toml:"mode"`
Log struct {
Debug string `toml:"debug"`
Error string `toml:"error"`
} `toml:"log"`
TransportHTTP struct {
Host string `toml:"host"`
KeepAliveDuration Duration `toml:"keep-alive-duration"`
TLS struct {
Enabled bool `toml:"enabled"`
MinVersion TLSVersion `toml:"min-version"`
CertificateFile string `toml:"certificate-file"`
KeyFile string `toml:"key-file"`
CurvePreferences []TLSCurveID `toml:"curve-preferences"`
CipherSuites []TLSCipherSuite `toml:"cipher-suites"`
} `toml:"tls"`
} `toml:"transport-http"`
App struct {
MaxReqSize string `toml:"max-req-size"`
MaxFileSize string `toml:"max-file-size"`
MaxMultipartMembuf string `toml:"max-multipart-membuf"`
} `toml:"app"`
}
func (fl *File) mode(conf *Config) error {
if err := fl.Mode.Validate(); err != nil {
return err
}
conf.Mode = fl.Mode
return nil
}
func (fl *File) debugLog(conf *Config) error {
var writer io.Writer
if strings.HasPrefix(fl.Log.Debug, "stdout") {
writer = os.Stdout
} else if strings.HasPrefix(fl.Log.Debug, "file:") &&
len(fl.Log.Debug) > 5 {
// Debug log to file
var err error
writer, err = os.OpenFile(
fl.Log.Debug[5:],
os.O_WRONLY|os.O_APPEND|os.O_CREATE,
0660,
)
if err != nil {
return errors.Wrap(err, "debug log file")
}
} else {
return fmt.Errorf("invalid: '%s'", fl.Log.Debug)
}
conf.DebugLog = log.New(
writer,
"DBG: ",
log.Ldate|log.Ltime,
)
return nil
}
func (fl *File) errorLog(conf *Config) error {
var writer io.Writer
if strings.HasPrefix(fl.Log.Error, "stderr") {
writer = os.Stdout
} else if strings.HasPrefix(fl.Log.Error, "file:") &&
len(fl.Log.Error) > 5 {
// Error log to file
var err error
writer, err = os.OpenFile(
fl.Log.Error[5:],
os.O_WRONLY|os.O_APPEND|os.O_CREATE,
0660,
)
if err != nil {
return errors.Wrap(err, "error log file")
}
} else {
return fmt.Errorf("invalid: '%s'", fl.Log.Error)
}
conf.ErrorLog = log.New(
writer,
"ERR: ",
log.Ldate|log.Ltime,
)
return nil
}
func (fl *File) transportHTTP(conf *Config) error {
// Host
if len(fl.TransportHTTP.Host) < 1 {
return nil
}
conf.TransportHTTP = &TransportHTTP{
Host: fl.TransportHTTP.Host,
KeepAliveDuration: time.Duration(fl.TransportHTTP.KeepAliveDuration),
}
// TLS
if fl.TransportHTTP.TLS.Enabled {
conf.TransportHTTP.TLS = &TransportHTTPTLS{
Config: &tls.Config{},
CertificateFilePath: fl.TransportHTTP.TLS.CertificateFile,
PrivateKeyFilePath: fl.TransportHTTP.TLS.KeyFile,
}
// Min version
conf.TransportHTTP.TLS.Config.MinVersion = uint16(
fl.TransportHTTP.TLS.MinVersion,
)
// Curve preferences
curveIDs := make(
[]tls.CurveID,
len(fl.TransportHTTP.TLS.CurvePreferences),
)
for i, curveID := range fl.TransportHTTP.TLS.CurvePreferences {
curveIDs[i] = tls.CurveID(curveID)
}
conf.TransportHTTP.TLS.Config.CurvePreferences = curveIDs
conf.TransportHTTP.TLS.Config.PreferServerCipherSuites = true
// Cipher suites
cipherSuites := make([]uint16, len(fl.TransportHTTP.TLS.CipherSuites))
for i, cipherSuite := range fl.TransportHTTP.TLS.CipherSuites {
cipherSuites[i] = uint16(cipherSuite)
}
conf.TransportHTTP.TLS.Config.CipherSuites = cipherSuites
}
return nil
}
func parseFileSize(str string) (uint64, error) {
if str == "" {
return 0, nil
}
// Parse
var v datasize.ByteSize
if err := v.UnmarshalText([]byte(str)); err != nil {
return 0, err
}
return uint64(v), nil
}
func (fl *File) app(conf *Config) error {
var err error
conf.App.MaxReqSize, err = parseFileSize(fl.App.MaxReqSize)
if err != nil {
return errors.Wrap(err, "parsing app.max-req-size")
}
conf.App.MaxFileSize, err = parseFileSize(fl.App.MaxFileSize)
if err != nil {
return errors.Wrap(err, "parsing app.max-file-size")
}
conf.App.MaxMultipartMembuf, err = parseFileSize(fl.App.MaxMultipartMembuf)
if err != nil {
return errors.Wrap(err, "parsing app.max-multipart-membuf")
}
return nil
}
// FromFile reads the configuration from a file
func FromFile(path string) (*Config, error) {
var file File
conf := &Config{}
// Read TOML config file
if _, err := toml.DecodeFile(path, &file); err != nil {
return nil, errors.Wrap(err, "TOML decode")
}
for setterName, setter := range map[string]func(*Config) error{
"mode": file.mode,
"log.debug": file.debugLog,
"log.error": file.errorLog,
"transport-http": file.transportHTTP,
"app": file.app,
} {
if err := setter(conf); err != nil {
return nil, errors.Wrap(err, setterName)
}
}
if err := conf.Init(); err != nil {
return nil, err
}
return conf, nil
}