Adds database code to server
This commit is contained in:
parent
9f28ec04b4
commit
7b63b97c5c
1 changed files with 31 additions and 11 deletions
|
@ -1,7 +1,8 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
import server.database as db
|
from server.database import DatabaseConnection
|
||||||
|
from server.conf import database_dir
|
||||||
|
|
||||||
ERRORS = {
|
ERRORS = {
|
||||||
'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'},
|
'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'},
|
||||||
|
@ -17,34 +18,53 @@ class CommentServer:
|
||||||
self.app.add_routes([web.post('/api', self.api)])
|
self.app.add_routes([web.post('/api', self.api)])
|
||||||
self.runner = None
|
self.runner = None
|
||||||
self.server = None
|
self.server = None
|
||||||
|
self.db_conn = DatabaseConnection(database_dir)
|
||||||
|
|
||||||
|
def ping(cls):
|
||||||
|
return 'pong'
|
||||||
|
|
||||||
methods = {
|
methods = {
|
||||||
'get_claim_comments': db.get_claim_comments,
|
'ping': ping,
|
||||||
'get_comment_ids': db.get_comment_ids,
|
'get_claim_comments': None,
|
||||||
'get_comments_by_id': db.get_comments_by_id,
|
'get_comment_ids': None,
|
||||||
'create_comment': db.create_comment,
|
'get_comments_by_id': None,
|
||||||
|
'create_comment': None
|
||||||
|
}
|
||||||
|
|
||||||
|
__methods = {
|
||||||
|
'ping'
|
||||||
|
}
|
||||||
|
|
||||||
|
__db_methods = {
|
||||||
|
'get_claim_comments',
|
||||||
|
'get_comment_ids',
|
||||||
|
'get_comments_by_id',
|
||||||
|
'create_comment'
|
||||||
}
|
}
|
||||||
|
|
||||||
def process_json(self, body) -> dict:
|
def process_json(self, body) -> dict:
|
||||||
response = {'jsonrpc': '2.0', 'id': body['id']}
|
response = {'jsonrpc': '2.0', 'id': body['id']}
|
||||||
if body['method'] in self.methods:
|
if body['method'] in self.methods:
|
||||||
|
method = body['method']
|
||||||
params = body.get('params', {})
|
params = body.get('params', {})
|
||||||
result = self.methods[body['method']](self, **params)
|
if method in self.__db_methods:
|
||||||
if type(result) is dict and 'error' in result:
|
result = self.db_conn.__getattribute__(method).__call__(**params)
|
||||||
response['error'] = result['error']
|
|
||||||
else:
|
else:
|
||||||
|
result = self.__methods[method](self, **params)
|
||||||
response['result'] = result
|
response['result'] = result
|
||||||
else:
|
else:
|
||||||
response['error'] = ERRORS['UNKNOWN']
|
response['error'] = ERRORS['UNKNOWN']
|
||||||
return response
|
return response
|
||||||
|
|
||||||
async def _start(self):
|
async def _start(self):
|
||||||
|
self.db_conn.obtain_connection()
|
||||||
self.runner = web.AppRunner(self.app)
|
self.runner = web.AppRunner(self.app)
|
||||||
await self.runner.setup()
|
await self.runner.setup()
|
||||||
self.server = web.TCPSite(self.runner, 'localhost', self.port)
|
self.server = web.TCPSite(self.runner, 'localhost', self.port)
|
||||||
await self.server.start()
|
await self.server.start()
|
||||||
|
|
||||||
async def _stop(self):
|
async def _stop(self):
|
||||||
|
self.db_conn.connection.close()
|
||||||
await self.runner.cleanup()
|
await self.runner.cleanup()
|
||||||
|
|
||||||
async def run(self, max_timeout=3600):
|
async def run(self, max_timeout=3600):
|
||||||
|
@ -59,9 +79,9 @@ class CommentServer:
|
||||||
async def api(self, request):
|
async def api(self, request):
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
if type(body) is list or type(body) is dict:
|
if type(body) is list or type(body) is dict:
|
||||||
if type(body) is list:
|
if type(body) is list: # batch request
|
||||||
response = [self.process_json(part) for part in body]
|
response = [self.process_json(part) for part in body]
|
||||||
else:
|
else: # single rpc request
|
||||||
response = self.process_json(body)
|
response = self.process_json(body)
|
||||||
return web.json_response(response)
|
return web.json_response(response)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue