-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconf.go
executable file
·141 lines (112 loc) · 3.13 KB
/
conf.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
package rcc
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/baidu/brcc/brcc-go-sdk/logutil"
)
const (
DefaultCallbackIntervalInt = 300 // 默认300s触发一次更新拉取
DefaultRequestTimeoutInt = 5 // 默认5秒钟接口调用超时时间
DefaultFilePath = "/tmp/rcc"
)
// Conf
type Conf struct {
ProjectName string `toml:"projectName"`
EnvName string `toml:"envName"`
ServerUrl string `toml:"serverUrl"`
ApiPassword string `toml:"apiPassword"`
VersionName string `toml:"versionName"`
EnableCallback bool `toml:"enableCallback"`
CallbackIntervalInt int64 `toml:"callbackInterval"`
RequestTimeoutInt int64 `toml:"requestTimeout"`
EnableCache bool `toml:"enableCache"`
CacheDir string `json:"cacheDir,omitempty"`
Token string
EnvId int64
CallbackInterval time.Duration
RequestTimeout time.Duration
}
// NewConf create Conf from file
func NewConf(name string) (*Conf, error) {
// initial log
logutil.InitLog()
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer func() {
_ = f.Close()
}()
var ret Conf
if _, err := toml.DecodeReader(f, &ret); err != nil {
return nil, err
}
ret.CallbackInterval = time.Duration(ret.CallbackIntervalInt) * time.Second
ret.RequestTimeout = time.Duration(ret.RequestTimeoutInt) * time.Second
return &ret, nil
}
func (c *Conf) normalize(ctx context.Context) error {
if c.ProjectName == "" || c.EnvName == "" || c.ApiPassword == "" || c.ServerUrl == "" || c.VersionName == "" {
return fmt.Errorf("rcc conf validate error: %v", c)
}
if c.CacheDir == "" {
c.CacheDir = DefaultFilePath
}
if c.RequestTimeoutInt == 0 {
c.RequestTimeoutInt = DefaultRequestTimeoutInt
}
c.RequestTimeout = time.Second * time.Duration(c.RequestTimeoutInt)
if c.EnableCallback {
if c.CallbackIntervalInt == 0 {
c.CallbackIntervalInt = DefaultCallbackIntervalInt
}
c.CallbackInterval = time.Second * time.Duration(c.CallbackIntervalInt)
}
err := c.refreshToken(ctx)
if err != nil {
return err
}
err = c.acquireEnvironment(ctx)
if err != nil {
return err
}
return nil
}
func (c *Conf) refreshToken(ctx context.Context) error {
url := requestTokenUrl(c)
data := map[string]interface{}{
"projectName": c.ProjectName,
"apiPassword": c.ApiPassword,
}
rspData := struct {
Token string `json:"token"`
}{}
err := (&httpRequester{ctx, &http.Client{Timeout: c.RequestTimeout}}).Post(url, data, &rspData)
if err != nil {
return fmt.Errorf("refresh Token error: %s", err)
}
if rspData.Token == "" {
return fmt.Errorf("refresh Token error: token is empty")
}
c.Token = rspData.Token
return nil
}
func (c *Conf) acquireEnvironment(ctx context.Context) error {
url := apiEnvironment(c)
rspData := struct {
EnvironmentId int64 `json:"environmentId"`
}{}
err := (&httpRequester{ctx, &http.Client{Timeout: c.RequestTimeout}}).Get(url, &rspData)
if err != nil {
return fmt.Errorf("acquire Environment error: %s", err)
}
if rspData.EnvironmentId == 0 {
return fmt.Errorf("acquire Environment error: environmentId is zero")
}
c.EnvId = rspData.EnvironmentId
return nil
}