-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
156 lines (128 loc) · 3.58 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package ernest_config_client
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/jinzhu/gorm"
"github.com/nats-io/go-nats"
"github.com/r3labs/akira"
"gopkg.in/redis.v3"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
var err error
var cfg map[string]interface{}
type Config struct {
uri string
nats akira.Connector
postgres *gorm.DB
redis *redis.Client
}
func NewConfig(natsURI string) *Config {
c := Config{uri: natsURI}
c.setup()
return &c
}
func (c *Config) setup() {
c.Nats()
}
func (c *Config) SetConnector(conn akira.Connector) {
c.nats = conn
}
func (c *Config) Nats() *nats.Conn {
if c.nats != nil {
return c.nats.(*nats.Conn)
}
for {
c.nats, err = nats.Connect(c.uri)
if err != nil {
log.Println("Waiting for nats on " + c.uri + ". Retrying in 2 seconds ...")
time.Sleep(time.Second * 2)
continue
}
log.Println("Successfully connected to nats on '" + c.uri + "'")
break
}
return c.nats.(*nats.Conn)
}
func (c *Config) Postgres(table string) *gorm.DB {
var resp *nats.Msg
var pgCfg map[string]interface{}
for c.postgres == nil {
resp, err = c.nats.Request("config.get.postgres", nil, time.Second)
if err != nil {
log.Println("Waiting for config.get.postgres response. Retrying in 5 seconds ...")
time.Sleep(time.Second * 5)
continue
}
err = json.Unmarshal(resp.Data, &pgCfg)
if err != nil {
log.Println("Invalid config.get.postgres response, received '" + string(resp.Data) + "'. Retrying in 5 seconds ...")
time.Sleep(time.Second * 5)
continue
}
uri := fmt.Sprintf("%s/%s?sslmode=disable", pgCfg["url"], table)
c.postgres, err = gorm.Open("postgres", uri)
if err != nil {
log.Println("Unsuccesful connection to postgres '" + uri + "'. Retrying in 5 seconds ...")
time.Sleep(time.Second * 5)
c.postgres = nil
continue
}
log.Println("Successfully connected to postgres on '" + uri + "'")
}
return c.postgres
}
func (c *Config) Redis() *redis.Client {
if c.redis != nil {
return c.redis
}
var redisCfg struct {
Addr string `json:"addr"`
Password string `json:"password"`
DB int64 `json:"db"`
}
for c.redis == nil {
resp, err := c.nats.Request("config.get.redis", nil, time.Second)
if err != nil {
log.Println("Waiting for config.get.redis response. Retrying in 5 seconds ...")
time.Sleep(time.Second * 5)
continue
}
err = json.Unmarshal(resp.Data, &redisCfg)
if err != nil {
log.Println("Invalid config.get.redis response, received '" + string(resp.Data) + "'. Retrying in 5 seconds ...")
time.Sleep(time.Second * 5)
continue
}
redis := redis.NewClient(&redis.Options{
Addr: redisCfg.Addr,
Password: redisCfg.Password,
DB: redisCfg.DB,
})
pong, err := redis.Ping().Result()
if err != nil {
log.Println("Redis responded with '" + pong + "' to a ping request. Reconnecting in 5 seconds ...")
time.Sleep(time.Second * 5)
continue
}
log.Println("Successfully connected to redis on '" + redisCfg.Addr + "'")
c.redis = redis
}
return c.redis
}
func (c *Config) GetConfig(ctype string, result interface{}) error {
var msg *nats.Msg
for msg == nil {
msg, err = c.nats.Request("config.get."+ctype, nil, time.Second)
if err != nil {
log.Printf("Waiting for config.get.%s response. Retrying in 5 seconds ...", ctype)
time.Sleep(time.Second * 5)
continue
}
}
return json.Unmarshal(msg.Data, result)
}