-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
76 lines (64 loc) · 1.6 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
package consul
import (
"fmt"
"github.com/luraproject/lura/config"
)
type Config struct {
Address, Name string
Port int
Tags []string
}
// Namespace is the key to use to store and access the custom config data
const Namespace = "github_com/letgoapp/krakend-consul"
var (
// ErrNoConfig is the error to be returned when there is no config with the consul namespace
ErrNoConfig = fmt.Errorf("unable to create the consul client: no config")
// ErrBadConfig is the error to be returned when the config is not well defined
ErrBadConfig = fmt.Errorf("unable to create the consul client with the received config")
// ErrNoMachines is the error to be returned when the config has not defined one or more servers
ErrNoMachines = fmt.Errorf("unable to create the consul client without a set of servers")
)
func parse(e config.ExtraConfig, port int) (Config, error) {
cfg := Config{
Name: "krakend",
Port: port,
}
v, ok := e[Namespace]
if !ok {
return cfg, ErrNoConfig
}
tmp, ok := v.(map[string]interface{})
if !ok {
return cfg, ErrBadConfig
}
a, ok := tmp["address"]
if !ok {
return cfg, ErrNoMachines
}
cfg.Address, ok = a.(string)
if !ok {
return cfg, ErrNoMachines
}
if a, ok = tmp["name"]; ok {
cfg.Name, ok = a.(string)
}
cfg.Tags = parseTags(tmp)
return cfg, nil
}
func parseTags(cfg map[string]interface{}) []string {
result := []string{}
tags, ok := cfg["tags"]
if !ok {
return result
}
tgs, ok := tags.([]interface{})
if !ok {
return result
}
for _, tg := range tgs {
if t, ok := tg.(string); ok {
result = append(result, t)
}
}
return result
}