-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (59 loc) · 2.21 KB
/
Copy pathmain.py
File metadata and controls
75 lines (59 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
from traceback import format_exc
from aiohttp import ClientSession, TCPConnector
from aioredis import from_url
from sanic import Sanic
from sanic import json as json_response
from sanic.log import logger
from apis.api import bp_api
from apis.home import bp_home
from apis.script import bp_script
from utils import config
from utils.common import fail, success
from utils.db import DB
from utils.log import DEFAULT_LOGGING
from utils.task import check_online
app = Sanic('nodequery', log_config=DEFAULT_LOGGING)
app.config.update_config(config)
app.static('/favicon.ico', 'static/favicon.png')
app.blueprint(bp_api)
app.blueprint(bp_script)
app.blueprint(bp_home)
if config.PROD:
app.add_task(check_online(app))
@app.middleware('response')
def add_cors_headers(request, response):
headers = {
"Access-Control-Allow-Methods": "PUT, GET, POST, DELETE, OPTIONS",
"Access-Control-Allow-Origin": "*",
'Access-Control-Request-Headers': '*',
"Access-Control-Allow-Credentials": "true",
'X-Request-ID': request.id
}
response.headers.update(headers)
@app.exception(Exception)
async def catch_anything(request, exception):
message = exception.message if hasattr(exception, 'message') and exception.message else repr(exception)
code = 500
if hasattr(exception, 'status_code'):
code = exception.status_code
elif hasattr(exception, 'status'):
code = exception.status
logger.exception(exception)
data = fail(message, format_exc(), code)
if code == 777:
code = 200
data = success()
return json_response(data, code)
@app.listener('before_server_start')
async def setup_db(_app: Sanic, loop) -> None:
DB.init_db(loop, 'db0', 'agent')
_app.ctx.redis = await from_url(config.REDIS_URI, decode_responses=True)
_app.ctx.request_session = ClientSession(loop=loop, connector=TCPConnector(ssl=False, loop=loop))
@app.listener('before_server_stop')
async def close_db(_app: Sanic, loop) -> None:
DB.close_db()
await _app.ctx.redis.close()
await _app.ctx.request_session.close()
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(os.getenv('PORT', 8080)), access_log=config.DEV, dev=False, fast=config.PROD)