/
apiCall.go
510 lines (454 loc) · 12.5 KB
/
apiCall.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package app
import (
"bytes"
"database/sql"
"encoding/json"
"github.com/EPICPaaS/appmsgsrv/db"
"github.com/EPICPaaS/go-uuid/uuid"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
const (
APICALL_EXIST = "select id from api_call where customer_id = ? and tenant_id = ? and caller_id=? and type =? and api_name = ? and sharding = ? "
APICALL_ADD = "UPDATE api_call SET count = count+1 ,updated=? WHERE customer_id = ? and tenant_id = ? and caller_id=? and type =? and api_name = ? and sharding = ?"
INSERT_APICALL = "insert into api_call(id , customer_id,tenant_id,caller_id,type,api_name,count,sharding,created,updated) values(?,?,?,?,?,?,?,?,?,?)"
GET_APICALL_COUNT = "select sum(count) as count from api_call WHERE customer_id = ? and tenant_id = ? and api_name = ? "
GET_CUSTOMER_COUNT = "select sum(count) as count from api_call WHERE customer_id = ? and api_name = ? "
SELECT_EXIST = "select id from quota where customer_id = ? and tenant_id=? and api_name=? and type = ?"
SELECT_QUOTA = "select id , customer_id,tenant_id,api_name,type,value,created,updated from quota where customer_id = ? and tenant_id=? and api_name=?"
UPDATE_QUOTA = "update quota set value=? , updated =? where customer_id = ? and tenant_id=? and api_name=? and type = ?"
INSERT_QUOTA = "insert into quota(id , customer_id,tenant_id,api_name,type,value,created,updated) values(?,?,?,?,?,?,?,?)"
SELECT_QUOTA_ALL = "select id , customer_id,tenant_id,api_name,type,value,created,updated from quota"
/*配额类型*/
EXPIRE = "expire"
API_CNT = "api_cnt"
)
var QuotaAll = make(map[string]Quota)
var LoadQuotaTime = time.NewTicker(5 * time.Minute)
type ApiCall struct {
Id string
CustomerId string
TenantId string
CallerId string
Type string
ApiName string
Count int
Sharding int
Created time.Time
Updated time.Time
}
type Quota struct {
Id string
CustomerId string
TenantId string
ApiName string
Type string
Value string
Created time.Time
Updated time.Time
lock sync.Mutex
}
//记录api调用次数
func ApiCallStatistics(w http.ResponseWriter, r *http.Request) bool {
baseRes := baseResponse{AuthErr, "request is not available"}
res := map[string]interface{}{"baseResponse": &baseRes}
resBody := "request is not available"
body, err := ioutil.ReadAll(r.Body)
if err != nil {
baseRes.Ret = ParamErr
logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error())
RetPWriteJSON(w, r, res, &resBody, time.Now())
return false
}
//将body数据写回到request
rr := bytes.NewReader(body)
bodyRc := ioutil.NopCloser(rr)
r.Body = bodyRc
var args map[string]interface{}
if err := json.Unmarshal(body, &args); err != nil {
logger.Errorf(" json.Unmarshal failed (%s)", err)
baseRes.Ret = ParamErr
RetPWriteJSON(w, r, res, &resBody, time.Now())
return false
}
baseReq := args["baseRequest"].(map[string]interface{})
token := baseReq["token"].(string)
appName := r.URL.String()
var tenantId, cllerId string
sharding := 0
deviceType := "app"
if baseReq["deviceType"] != nil {
deviceType = baseReq["deviceType"].(string)
deviceType = strings.ToLower(deviceType)
}
/* Token 校验,分为用户校验和应用校验*/
if deviceType == DEVICE_TYPE_IOS || deviceType == DEVICE_TYPE_ANDROID { //移动端和网页端
var user *member
if args["userName"] != nil { // 登录api接口
userName := args["userName"].(string)
user = getUserByCode(userName)
} else { //发送消息接口
user = getUserByToken(token)
}
if nil == user {
logger.Trace("api_call error: [Logon failure]")
baseRes.ErrMsg = "Auth failure"
RetPWriteJSON(w, r, res, &resBody, time.Now())
return false
}
tenantId = user.TenantId
cllerId = user.Uid
} else { //应用校验
application, err := getApplicationByToken(token)
if nil != err || nil == application {
logger.Trace("api_call error: [Logon failure]")
baseRes.ErrMsg = "Auth failure"
RetPWriteJSON(w, r, res, &resBody, time.Now())
return false
}
//应用10个分片
sharding = rand.Intn(10)
tenantId = application.TenantId
cllerId = application.Id
}
//获取租户信息
tenant := getTenantById(tenantId)
if tenant == nil {
logger.Errorf("not found tenantId : %s", tenantId)
baseRes.Ret = OverQuotaApicall
baseRes.ErrMsg = "not found tenant"
RetPWriteJSON(w, r, res, &resBody, time.Now())
return false
}
apiCall := &ApiCall{
CustomerId: tenant.CustomerId,
TenantId: tenantId,
CallerId: cllerId,
Type: deviceType,
ApiName: appName,
Count: 1, //默认值1
Sharding: sharding,
}
//检验此调用是否合法
if !ValidApiCall(apiCall) {
baseRes.Ret = OverQuotaApicall
baseRes.ErrMsg = "apicall auth failure"
RetPWriteJSON(w, r, res, &resBody, time.Now())
return false
}
if apiCallExist(apiCall) { //修改
addApiCount(apiCall)
} else { //新增
insertApiCall(apiCall)
}
return true
}
//判断该记录是否存在
func apiCallExist(apiCall *ApiCall) bool {
rows, err := db.MySQL.Query(APICALL_EXIST, apiCall.CustomerId, apiCall.TenantId, apiCall.CallerId, apiCall.Type, apiCall.ApiName, apiCall.Sharding)
if rows != nil {
defer rows.Close()
}
if err != nil {
logger.Error(err)
return false
}
if err = rows.Err(); err != nil {
logger.Error(err)
return false
}
return rows.Next()
}
//统计调用次数
func addApiCount(apiCall *ApiCall) bool {
tx, err := db.MySQL.Begin()
if err != nil {
logger.Error(err)
return false
}
_, err = tx.Exec(APICALL_ADD, time.Now().Local(), apiCall.CustomerId, apiCall.TenantId, apiCall.CallerId, apiCall.Type, apiCall.ApiName, apiCall.Sharding)
if err != nil {
logger.Error(err)
if err := tx.Rollback(); err != nil {
logger.Error(err)
}
return false
}
//提交操作
if err := tx.Commit(); err != nil {
logger.Error(err)
return false
}
return true
}
//添加apiCall
func insertApiCall(apiCall *ApiCall) bool {
tx, err := db.MySQL.Begin()
if err != nil {
logger.Error(err)
return false
}
_, err = tx.Exec(INSERT_APICALL, uuid.New(), apiCall.CustomerId, apiCall.TenantId, apiCall.CallerId, apiCall.Type, apiCall.ApiName, apiCall.Count, apiCall.Sharding, time.Now().Local(), time.Now().Local())
if err != nil {
logger.Error(err)
if err := tx.Rollback(); err != nil {
logger.Error(err)
}
return false
}
if err := tx.Commit(); err != nil {
logger.Error(err)
return false
}
return true
}
//获取没用户/应用调用api次数
func getApiCallCount(customerId, tenantId, apiName string) int {
count := 0
var rows *sql.Rows
var err error
//查询但customer下的所有租户调用次数
if tenantId == "*" {
rows, err = db.MySQL.Query(GET_CUSTOMER_COUNT, customerId, apiName)
} else {
rows, err = db.MySQL.Query(GET_APICALL_COUNT, customerId, tenantId, apiName)
}
if rows != nil {
defer rows.Close()
}
if err != nil {
logger.Error(err)
return count
}
if err = rows.Err(); err != nil {
logger.Error(err)
return count
}
//取出count
for rows.Next() {
if err = rows.Scan(&count); err != nil {
//logger.Error(err)
return count
}
return count
}
return count
}
//记录api调用次数
func (*app) SyncQuota(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method Not Allowed", 405)
return
}
baseRes := baseResponse{OK, ""}
body := ""
res := map[string]interface{}{"baseResponse": &baseRes}
defer RetPWriteJSON(w, r, res, &body, time.Now())
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
res["ret"] = ParamErr
logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error())
return
}
body = string(bodyBytes)
var args map[string]interface{}
if err := json.Unmarshal(bodyBytes, &args); err != nil {
baseRes.ErrMsg = err.Error()
baseRes.Ret = ParamErr
return
}
baseReq := args["baseRequest"].(map[string]interface{})
token := baseReq["token"].(string)
//应用校验
_, err = getApplicationByToken(token)
if nil != err {
baseRes.Ret = AuthErr
return
}
quotaM := args["quata"].(map[string]interface{})
quota := &Quota{
CustomerId: quotaM["customerId"].(string),
TenantId: quotaM["tenantId"].(string),
ApiName: quotaM["apiName"].(string),
Type: quotaM["type"].(string),
Value: quotaM["value"].(string),
}
retBool := false
if isExistQuota(quota) {
retBool = updateQuota(quota)
} else {
retBool = insertQuota(quota)
}
if !retBool {
baseRes.Ret = InternalErr
baseRes.ErrMsg = "sync quota fail !"
return
}
}
// 判断配额是否存在.
func isExistQuota(quota *Quota) bool {
rows, err := db.MySQL.Query(SELECT_EXIST, quota.CustomerId, quota.TenantId, quota.ApiName, quota.Type)
if rows != nil {
defer rows.Close()
}
if err != nil {
logger.Error(err)
return false
}
if err = rows.Err(); err != nil {
logger.Error(err)
return false
}
return rows.Next()
}
//修改quota
func updateQuota(quota *Quota) bool {
tx, err := db.MySQL.Begin()
if err != nil {
logger.Error(err)
return false
}
_, err = tx.Exec(UPDATE_QUOTA, quota.Value, time.Now().Local(), quota.CustomerId, quota.TenantId, quota.ApiName, quota.Type)
if err != nil {
logger.Error(err)
if err := tx.Rollback(); err != nil {
logger.Error(err)
}
return false
}
if err = tx.Commit(); err != nil {
logger.Error(err)
return false
}
return true
}
//修改quota
func insertQuota(quota *Quota) bool {
tx, err := db.MySQL.Begin()
if err != nil {
logger.Error(err)
return false
}
_, err = tx.Exec(INSERT_QUOTA, uuid.New(), quota.CustomerId, quota.TenantId, quota.ApiName, quota.Type, quota.Value, time.Now().Local(), time.Now().Local())
if err != nil {
logger.Error(err)
if err := tx.Rollback(); err != nil {
logger.Error(err)
}
return false
}
if err = tx.Commit(); err != nil {
logger.Error(err)
return false
}
return true
}
// 获取配额信息.
func GetQuotas(customerId, tenantId, apiName string) ([]Quota, error) {
quotas := []Quota{}
rows, err := db.MySQL.Query(SELECT_QUOTA, customerId, tenantId, apiName)
if rows != nil {
defer rows.Close()
}
if err != nil {
logger.Error(err)
return nil, err
}
for rows.Next() {
quota := Quota{}
if err := rows.Scan("a.Id, "a.CustomerId, "a.TenantId, "a.ApiName, "a.Type, "a.Value, "a.Created, "a.Created); err != nil {
logger.Error(err)
return nil, err
}
quotas = append(quotas, quota)
}
if err = rows.Err(); err != nil {
logger.Error(err)
return nil, err
}
return quotas, err
}
//初始化配额配置
func InitQuotaAll() {
rows, err := db.MySQL.Query(SELECT_QUOTA_ALL)
if err != nil {
logger.Errorf("load quota err [%s]", err)
}
if rows != nil {
defer rows.Close()
}
QuotaAll = nil
QuotaAll = make(map[string]Quota)
key := bytes.Buffer{}
for rows.Next() {
quota := Quota{}
if err := rows.Scan("a.Id, "a.CustomerId, "a.TenantId, "a.ApiName, "a.Type, "a.Value, "a.Created, "a.Created); err != nil {
logger.Errorf("load quota err [%s]", err)
break
}
key.WriteString(quota.CustomerId)
key.WriteString(quota.TenantId)
key.WriteString(quota.ApiName)
key.WriteString(quota.Type)
QuotaAll[key.String()] = quota
key.Reset()
}
if err = rows.Err(); err != nil {
logger.Errorf("load quota err [%s]", err)
}
}
//定时加载配额信息5分钟加载一次
func LoadQuotaAll() {
for _ = range LoadQuotaTime.C {
InitQuotaAll()
}
}
//检验本次调用是否合法
func ValidApiCall(apiCall *ApiCall) bool {
//校验时间期限
key := apiCall.CustomerId + apiCall.TenantId + apiCall.ApiName + EXPIRE
quota, ok := QuotaAll[key]
//该租户不存在配置侧查询全局配置
if !ok {
key := apiCall.CustomerId + "*" + apiCall.ApiName + EXPIRE
quota, ok = QuotaAll[key]
}
if ok {
validTime, err := time.ParseInLocation("2006/01/02 15:04:05", quota.Value, time.Local)
if err != nil || validTime.Before(time.Now().Local()) {
logger.Error(err)
return false
}
//校验api调用计数
key = apiCall.CustomerId + apiCall.TenantId + apiCall.ApiName + API_CNT
quota, ok = QuotaAll[key]
if !ok {
key = apiCall.CustomerId + "*" + apiCall.ApiName + API_CNT
quota, ok = QuotaAll[key]
}
if ok {
quotaCount, err := strconv.Atoi(quota.Value)
if err != nil {
logger.Error(err)
return false
}
if quotaCount == -1 { //-1 表示不限限制次数
return true
}
count := getApiCallCount(quota.CustomerId, quota.TenantId, quota.ApiName)
if quotaCount > count {
return true
}
} else {
return false
}
} else {
return false
}
return false
}