-
Notifications
You must be signed in to change notification settings - Fork 11
/
env.go
78 lines (63 loc) · 1.37 KB
/
env.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/tidwall/buntdb"
)
type Env struct {
Acc map[string]string
Conf string
DB *buntdb.DB
At *Token
}
// 读取配置文件中的appid和secret值到一个map中
func (e *Env) GetAccounts(file string) {
accounts := make([]Account, 1)
e.Conf = file
if file == "" {
e.Conf = "account.json"
}
if _, err := os.Stat(e.Conf); err != nil {
os.Exit(1)
}
raw, err := ioutil.ReadFile(file)
if err != nil {
os.Exit(1)
}
json.Unmarshal(raw, &accounts)
for _, a := range accounts {
e.Acc[a.AppID] = a.Secret
}
return
}
func (e *Env) GetValue(appid string, key string) string {
var value string
err := e.DB.View(func(tx *buntdb.Tx) error {
v, err := tx.Get(appid + "_" + key)
if err != nil {
return err
}
value = v
return nil
})
if err != nil {
value = ""
}
return value
}
// 更新AppID上下文环境中的Access Token和到期时间
func (e *Env) UpdateTokens(appid string) {
timestamp := time.Now().Unix()
e.DB.Update(func(tx *buntdb.Tx) error {
tx.Delete(appid + "_timestamp")
tx.Delete(appid + "_access_token")
tx.Delete(appid + "_expires_in")
tx.Set(appid+"_timestamp", strconv.FormatInt(timestamp, 10), nil)
tx.Set(appid+"_access_token", e.At.AccessToken, nil)
tx.Set(appid+"_expires_in", strconv.Itoa(e.At.Expire), nil)
return nil
})
}