2019-05-16 01:17:06 +02:00
|
|
|
import asyncio
|
2019-05-20 08:11:02 +02:00
|
|
|
import json
|
|
|
|
|
2019-05-16 01:17:06 +02:00
|
|
|
from aiohttp import web
|
|
|
|
|
2019-05-20 11:08:04 +02:00
|
|
|
from settings import config
|
2019-05-20 08:59:54 +02:00
|
|
|
from lbry_comment_server import database_dir
|
2019-05-20 11:08:04 +02:00
|
|
|
from lbry_comment_server.database import obtain_connection
|
2019-05-16 05:32:29 +02:00
|
|
|
|
|
|
|
ERRORS = {
|
|
|
|
'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'},
|
|
|
|
'INTERNAL': {'code': -32603, 'message': 'An internal error'},
|
|
|
|
'UNKNOWN': {'code': -1, 'message': 'An unknown or very miscellaneous error'},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CommentServer:
|
2019-05-16 01:17:06 +02:00
|
|
|
def __init__(self, port=2903):
|
|
|
|
self.port = port
|
|
|
|
self.app = web.Application(debug=True)
|
|
|
|
self.app.add_routes([web.post('/api', self.api)])
|
|
|
|
self.runner = None
|
|
|
|
self.server = None
|
2019-05-20 07:18:47 +02:00
|
|
|
self.db_conn = DatabaseConnection(database_dir)
|
|
|
|
|
2019-05-20 08:11:02 +02:00
|
|
|
def ping(self):
|
2019-05-20 07:18:47 +02:00
|
|
|
return 'pong'
|
2019-05-16 01:17:06 +02:00
|
|
|
|
|
|
|
methods = {
|
2019-05-20 07:18:47 +02:00
|
|
|
'ping': ping,
|
|
|
|
'get_claim_comments': None,
|
|
|
|
'get_comment_ids': None,
|
|
|
|
'get_comments_by_id': None,
|
|
|
|
'create_comment': None
|
|
|
|
}
|
|
|
|
|
|
|
|
__db_methods = {
|
|
|
|
'get_claim_comments',
|
|
|
|
'get_comment_ids',
|
|
|
|
'get_comments_by_id',
|
|
|
|
'create_comment'
|
2019-05-16 01:17:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def process_json(self, body) -> dict:
|
|
|
|
response = {'jsonrpc': '2.0', 'id': body['id']}
|
|
|
|
if body['method'] in self.methods:
|
2019-05-20 07:18:47 +02:00
|
|
|
method = body['method']
|
2019-05-16 01:17:06 +02:00
|
|
|
params = body.get('params', {})
|
2019-05-20 08:11:02 +02:00
|
|
|
try:
|
|
|
|
if method in self.__db_methods:
|
|
|
|
result = self.db_conn.__getattribute__(method).__call__(**params)
|
|
|
|
else:
|
|
|
|
result = self.methods[method](self, **params)
|
|
|
|
response['result'] = result
|
|
|
|
except TypeError as te:
|
|
|
|
print(te)
|
|
|
|
response['error'] = ERRORS['INVALID_PARAMS']
|
2019-05-16 01:17:06 +02:00
|
|
|
else:
|
2019-05-16 05:32:29 +02:00
|
|
|
response['error'] = ERRORS['UNKNOWN']
|
2019-05-16 01:17:06 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
async def _start(self):
|
2019-05-20 07:18:47 +02:00
|
|
|
self.db_conn.obtain_connection()
|
2019-05-16 01:17:06 +02:00
|
|
|
self.runner = web.AppRunner(self.app)
|
|
|
|
await self.runner.setup()
|
|
|
|
self.server = web.TCPSite(self.runner, 'localhost', self.port)
|
|
|
|
await self.server.start()
|
|
|
|
|
|
|
|
async def _stop(self):
|
2019-05-20 07:18:47 +02:00
|
|
|
self.db_conn.connection.close()
|
2019-05-16 01:17:06 +02:00
|
|
|
await self.runner.cleanup()
|
|
|
|
|
|
|
|
async def run(self, max_timeout=3600):
|
|
|
|
try:
|
|
|
|
await self._start()
|
|
|
|
await asyncio.sleep(max_timeout)
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
await self._stop()
|
|
|
|
|
|
|
|
async def api(self, request):
|
2019-05-20 08:11:02 +02:00
|
|
|
try:
|
|
|
|
body = await request.json()
|
|
|
|
if type(body) is list or type(body) is dict:
|
|
|
|
if type(body) is list: # batch request
|
|
|
|
response = [self.process_json(part) for part in body]
|
|
|
|
else: # single rpc request
|
|
|
|
response = self.process_json(body)
|
|
|
|
return web.json_response(response)
|
|
|
|
else:
|
|
|
|
return web.json_response({'error': ERRORS['UNKNOWN']})
|
|
|
|
except json.decoder.JSONDecodeError as jde:
|
|
|
|
return web.json_response({
|
|
|
|
'error': {'message': jde.msg, 'code': -1}
|
|
|
|
})
|
|
|
|
|
2019-05-16 01:17:06 +02:00
|
|
|
|
2019-05-20 08:55:02 +02:00
|
|
|
|
2019-05-16 01:17:06 +02:00
|
|
|
if __name__ == '__main__':
|
2019-05-16 05:32:29 +02:00
|
|
|
app = CommentServer()
|
2019-05-16 01:17:06 +02:00
|
|
|
asyncio.run(app.run())
|