cic-internal-integration/apps/cic-eth/cic_eth/runnable/daemons/server.py
2021-11-02 18:50:14 +03:00

100 lines
2.9 KiB
Python

# standard imports
import json
import logging
import os
import sys
import uuid
from urllib.parse import parse_qsl, urlparse
import cic_eth.cli
import redis
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback
argparser = cic_eth.cli.ArgumentParser(arg_flags)
argparser.process_local_flags(local_arg_flags)
args = argparser.parse_args()
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
celery_app = cic_eth.cli.CeleryApp.from_config(config)
from cic_eth.api.api_task import Api
# uwsgi application
def application(env, start_response):
parsed_url = urlparse(env['REQUEST_URI']) # /api
path = parsed_url.path
params = dict(parse_qsl(parsed_url.query))
request_method = env['REQUEST_METHOD']
chain_spec = config.get('CHAIN_SPEC')
redis_host = config.get('REDIS_HOST')
redis_port = config.get('REDIS_PORT')
redis_db = config.get('REDIS_DB')
celery_queue = config.get('CELERY_QUEUE')
redis_channel = str(uuid.uuid4())
r = redis.Redis(redis_host, redis_port, redis_db)
ps = r.pubsub()
ps.subscribe(redis_channel)
ps.get_message() # Subscription Object
api = Api(
chain_spec,
queue=celery_queue,
callback_param='{}:{}:{}:{}'.format(
redis_host, redis_port, redis_db, redis_channel),
callback_task='cic_eth.callbacks.redis.redis',
callback_queue=celery_queue,
)
if path == '/list':
address = params.get('address')
print('address', address)
# address, limit=10, external_task=None, external_queue=None
api.list(address)
elif path == '/balance':
api.balance(**params)
elif path == '/create_account':
api.create_account(**params)
elif path == '/ping':
api.ping(**params)
elif path == '/transfer':
api.transfer(**params)
elif path == '/transfer_from':
api.transfer_from(**params)
elif path == '/token':
api.token(**params)
elif path == '/tokens':
api.tokens(**params)
elif path == '/default_token':
api.default_token()
ps.get_message() # returns None !?
try:
o = ps.get_message(timeout=config.get('REDIS_TIMEOUT'))
except TimeoutError as e:
sys.stderr.write(
'got no new address from cic-eth before timeout: {}\n'.format(e))
sys.exit(1)
ps.unsubscribe()
m = json.loads(o['data'])
print(m['result'])
data = {"path": path,
"request_method": request_method, "result": m}
json_data = json.dumps(data)
content = json_data.encode('utf-8')
headers = []
headers.append(('Content-Length', str(len(content))),)
headers.append(('Access-Control-Allow-Origin', '*',))
headers.append(('Content-Type', 'application/json',))
start_response('200 OK', headers)
return [content]