-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
83 lines (69 loc) · 2.05 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
package config
import (
"context"
"fmt"
"io/ioutil"
"time"
"github.com/anz-bank/sysl-go/common"
"github.com/anz-bank/sysl-go/validator"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
type DefaultConfig struct {
Library LibraryConfig `yaml:"library"`
GenCode GenCodeConfig `yaml:"genCode"`
// development config can be used to set some config options only appropriate for dev/test environments.
Development *DevelopmentConfig `yaml:"development"`
}
// LoadConfig reads and validates a configuration loaded from file.
// file: the path to the yaml-encoded config file
// defaultConfig: a pointer to the default config struct to populate
// customConfig: a pointer to the custom config struct to populate.
func LoadConfig(file string, defaultConfig *DefaultConfig, customConfig interface{}) error {
b, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("read config file error: %s", err)
}
if err = yaml.Unmarshal(b, customConfig); err != nil {
return fmt.Errorf("unmarshal config file error: %s", err)
}
c := make(map[string]interface{})
if err = yaml.Unmarshal(b, &c); err != nil {
return fmt.Errorf("unmarshal config file error: %s", err)
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: nil,
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
Result: defaultConfig,
})
if err != nil {
return err
}
err = decoder.Decode(c)
if err != nil {
return err
}
err = validator.Validate(defaultConfig)
if err != nil {
return err
}
err = validator.Validate(customConfig)
if err != nil {
return err
}
return err
}
func NewCallback(
config *GenCodeConfig,
downstreamTimeOut time.Duration,
mapError func(ctx context.Context, err error) *common.HTTPError,
) common.Callback {
// construct the rest configuration (aka. gen callback)
return common.Callback{
UpstreamTimeout: config.Upstream.ContextTimeout,
DownstreamTimeout: downstreamTimeOut,
RouterBasePath: config.Upstream.HTTP.BasePath,
UpstreamConfig: &config.Upstream,
MapErrorFunc: mapError,
}
}