Skip to content

Commit

Permalink
coprocess: reimplement gateway bindings using ctypes
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasinsaurralde committed Aug 21, 2019
1 parent 6c12642 commit e30e234
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 97 deletions.
9 changes: 5 additions & 4 deletions coprocess/python/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from tyk.object import TykCoProcessObject
from tyk.event import TykEvent

# from gateway import TykGateway as tyk
# TODO: handle exceptions here
from gateway import TykGateway as tyk

import sys
def except_hook(type, value, traceback):
# tyk.log_error("{0}".format(value))
tyk.log_error("{0}".format(value))
pass

sys.excepthook = except_hook
Expand All @@ -19,7 +20,7 @@ class TykDispatcher:
'''A simple dispatcher'''

def __init__(self, bundle_root_path):
# tyk.log("Initializing dispatcher", "info")
tyk.log("Initializing dispatcher", "info")
self.bundle_root_path = bundle_root_path
self.bundles = []
self.hook_table = {}
Expand Down Expand Up @@ -89,5 +90,5 @@ def dispatch_event(self, event_json):
raise Exception("Couldn't dispatch '{0}' event: {1}", event.handler_name, e)

def reload(self):
# tyk.log("Reloading event handlers and middlewares.", "info")
tyk.log("Reloading event handlers and middlewares.", "info")
pass
33 changes: 33 additions & 0 deletions coprocess/python/tyk/gateway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ctypes
parent = ctypes.cdll.LoadLibrary(None)

parent.TykGetData.argtypes = [ctypes.c_char_p]
parent.TykGetData.restype = ctypes.c_char_p

parent.TykStoreData.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int]

class TykGateway():
def log(message, level):
message_p = ctypes.c_char_p(bytes(message, "utf-8"))
level_p = ctypes.c_char_p(bytes(level, "utf-8"))
parent.CoProcessLog(message_p, level_p)

def log_error(message):
message_p = ctypes.c_char_p(bytes(message, "utf-8"))
level_p = ctypes.c_char_p(bytes("error", "utf-8"))
parent.CoProcessLog(message_p, level_p)

def get_data(key):
key_p = ctypes.c_char_p(bytes(key, "utf-8"))
return parent.TykGetData(key_p)

def store_data(key, value, ttl):
key_p = ctypes.c_char_p(bytes(key, "utf-8"))
value_p = ctypes.c_char_p(bytes(value, "utf-8"))
ttl_int = ctypes.c_int(ttl)
parent.TykStoreData(key_p, value_p, ttl_int)

def trigger_event(name, payload):
name_p = ctypes.c_char_p(bytes(name, "utf-8"))
payload_p = ctypes.c_char_p(bytes(payload, "utf-8"))
parent.TykTriggerEvent(name_p, payload_p)
78 changes: 0 additions & 78 deletions coprocess/python/tyk/gateway_wrapper.c

This file was deleted.

8 changes: 0 additions & 8 deletions coprocess/python/tyk/gateway_wrapper.h

This file was deleted.

8 changes: 4 additions & 4 deletions coprocess/python/tyk/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import tyk.decorators as decorators
from tyk.loader import MiddlewareLoader
# from gateway import TykGateway as tyk
from gateway import TykGateway as tyk

HandlerDecorators = list( map( lambda m: m[1], inspect.getmembers(decorators, inspect.isclass) ) )

class TykMiddleware:
def __init__(self, filepath, bundle_root_path=None):
# tyk.log( "Loading module: '{0}'".format(filepath), "info")
tyk.log( "Loading module: '{0}'".format(filepath), "info")
self.filepath = filepath
self.handlers = {}

Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self, filepath, bundle_root_path=None):
self.register_handlers()
self.cleanup()
except Exception as e:
# tyk.log_error("Middleware initialization error: {0}".format(e))
tyk.log_error("Middleware initialization error: {0}".format(e))
pass

def register_handlers(self):
Expand All @@ -64,7 +64,7 @@ def build_hooks_and_event_handlers(self):
for handler in self.handlers[hook_type]:
handler.middleware = self
hooks[handler.name] = handler
# tyk.log("Loading hook '{0}' ({1})".format(handler.name, self.filepath), "debug")
tyk.log("Loading hook '{0}' ({1})".format(handler.name, self.filepath), "debug")
return hooks

def cleanup(self):
Expand Down
3 changes: 0 additions & 3 deletions gateway/coprocess_api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// +build coprocess
// +build !grpc

package gateway

import "C"
Expand Down

0 comments on commit e30e234

Please sign in to comment.