/
config.go
285 lines (255 loc) · 6.8 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)
var defaultFileContent = []byte(`{
// should mmseqs und webserver output be printed
"verbose": true,
"server" : {
"address" : "127.0.0.1:8081",
// prefix for all API endpoints
"pathprefix" : "/api/",
/* enable HTTP Basic Auth (optional)
"auth": {
"username" : "",
"password" : ""
},
*/
// should CORS headers be set to allow requests from anywhere
"cors" : true
},
// camep specific settigns, special character ~ is resolved relative to the binary location
"cameo" : {
// path to mmseqs databases, has to be shared between server/workers
"path" : "~",
// server names
"servers" : [],
// response URL
"response" : ""
},
"mail" : {
"mailer" : {
// three types available:
// null: uses NullTransport class, which ignores all sent emails
"type" : "null"
/* smtp: Uses SMTP to send emails example for gmail
"type" : "smtp",
"transport" : {
// full host URL with port
"host" : "smtp.gmail.com:587",
// smtp auth
"auth" : {
// auth type, can be plain (RFC 4616) or login (e.g. for exchange server)
"type" : "plain",
// empty for gmail
"identity" : "",
// gmail user
"username" : "user@gmail.com",
"password" : "password",
"host" : "smtp.gmail.com",
}
}
*/
/* mailgun: Uses the mailgun API to send emails
"type" : "mailgun",
"transport" : {
// mailgun domain
"domain" : "mail.mmseqs.com",
// mailgun API private key
"secretkey" : "key-XXXX",
// mailgun API public key
"publickey" : "pubkey-XXXX"
}
*/
},
// Email FROM field
"sender" : "mail@example.org",
/* Bracket notation is also possible:
"sender" : "Webserver <mail@example.org>",
*/,
// list of emails to BCC
"bcc" : [],
"confirm" : ""
}
}
`)
type ConfigCameo struct {
JobPath string `json:"path"`
Servers []string `json:"servers"`
ResponseURL string `json:"response"`
}
type ConfigMail struct {
Mailer *ConfigMailtransport `json:"mailer"`
Sender string `json:"sender"`
Confirm string `json:"confirm"`
Target string `json:"target"`
BCC []string `json:"bcc"`
}
type ConfigAuth struct {
Username string `json:"username"`
Password string `json:"password" validate:"required"`
}
type ConfigServer struct {
Address string `json:"address" validate:"required"`
PathPrefix string `json:"pathprefix"`
CORS bool `json:"cors"`
Auth *ConfigAuth `json:"auth"`
}
type ConfigRoot struct {
Server ConfigServer `json:"server" validate:"required"`
Cameo ConfigCameo `json:"cameo" validate:"required"`
Mail ConfigMail `json:"mail"`
Verbose bool `json:"verbose"`
}
func ReadConfigFromFile(name string) (ConfigRoot, error) {
file, err := os.Open(name)
if err != nil {
return ConfigRoot{}, err
}
defer file.Close()
absPath, err := filepath.Abs(name)
if err != nil {
return ConfigRoot{}, err
}
relativeTo := filepath.Dir(absPath)
return ReadConfig(file, relativeTo)
}
func DefaultConfig() (ConfigRoot, error) {
r := bytes.NewReader(defaultFileContent)
ex, err := os.Executable()
if err != nil {
panic(err)
}
relativeTo := filepath.Dir(ex)
return ReadConfig(r, relativeTo)
}
func ReadConfig(r io.Reader, relativeTo string) (ConfigRoot, error) {
var config ConfigRoot
if err := DecodeJsonAndValidate(r, &config); err != nil {
return config, fmt.Errorf("fatal error for config file: %s", err)
}
paths := []*string{&config.Cameo.JobPath}
for _, path := range paths {
if strings.HasPrefix(*path, "~") {
*path = strings.TrimLeft(*path, "~")
*path = filepath.Join(relativeTo, *path)
}
}
return config, nil
}
func (c *ConfigRoot) CheckPaths() error {
paths := []string{c.Cameo.JobPath}
for _, path := range paths {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0755)
}
}
return nil
}
func (c *ConfigRoot) ReadParameters(args []string) error {
var key string
inParameter := false
for _, arg := range args {
if strings.HasPrefix(arg, "-") {
if inParameter {
return errors.New("invalid parameter string")
}
key = strings.TrimLeft(arg, "-")
inParameter = true
} else {
if !inParameter {
return errors.New("invalid parameter string")
}
err := c.setParameter(key, arg)
if err != nil {
return err
}
inParameter = false
}
}
if inParameter {
return errors.New("invalid parameter string")
}
return nil
}
func (c *ConfigRoot) setParameter(key string, value string) error {
path := strings.Split(key, ".")
return setNodeValue(c, path, value)
}
// DFS in Config Tree to set the new value
func setNodeValue(node interface{}, path []string, value string) error {
if len(path) == 0 {
if v, ok := node.(reflect.Value); ok {
if !v.IsValid() || !v.CanSet() {
return errors.New("leaf node is not valid")
}
switch v.Kind() {
case reflect.Struct:
return errors.New("leaf node is a struct")
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
v.SetInt(i)
break
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
v.SetUint(i)
break
case reflect.Bool:
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
v.SetBool(b)
break
case reflect.String:
v.SetString(value)
break
default:
return errors.New("leaf node type not implemented")
}
return nil
} else {
return errors.New("leaf node is not a value")
}
}
v, ok := node.(reflect.Value)
if !ok {
v = reflect.ValueOf(node).Elem()
}
if v.Kind() == reflect.Ptr {
if v.IsNil() {
t := v.Type().Elem()
n := reflect.New(t)
v.Set(n)
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return errors.New("node is not a struct")
}
for i := 0; i < v.NumField(); i++ {
tag := v.Type().Field(i).Tag.Get("json")
if tag == "" || tag == "-" {
continue
}
if tag == path[0] {
f := v.Field(i)
return setNodeValue(f, path[1:], value)
}
}
return errors.New("path not found in config")
}