forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_api.go
88 lines (72 loc) · 2.09 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
87
88
// +build coprocess
// +build !grpc
package main
/*
#include <stdio.h>
#include "coprocess/sds/sds.h"
#include "coprocess/api.h"
#ifdef ENABLE_PYTHON
#include "coprocess/python/dispatcher.h"
#include "coprocess/python/binding.h"
#endif
*/
import "C"
import (
"github.com/TykTechnologies/logrus"
"github.com/TykTechnologies/tykcommon"
)
// CoProcessDefaultKeyPrefix is used as a key prefix for this CP.
const CoProcessDefaultKeyPrefix string = "coprocess-data:"
// TODO: implement INCR, DECR?
// TykStoreData is a CoProcess API function for storing data.
//export TykStoreData
func TykStoreData(CKey *C.char, CValue *C.char, CTTL C.int) {
key := C.GoString(CKey)
value := C.GoString(CValue)
ttl := int64(CTTL)
thisStorageHandler := GetGlobalLocalStorageHandler(CoProcessDefaultKeyPrefix, false)
thisStorageHandler.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)
thisStorageHandler := GetGlobalLocalStorageHandler(CoProcessDefaultKeyPrefix, false)
// TODO: return error
val, _ := thisStorageHandler.GetKey(key)
return C.CString(val)
}
// TykTriggerEvent is a CoProcess API function for triggering Tyk system events.
//export TykTriggerEvent
func TykTriggerEvent(CEventName *C.char, CPayload *C.char) {
eventName := C.GoString(CEventName)
payload := C.GoString(CPayload)
FireSystemEvent(tykcommon.TykEvent(eventName), EventMetaDefault{
Message: payload,
})
}
// CoProcessLog is a bridge for using Tyk log from CP.
//export CoProcessLog
func CoProcessLog(CMessage *C.char, CLogLevel *C.char) {
var message, logLevel string
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)
}
}