-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (51 loc) · 1022 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Config struct {
LB URI `json:"lb"`
Servers []ServerURI `json:"servers"`
Protocol Algo `json:"protocol"`
}
type Algo int
const (
RoundRobin Algo = iota
WeightedRoundRobin
)
type URI struct {
Host string `json:"host"`
Port int `json:"port"`
}
type ServerURI struct {
Host string `json:"host"`
Port int `json:"port"`
Weight int `json:"weight"`
Uri string `json:"uri"`
}
func ReadConfig() *Config {
f, err := os.Open("config.json")
if err != nil {
panic(fmt.Errorf("%w", err))
}
defer f.Close()
b, _ := ioutil.ReadAll(f)
c := &Config{}
err = json.Unmarshal(b, c)
if err != nil {
panic(fmt.Errorf("%w", err))
}
servers := []ServerURI{}
for _, server := range c.Servers {
if server.Weight < 1 {
panic(fmt.Errorf("server weight cannot be less than 1"))
}
for i := 0; i < server.Weight; i += 1 {
servers = append(servers, server)
}
}
c.Servers = servers
return c
}