-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
68 lines (52 loc) · 1.28 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
package main
import (
"errors"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
yaml "gopkg.in/yaml.v2"
)
const DefaultSamplingRate = 1
const DefaultQueueConnectionNum = 5
type Config struct {
SourceQueue string `yaml:"source_queue"`
QueueConnectionNum int `yaml:"connection_num"`
WSEndpoint string `yaml:"ws_endpoint"`
WSPort int `yaml:"ws_port"`
AWSRegion string `yaml:"region,omitempty"`
AWSEndpoint string `yaml:"endpoint,omitempty"`
AWSConfig aws.Config
SamplingRate int `yaml:"sampling_rate"`
}
func parseConfig(path string) (Config, error) {
var conf Config
data, err := ioutil.ReadFile(path)
if err != nil {
return conf, err
}
err = yaml.Unmarshal(data, &conf)
return conf, err
}
func NewConfig(path string) (Config, error) {
conf, err := parseConfig(path)
if err != nil {
return conf, err
}
if conf.WSEndpoint == "" {
return conf, errors.New("ws endpoint is not set")
}
if conf.QueueConnectionNum == 0 {
conf.QueueConnectionNum = DefaultQueueConnectionNum
}
ac := aws.NewConfig()
if conf.AWSRegion != "" {
ac = ac.WithRegion(conf.AWSRegion)
}
if conf.AWSEndpoint != "" {
ac = ac.WithEndpoint(conf.AWSEndpoint)
}
if conf.SamplingRate == 0 {
conf.SamplingRate = DefaultSamplingRate
}
conf.AWSConfig = *ac
return conf, nil
}