-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
102 lines (87 loc) · 1.91 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
package bridge
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"gopkg.in/yaml.v2"
)
type Config struct {
Server struct {
PathPrefix string
Port int
}
Bridges []Bridge
}
// configYAML contains all information for bridging.
type configYAML struct {
Version string
Server struct {
PathPrefix string `yaml:"path_prefix"`
Port int `yaml:"port"`
}
Bridges []struct {
Name string
Input struct {
Source string
Options map[string]string
}
Output struct {
Target string
Options map[string]string
}
Converter struct {
JSON string
}
}
}
// newConfigFromFile reads yaml file and load it.
func newConfigFromFile(filePath string) (*Config, error) {
body, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return parseYAML(body)
}
// newConfigFromHTTP downloads yaml file with HTTP GET request from the URL,
// and load it as configuration.
func newConfigFromHTTP(url string) (config *Config, err error) {
client := &http.Client{}
res, err := client.Get(url)
if err != nil {
return
}
if res.StatusCode >= 300 {
err = fmt.Errorf("server returned status " + strconv.Itoa(res.StatusCode))
return
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
return parseYAML(body)
}
func parseYAML(body []byte) (config *Config, err error) {
var cfgYAML configYAML
err = yaml.Unmarshal(body, &cfgYAML)
if err != nil {
return
}
var bridges []Bridge
for _, b := range cfgYAML.Bridges {
input := GetInputSource(b.Input.Source)
output := GetOutputTarget(b.Output.Target, b.Output.Options)
var converter Converter
if b.Converter.JSON != "" {
converter = NewJSONConverter([]byte(b.Converter.JSON))
}
bridges = append(bridges, NewBridge(b.Name, input, output, converter))
}
return &Config{
Server: struct {
PathPrefix string
Port int
}{cfgYAML.Server.PathPrefix, cfgYAML.Server.Port},
Bridges: bridges,
}, nil
}