forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_api.go
86 lines (71 loc) · 1.98 KB
/
coprocess_api.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
// +build coprocess
// +build !grpc
package main
/*
#include <stdio.h>
#include "coprocess/api.h"
#ifdef ENABLE_PYTHON
#include "coprocess/python/dispatcher.h"
#include "coprocess/python/binding.h"
#endif
*/
import "C"
import (
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/storage"
)
// CoProcessDefaultKeyPrefix is used as a key prefix for this CP.
const CoProcessDefaultKeyPrefix = "coprocess-data:"
// TODO: implement INCR, DECR?
// TykStoreData is a CoProcess API function for storing data.
//export TykStoreData
func TykStoreData(CKey, CValue *C.char, CTTL C.int) {
key := C.GoString(CKey)
value := C.GoString(CValue)
ttl := int64(CTTL)
store := storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
store.SetKey(key, value, ttl)
}
// TykGetData is a CoProcess API function for fetching data.
//export TykGetData
func TykGetData(CKey *C.char) *C.char {
key := C.GoString(CKey)
store := storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
// TODO: return error
val, _ := store.GetKey(key)
return C.CString(val)
}
// TykTriggerEvent is a CoProcess API function for triggering Tyk system events.
//export TykTriggerEvent
func TykTriggerEvent(CEventName, CPayload *C.char) {
eventName := C.GoString(CEventName)
payload := C.GoString(CPayload)
FireSystemEvent(apidef.TykEvent(eventName), EventMetaDefault{
Message: payload,
})
}
// CoProcessLog is a bridge for using Tyk log from CP.
//export CoProcessLog
func CoProcessLog(CMessage, CLogLevel *C.char) {
message := C.GoString(CMessage)
logLevel := C.GoString(CLogLevel)
switch logLevel {
case "debug":
log.WithFields(logrus.Fields{
"prefix": CoProcessName,
}).Debug(message)
case "error":
log.WithFields(logrus.Fields{
"prefix": CoProcessName,
}).Error(message)
case "warning":
log.WithFields(logrus.Fields{
"prefix": CoProcessName,
}).Warning(message)
default:
log.WithFields(logrus.Fields{
"prefix": CoProcessName,
}).Info(message)
}
}