This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
config.go
147 lines (127 loc) · 3.22 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
package config
import (
"context"
"fmt"
"github.com/appleboy/gorush/config"
"github.com/google/wire"
"github.com/tsundata/assistant/internal/pkg/middleware/etcd"
"github.com/tsundata/assistant/internal/pkg/util"
clientv3 "go.etcd.io/etcd/client/v3"
"gopkg.in/yaml.v2"
"strings"
"sync"
"time"
)
type AppConfig struct {
kv *etcd.Client
once sync.Once
ID string
Name string `json:"name" yaml:"name"`
SvcAddr SvcAddr `json:"svc_addr" yaml:"svc_addr"`
Http Http `json:"http" yaml:"http"`
Rpc Rpc `json:"rpc" yaml:"rpc"`
Web Web `json:"web" yaml:"web"`
Gateway Gateway `json:"gateway" yaml:"gateway"`
Storage Storage `json:"storage" yaml:"storage"`
Jwt Jwt `json:"jwt" yaml:"jwt"`
Mysql Mysql `json:"mysql" yaml:"mysql"`
Redis Redis `json:"redis" yaml:"redis"`
Influx Influx `json:"influx" yaml:"influx"`
Jaeger Jaeger `json:"jaeger" yaml:"jaeger"`
Nats Nats `json:"nats" yaml:"nats"`
Rollbar Rollbar `json:"rollbar" yaml:"rollbar"`
Newrelic Newrelic `json:"newrelic" yaml:"newrelic"`
// Notification
config.ConfYaml
}
func NewConfig(id string, kv *etcd.Client) *AppConfig {
var xc AppConfig
xc.kv = kv
xc.Name = id
uuid := util.UUID()
xc.ID = uuid
xc.readConfig()
go xc.watch()
return &xc
}
func (c *AppConfig) readConfig() {
// common
resp, err := c.kv.Get(context.Background(), "config/common")
if err != nil {
panic(err)
}
var value []byte
for _, ev := range resp.Kvs {
value = ev.Value
}
err = yaml.Unmarshal(value, &c)
if err != nil {
panic(err)
}
// app
resp, err = c.kv.Get(context.Background(), fmt.Sprintf("config/%s", c.Name))
if err != nil {
panic(err)
}
if len(resp.Kvs) > 0 {
for _, ev := range resp.Kvs {
value = ev.Value
}
err = yaml.Unmarshal(value, &c)
if err != nil {
fmt.Println(err)
}
}
}
func (c *AppConfig) watch() {
c.once.Do(func() {
tick := time.NewTicker(10 * time.Second)
for range tick.C {
c.readConfig()
}
})
}
func (c *AppConfig) GetConfig(ctx context.Context, key string) (string, error) {
resp, err := c.kv.Get(ctx, fmt.Sprintf("config/%s", key))
if err != nil {
return "", err
}
if len(resp.Kvs) > 0 {
var value []byte
for _, ev := range resp.Kvs {
value = ev.Value
}
return util.ByteToString(value), nil
}
return "", nil
}
func (c *AppConfig) GetSetting(ctx context.Context, key string) (string, error) {
resp, err := c.kv.Get(ctx, fmt.Sprintf("setting/%s", key))
if err != nil {
return "", err
}
if len(resp.Kvs) > 0 {
var value []byte
for _, ev := range resp.Kvs {
value = ev.Value
}
return util.ByteToString(value), nil
}
return "", nil
}
func (c *AppConfig) SetSetting(ctx context.Context, key, value string) error {
_, err := c.kv.Put(ctx, fmt.Sprintf("setting/%s", key), value)
return err
}
func (c *AppConfig) GetSettings(ctx context.Context) (map[string]string, error) {
resp, err := c.kv.Get(ctx, "setting", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
if err != nil {
return nil, err
}
result := make(map[string]string)
for _, ev := range resp.Kvs {
result[strings.ReplaceAll(util.ByteToString(ev.Key), "setting/", "")] = util.ByteToString(ev.Value)
}
return result, nil
}
var ProviderSet = wire.NewSet(NewConfig)