-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
136 lines (118 loc) · 3.43 KB
/
sync.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
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/couchbase/gocb/v2"
// "github.com/couchbase/go-couchbase"
)
/*
{
"value": "TrzdyE9qjetRWN0tisqUIo8YjL26Mhz5E-ZXHc0KeyvLu99JuXmd_ZiKyg8T6MJQHyiQAlpQd5XvsTmve_2M9e2qC05lEuN92NkNgG0p3pyYyuauR8PvHC1y6pO4c3IqnjhHcE0eh1bXljSd5pqGLgzGe0PpBJnunGwDOy8-4LEkn2XuILFPxwXgovWICaUvmvZPLSqk7I8hpNbmN0V89Q",
"accountType": "WECOM",
"expireIn": 7200,
"createdAt": "2021-10-27T12:32:02.502Z",
"updatedAt": "2021-12-28T06:42:34.263Z",
"_type": "Token"
}
*/
// {
// "value": "52_6nVKARh5ByQnTEWvm4H9ru_ECmvO20AVSNSa-qNcNYzWBwzmEjr1xfgcJNXALjRz9RJRaKHbs9aQtFh8UJzgiltp04ks453eN98SwXIPIQyHkX8roIerMkMxvWUrhR6Szn5LLqO2OkL9TlznZMMcABAIVV",
// "accountType": "MP",
// "expireIn": 7200,
// "createdAt": "2021-06-29T08:22:33.249Z",
// "updatedAt": "2021-12-28T07:01:13.331Z",
// "_type": "Token"
// }
func conncb(cb_addr string, user_name string, password string) (*gocb.Cluster, error) {
cluster, err := gocb.Connect(cb_addr, gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: user_name,
Password: password,
},
})
if err != nil {
return nil, err
}
return cluster, nil
}
func main() {
const staging_cb_addr = "172.16.5.35:8091"
const staging_cb_user = "xxxxxx"
const staging_cb_pwd = "xxxxxx"
const prod_cb_addr = "172.16.5.58:8091"
const prod_cb_user = "xxxxxx"
const prod_cb_pwd = "xxxxxx"
const token_id_wecom = "accessToken:wwc9a5cb2719ab6225"
const token_id_mp = "accessToken:wx88ed27696de5b9a4"
type Doc struct {
ExpireIn int `json:"expireIn"`
Type string `json:"_type"`
Value string `json:"value"`
AccountType string `json:"accountType"`
CreateAt string `json:"createdAt"`
UpdateAt string `json:"updatedAt"`
}
var srcdocument_wecom_token Doc
var srcdocument_mp_token Doc
cluster, err := conncb(prod_cb_addr, prod_cb_user, prod_cb_pwd)
if err != nil {
log.Fatal(err)
}
bucket := cluster.Bucket("wechat-connector")
err = bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
log.Fatal(err)
}
collection := bucket.DefaultCollection()
result, err := collection.Get(token_id_wecom, nil)
if err != nil {
log.Fatal(err)
}
err = result.Content(&srcdocument_wecom_token)
if err != nil {
log.Fatal(err)
}
prettyJSON, err := json.MarshalIndent(srcdocument_wecom_token, "", " ")
if err != nil {
log.Fatal("Failed to generate json for wecom token", err)
}
fmt.Printf("%s\n", string(prettyJSON))
result, err = collection.Get(token_id_mp, nil)
if err != nil {
log.Fatal(err)
}
err = result.Content(&srcdocument_mp_token)
if err != nil {
log.Fatal(err)
}
prettyJSON, err = json.MarshalIndent(srcdocument_mp_token, "", " ")
if err != nil {
log.Fatal("Failed to generate json for mp token", err)
}
fmt.Printf("%s\n", string(prettyJSON))
// connect staging couchbase ,and update token from prod couchbase.
cluster, err = conncb(staging_cb_addr, staging_cb_user, staging_cb_pwd)
if err != nil {
log.Fatal(err)
}
bucket = cluster.Bucket("wechat-connector")
err = bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
log.Fatal(err)
}
collection = bucket.DefaultCollection()
_, err = collection.Upsert(token_id_wecom, &srcdocument_wecom_token, nil)
if err != nil {
log.Fatal(err)
} else {
fmt.Println("wecom token synced!")
}
collection.Upsert(token_id_mp, &srcdocument_mp_token, nil)
if err != nil {
log.Fatal(err)
} else {
fmt.Println("mp token synced!")
}
}