-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
268 lines (225 loc) · 6.09 KB
/
builder.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package letsgo
/*
* DO NOT MODIFY
*/
import (
"crypto/md5"
"encoding/hex"
"strconv"
"time"
"github.com/time2k/letsgo-ng/config"
)
// NewHTTPQueryBuilder 实例化
func NewHTTPQueryBuilder(commp *CommonParams) *HTTPQueryBuilder {
return &HTTPQueryBuilder{CommonParams: commp}
}
// NewDBQueryBuilder 实例化
func NewDBQueryBuilder(commp *CommonParams) *DBQueryBuilder {
return &DBQueryBuilder{CommonParams: commp}
}
// NewScheduleBuilder 实例化
func NewScheduleBuilder(commp *CommonParams) *ScheduleBuilder {
return &ScheduleBuilder{CommonParams: commp}
}
/*
* http builder define
*/
// HTTPRequest http请求结构体
type HTTPRequest struct {
UniqID string
NeedCache bool
Rtype string
Method string
URL string
Header map[string]string
Postdata map[string]interface{}
}
// HTTPResponseResult http响应结构体
type HTTPResponseResult struct {
UniqID string
URL string
ResponseStatus int
HTTPStatus string
HTTPStatusCode int
ContentLength int64
Body []byte
}
// HTTPQueryBuilder 包含请求和多个响应channel的结构体
type HTTPQueryBuilder struct {
Requests []HTTPRequest
ResponseCH chan HTTPResponseResult
CacheExpireTime int32
*CommonParams
}
// GetBuilder 得到Builder信息
func (httpm *HTTPQueryBuilder) GetBuilder() *HTTPQueryBuilder {
return httpm
}
// GetCacheExpire 得到缓存超时信息
func (httpm *HTTPQueryBuilder) GetCacheExpire() int32 {
return httpm.CacheExpireTime
}
// GetHTTPUniqid 生成HTTP请求的唯一标识
func (httpm *HTTPQueryBuilder) GetHTTPUniqid(rtype string, method string, urls string, header map[string]string, postdata map[string]interface{}) string {
var UniqBase string
UniqBase += rtype + "|"
UniqBase += method + "|"
UniqBase += urls + "|"
/*if header != nil {
data := make(url.Values)
for k, v := range header {
data[k] = []string{v}
}
UniqBase += data.Encode() + "|"
}
if postdata != nil {
data := make(url.Values)
for k, v := range postdata {
data[k] = []string{v.(string)}
}
UniqBase += data.Encode() + "|"
}*/
UniqBase += strconv.FormatInt(time.Now().UnixNano(), 10)
md5Ctx := md5.New()
md5Ctx.Write([]byte(UniqBase))
return hex.EncodeToString(md5Ctx.Sum(nil))
}
// SetRequest 设置HTTP请求
func (httpm *HTTPQueryBuilder) SetRequest(needcache bool, rtype string, method string, url string, header map[string]string, postdata map[string]interface{}) string {
newRequest := HTTPRequest{}
newRequest.UniqID = httpm.GetHTTPUniqid(rtype, method, url, header, postdata)
newRequest.NeedCache = needcache
newRequest.Rtype = rtype
newRequest.Method = method
newRequest.URL = url
newRequest.Header = header
newRequest.Postdata = postdata
httpm.Requests = append(httpm.Requests, newRequest)
return newRequest.UniqID
}
// SetCacheExpire 设置缓存超时信息
func (httpm *HTTPQueryBuilder) SetCacheExpire(ExpireTime int32) {
httpm.CacheExpireTime = ExpireTime
}
// InitHTTP 初始化http相关
func (httpm *HTTPQueryBuilder) InitHTTP() {
httpm.ResponseCH = make(chan HTTPResponseResult, config.CACHEHTTP_CHANNEL_BUFFER_LEN)
}
// GetDebugInfo 得到debug信息
func (httpm *HTTPQueryBuilder) GetDebugInfo() *DebugInfo {
return httpm.CommonParams.Debug
}
/*
* http model define end
*/
/*
* db builder define
*/
// DBQueryBuilder 数据库模块结构体
type DBQueryBuilder struct {
UseCache bool
CacheKey string
CacheExpireTime int32
SQL string
SQLcondition []interface{}
Result interface{}
DBName string
*CommonParams
}
// IsUseCache 是否使用缓存
func (dbm *DBQueryBuilder) IsUseCache() bool {
return dbm.UseCache
}
// GetCacheKey 得到缓存的key
func (dbm *DBQueryBuilder) GetCacheKey() string {
return dbm.CacheKey
}
// SetCacheKey 设置缓存的key
func (dbm *DBQueryBuilder) SetCacheKey(CacheKey string) {
dbm.CacheKey = CacheKey
if !dbm.UseCache {
dbm.UseCache = true
}
}
// GetCacheExpire 得到缓存超时信息
func (dbm *DBQueryBuilder) GetCacheExpire() int32 {
return dbm.CacheExpireTime
}
// SetCacheExpire 设置缓存超时信息
func (dbm *DBQueryBuilder) SetCacheExpire(ExpireTime int32) {
dbm.CacheExpireTime = ExpireTime
if !dbm.UseCache {
dbm.UseCache = true
}
}
// GetBuilder 得到Builder信息
func (dbm *DBQueryBuilder) GetBuilder() *DBQueryBuilder {
return dbm
}
// SetSQL 设置SQL请求信息
func (dbm *DBQueryBuilder) SetSQL(sql string) {
dbm.SQL = sql
}
// GetDbname 得到数据库名称
func (dbm *DBQueryBuilder) GetDbname() string {
return dbm.DBName
}
// SetDbname 设置数据库名称
func (dbm *DBQueryBuilder) SetDbname(name string) {
dbm.DBName = name
}
// SetSQLcondition 设置SQL查询条件
func (dbm *DBQueryBuilder) SetSQLcondition(con interface{}) {
dbm.SQLcondition = append(dbm.SQLcondition, con)
}
// SetResult 设置接收的数据结构
func (dbm *DBQueryBuilder) SetResult(data interface{}) {
dbm.Result = data
}
// GetDebugInfo 得到debug信息
func (dbm *DBQueryBuilder) GetDebugInfo() *DebugInfo {
return dbm.CommonParams.Debug
}
/*
* db builder define end
*/
/*
* schedule builder define
*/
// FuncDesc 方法描述结构体
type FuncDesc struct {
ModelFunc
CommP *CommonParams
Args []interface{}
SEQID string
}
// ScheduleBuilder 结构体
type ScheduleBuilder struct {
FuncDescs []FuncDesc
DataCH chan ScheduleChan
*CommonParams
}
// GetBuilder 得到Builder信息
func (schedulem *ScheduleBuilder) GetBuilder() *ScheduleBuilder {
return schedulem
}
// SetSchedule 设置调度器信息
func (schedulem *ScheduleBuilder) SetSchedule(funcx ModelFunc, args ...interface{}) {
schedulem.FuncDescs = append(schedulem.FuncDescs, FuncDesc{ModelFunc: funcx, CommP: schedulem.CommonParams, Args: args})
}
// GetDebugInfo 得到debug信息
func (schedulem *ScheduleBuilder) GetDebugInfo() *DebugInfo {
return schedulem.CommonParams.Debug
}
// ScheduleChan 结构体
type ScheduleChan struct {
SEQID string
RET BaseReturnData
}
// InitSchedule 初始化调度器相关
func (schedulem *ScheduleBuilder) InitSchedule() {
schedulem.DataCH = make(chan ScheduleChan, config.SCHEDULE_CHANNEL_BUFFER_LEN)
}
/*
* schedule builder define end
*/