-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfo.go
138 lines (127 loc) · 2.78 KB
/
info.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
package configurator
import (
"fmt"
"github.com/subosito/gotenv"
"github.com/xincao9/dkv/client"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
const (
FN = "application"
Ext = "json"
envKey = "env"
groupKey = "group"
projectKey = "project"
versionKey = "version"
masterKey = "master"
slavesKey = "slaves"
pathKey = "path"
path = "HOME"
syncPeriod = 30
)
var (
I *info
once = &sync.Once{}
)
func init () {
startInfo()
}
func startInfo () {
once.Do(initInfo)
}
func initInfo() {
if I != nil {
return
}
var err error
err = gotenv.Load(filepath.Join(os.Getenv(path), ".env"))
if err != nil {
log.Printf("warning file '%s' not found", filepath.Join(os.Getenv(path), ".env"))
log.Fatalln(err)
}
I, err = newInfo()
if err != nil {
log.Fatalf("Fatal error configurator newInfo : %v\n", err)
}
err = I.sync()
if err != nil {
log.Fatalf("Fatal error configurator sync : %v\n", err)
}
go func() {
for range time.Tick(time.Second * syncPeriod) {
err = I.sync()
if err != nil {
log.Printf("sync err: %v", err)
}
}
}()
}
func newInfo() (*info, error) {
inf := &info{}
inf.env = os.Getenv(envKey)
inf.group = os.Getenv(groupKey)
inf.project = os.Getenv(projectKey)
inf.version = os.Getenv(versionKey)
inf.master = os.Getenv(masterKey)
inf.Path = filepath.Join(os.Getenv(path), inf.group, inf.project, inf.version)
err := os.MkdirAll(inf.Path, 0755)
if err != nil {
return inf, err
}
if os.Getenv(slavesKey) != "" {
inf.slaves = strings.Split(os.Getenv(slavesKey), ",")
inf.client, err = client.NewMS(inf.master, inf.slaves, time.Second)
} else {
inf.client, err = client.New(inf.master, time.Second)
}
return inf, err
}
type info struct {
env string
group string
project string
version string
master string
slaves []string
Path string
client *client.Client
}
func (inf *info) key() string {
return fmt.Sprintf("configurator|%s|%s|%s|%s", inf.env, inf.group, inf.project, inf.version)
}
func (inf *info) sync() error {
k := inf.key()
r, err := inf.client.GetOrRealtime(k, true)
if err != nil {
return err
}
if r.Code != 200 {
return fmt.Errorf("dkv response code: %d, message: %s\n", r.Code, r.Message)
}
log.Printf("dkv response: %v\n", r)
d := r.KV.V
if d == "" {
return nil
}
err = ioutil.WriteFile(filepath.Join(inf.Path, fmt.Sprintf("%s.%s", FN, Ext)), []byte(d), 0644)
if err != nil {
return err
}
return nil
}
func (inf *info) Map() map[string]interface{} {
m := make(map[string]interface{})
m[envKey] = inf.env
m[groupKey] = inf.group
m[projectKey] = inf.project
m[versionKey] = inf.version
m[masterKey] = inf.master
m[slavesKey] = inf.slaves
m[pathKey] = inf.Path
return m
}