github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/coprocess_api.go (about)

     1  package gateway
     2  
     3  import "C"
     4  
     5  import (
     6  	"github.com/sirupsen/logrus"
     7  
     8  	"github.com/TykTechnologies/tyk/apidef"
     9  	"github.com/TykTechnologies/tyk/storage"
    10  )
    11  
    12  // CoProcessDefaultKeyPrefix is used as a key prefix for this CP.
    13  const CoProcessDefaultKeyPrefix = "coprocess-data:"
    14  
    15  // TODO: implement INCR, DECR?
    16  
    17  // TykStoreData is a CoProcess API function for storing data.
    18  //export TykStoreData
    19  func TykStoreData(CKey, CValue *C.char, CTTL C.int) {
    20  	key := C.GoString(CKey)
    21  	value := C.GoString(CValue)
    22  	ttl := int64(CTTL)
    23  
    24  	store := storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
    25  	store.SetKey(key, value, ttl)
    26  }
    27  
    28  // TykGetData is a CoProcess API function for fetching data.
    29  //export TykGetData
    30  func TykGetData(CKey *C.char) *C.char {
    31  	key := C.GoString(CKey)
    32  
    33  	store := storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix}
    34  	// TODO: return error
    35  	val, _ := store.GetKey(key)
    36  	return C.CString(val)
    37  }
    38  
    39  // TykTriggerEvent is a CoProcess API function for triggering Tyk system events.
    40  //export TykTriggerEvent
    41  func TykTriggerEvent(CEventName, CPayload *C.char) {
    42  	eventName := C.GoString(CEventName)
    43  	payload := C.GoString(CPayload)
    44  
    45  	FireSystemEvent(apidef.TykEvent(eventName), EventMetaDefault{
    46  		Message: payload,
    47  	})
    48  }
    49  
    50  // CoProcessLog is a bridge for using Tyk log from CP.
    51  //export CoProcessLog
    52  func CoProcessLog(CMessage, CLogLevel *C.char) {
    53  	message := C.GoString(CMessage)
    54  	logLevel := C.GoString(CLogLevel)
    55  	switch logLevel {
    56  	case "debug":
    57  		log.WithFields(logrus.Fields{
    58  			"prefix": "python",
    59  		}).Debug(message)
    60  	case "error":
    61  		log.WithFields(logrus.Fields{
    62  			"prefix": "python",
    63  		}).Error(message)
    64  	case "warning":
    65  		log.WithFields(logrus.Fields{
    66  			"prefix": "python",
    67  		}).Warning(message)
    68  	default:
    69  		log.WithFields(logrus.Fields{
    70  			"prefix": "python",
    71  		}).Info(message)
    72  	}
    73  }