-
Notifications
You must be signed in to change notification settings - Fork 14
/
server_config.go
234 lines (200 loc) · 7.56 KB
/
server_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
/*
* Nuts node
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package core
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const defaultConfigFile = "nuts.yaml"
const configFileFlag = "configfile"
const serverAddressFlag = "http.default.address"
const datadirFlag = "datadir"
const httpCORSOriginFlag = "http.default.cors.origin"
const defaultHTTPInterface = ":1323"
const strictModeFlag = "strictmode"
const defaultStrictMode = false
const defaultDatadir = "./data"
const defaultLogLevel = "info"
const loggerLevelFlag = "verbosity"
const defaultLoggerFormat = "text"
const loggerFormatFlag = "loggerformat"
// ServerConfig has global server settings.
type ServerConfig struct {
Verbosity string `koanf:"verbosity"`
LoggerFormat string `koanf:"loggerformat"`
Strictmode bool `koanf:"strictmode"`
Datadir string `koanf:"datadir"`
HTTP GlobalHTTPConfig `koanf:"http"`
configMap *koanf.Koanf
}
// GlobalHTTPConfig is the top-level config struct for HTTP interfaces.
type GlobalHTTPConfig struct {
// HTTPConfig contains the config for the default HTTP interface.
HTTPConfig `koanf:"default"`
// AltBinds contains binds for alternative HTTP interfaces. The key of the map is the first part of the path
// of the URL (e.g. `/internal/some-api` -> `internal`), the value is the HTTP interface it must be bound to.
AltBinds map[string]HTTPConfig `koanf:"alt"`
}
// HTTPConfig contains configuration for an HTTP interface, e.g. address.
// It will probably contain security related properties in the future (TLS configuration, user/pwd requirements).
type HTTPConfig struct {
// Address holds the interface address the HTTP service must be bound to, in the format of `interface:port` (e.g. localhost:5555).
Address string `koanf:"address"`
// CORS holds the configuration for Cross Origin Resource Sharing.
CORS HTTPCORSConfig `koanf:"cors"`
}
// HTTPCORSConfig contains configuration for Cross Origin Resource Sharing.
type HTTPCORSConfig struct {
// Origin specifies the AllowOrigin option. If no origins are given CORS is considered to be disabled.
Origin []string `koanf:"origin"`
}
// Enabled returns whether CORS is enabled according to this configuration.
func (cors HTTPCORSConfig) Enabled() bool {
return len(cors.Origin) > 0
}
// NewServerConfig creates a new config with some defaults
func NewServerConfig() *ServerConfig {
return &ServerConfig{
configMap: koanf.New(defaultDelimiter),
Verbosity: defaultLogLevel,
LoggerFormat: defaultLoggerFormat,
Strictmode: defaultStrictMode,
Datadir: defaultDatadir,
HTTP: GlobalHTTPConfig{
HTTPConfig: HTTPConfig{
Address: defaultHTTPInterface,
},
AltBinds: map[string]HTTPConfig{},
},
}
}
// Load follows the load order of configfile, env vars and then commandline param
func (ngc *ServerConfig) Load(cmd *cobra.Command) (err error) {
ngc.configMap = koanf.New(defaultDelimiter)
configFile := file.Provider(resolveConfigFile(cmd.PersistentFlags()))
// load file
if err = ngc.configMap.Load(configFile, yaml.Parser()); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return
}
}
if err = loadConfigIntoStruct(cmd.PersistentFlags(), ngc, ngc.configMap); err != nil {
return err
}
// Configure logging.
// TODO: see #40
lvl, err := logrus.ParseLevel(ngc.Verbosity)
if err != nil {
return err
}
logrus.SetLevel(lvl)
switch ngc.LoggerFormat {
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
return fmt.Errorf("invalid formatter: '%s'", ngc.LoggerFormat)
}
return nil
}
// resolveConfigFile resolves the path of the config file using the following sources:
// 1. commandline params (using the given flags)
// 2. environment vars,
// 3. default location.
func resolveConfigFile(flags *pflag.FlagSet) string {
k := koanf.New(defaultDelimiter)
// load env flags
e := env.Provider(defaultPrefix, defaultDelimiter, func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, defaultPrefix)), "_", defaultDelimiter, -1)
})
// can't return error
_ = k.Load(e, nil)
// load cmd flags, without a parser, no error can be returned
// this also loads the default flag value of nuts.yaml. So we need a way to know if it's overiden.
_ = k.Load(posflag.Provider(flags, defaultDelimiter, k), nil)
return k.String(configFileFlag)
}
// FlagSet returns the default server flags
func FlagSet() *pflag.FlagSet {
flagSet := pflag.NewFlagSet("server", pflag.ContinueOnError)
flagSet.String(configFileFlag, defaultConfigFile, "Nuts config file")
flagSet.String(loggerLevelFlag, defaultLogLevel, "Log level (trace, debug, info, warn, error)")
flagSet.String(loggerFormatFlag, defaultLoggerFormat, "Log format (text, json)")
flagSet.String(serverAddressFlag, defaultHTTPInterface, "Address and port the server will be listening to")
flagSet.Bool(strictModeFlag, defaultStrictMode, "When set, insecure settings are forbidden.")
flagSet.String(datadirFlag, defaultDatadir, "Directory where the node stores its files.")
flagSet.StringSlice(httpCORSOriginFlag, nil, "When set, enables CORS from the specified origins for the on default HTTP interface.")
return flagSet
}
// PrintConfig return the current config in string form
func (ngc *ServerConfig) PrintConfig() string {
return ngc.configMap.Sprint()
}
// InjectIntoEngine takes the loaded config and sets the engine's config struct
func (ngc *ServerConfig) InjectIntoEngine(e Injectable) error {
return unmarshalRecursive([]string{}, e.Config(), ngc.configMap)
}
func elemType(ty reflect.Type) (reflect.Type, bool) {
isPtr := ty.Kind() == reflect.Ptr
if isPtr {
return ty.Elem(), true
}
return ty, false
}
func unmarshalRecursive(path []string, config interface{}, configMap *koanf.Koanf) error {
if err := configMap.UnmarshalWithConf(strings.Join(path, "."), config, koanf.UnmarshalConf{
FlatPaths: true,
}); err != nil {
return err
}
configType, isPtr := elemType(reflect.TypeOf(config))
// If `config` is a struct or a pointer to a struct we're iterating its fields to find structs
if configType.Kind() == reflect.Struct {
valueOfConfig := reflect.ValueOf(config)
if isPtr {
valueOfConfig = valueOfConfig.Elem()
}
for i := 0; i < configType.NumField(); i++ {
field := configType.Field(i)
fieldType, _ := elemType(field.Type)
tagValue := field.Tag.Get("koanf")
// Unmarshal this field if it's a struct, and it has a `koanf` tag
if fieldType.Kind() == reflect.Struct && tagValue != "" {
fieldAddr := valueOfConfig.Field(i).Addr()
if err := unmarshalRecursive(append(path, tagValue), fieldAddr.Interface(), configMap); err != nil {
return err
}
}
}
}
return nil
}