-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·68 lines (55 loc) · 2.27 KB
/
main.py
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
#!/usr/bin/python3
# Click library has some problems with python3 when it comes to unicode: http://click.pocoo.org/5/python3/#python3-surrogates
# to fix this we need to set the environ variables to export the locales
import os
os.environ['LC_ALL'] = 'C.UTF-8'
os.environ['LANG'] = 'C.UTF-8'
import click
import logging
from js9 import j
from JumpScale9AYS.ays.server.app import app as sanic_app
def configure_logger(level):
if level == 'DEBUG':
click.echo("debug logging enabled")
# configure jumpscale loggers
j.logger.set_level(level)
# configure asyncio logger
asyncio_logger = logging.getLogger('asyncio')
asyncio_logger.handlers = []
asyncio_logger.addHandler(j.logger.handlers.consoleHandler)
asyncio_logger.addHandler(j.logger.handlers.fileRotateHandler)
asyncio_logger.setLevel(level)
@click.command()
@click.option('--host', '-h', default='127.0.0.1', help='listening address')
@click.option('--port', '-p', default=5000, help='listening port')
@click.option('--log', '-l', default='info', help='set logging level (error, warning, info, debug)')
@click.option('--dev', default=False, is_flag=True, help='enable development mode')
def main(host, port, log, dev):
if not j.core.db:
j.clients.redis.start4core()
j.core.db = j.clients.redis.get4core()
log = log.upper()
if log not in ('ERROR', 'WARNING', 'INFO', 'DEBUG'):
click.echo("logging level not valid", err=True)
return
configure_logger(log)
debug = log == 'DEBUG'
# load the app
@sanic_app.listener('before_server_start')
async def init_ays(sanic, loop):
loop.set_debug(debug)
j.atyourservice.server.debug = debug
j.atyourservice.server.dev_mode = dev
if j.atyourservice.server.dev_mode:
j.atyourservice.server.logger.info("development mode enabled")
j.atyourservice.server._start(loop=loop)
@sanic_app.listener('after_start')
async def after_start(sanic, loop):
print("AYS server running at http://{}:{}".format(host, port))
@sanic_app.listener('after_stop')
async def stop_ays(sanic, loop):
await j.atyourservice.server._stop()
# start server
sanic_app.run(debug=debug, host=host, port=port, workers=1)
if __name__ == '__main__':
main()