server.py 2.56 KB
import configparser
import sys

import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.options import options, define
from settings import ENV_VARS
from services.depts import SERVICES
from services.ucs_service.ucs_client import UCSHttpClient
import routes
import os.path

from utils import init_log


DEFAULT_CONFIG = 'local.ini'


def make_app():
    return tornado.web.Application(
        routes.url_map,
        login_url=options.login_url,
        cookie_secret=options.cookie_secret,
        users_secret=options.users_secret,
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        xsrf_cookies=True,
        debug=options.debug
    )


if __name__ == '__main__':
    '''
    python3.9 server.py debug=1 port=8888 config=local.ini
    '''

    log = init_log(only_stream=True)

    kwargs = dict(arg.split('=') for arg in sys.argv[1:])
    configfile_path = kwargs.get('config')
    port_arg = kwargs.get('port', '8888')
    debug_arg = bool(int(kwargs.get('debug', '0')))

    config = configparser.ConfigParser()
    config.read(configfile_path)

    LOCAL_CLIENT_DEBUG = config['deploy'].getboolean('local_client_debug')

    if LOCAL_CLIENT_DEBUG:
        from migrations_runner import run_migrations
        log.info('LOCAL DEBUG running migrations')
        run_migrations(only_model_changers=True)
    else:
        log.info('PRODUCTION REGIME')
        log.info('If you try to run locally, make sure you pass proper config argument')

    ucs_http_address = config['ucs']['ucs_http_address']

    SERVICES.ucs_http_client = UCSHttpClient(ucs_http_address)

    define("login_url", default="/login", type=str)
    define("users_secret", default="22G9B8TT70A0WH0MHWNXCU141CLL9QDVWBYLZPO7QGRRN79F5I5B3G55T132J3SU", type=str)
    define("cookie_secret", default="WGS3U4AGHCJII3CMS58RBCZ84P79NU6C9L954JIFMB85VZCR30W9IRMHPDNZXYMD", type=str)
    define("port", default=port_arg, help="run on the given port", type=int)
    define("debug", default=debug_arg, help="run in debug mode", type=int)
    define("config_file_path", default=configfile_path, help="application configfile path", type=str)
    tornado.options.parse_command_line()

    application = make_app()
    SERVICES.tornado_app = application
    http_server = tornado.httpserver.HTTPServer(application, max_body_size=1024 * 1024 * 1024)
    http_server.listen(options.port)

    log.info(f'blocks-admin server is listening at port:{options.port}')
    log.info(f'{ENV_VARS}')

    tornado.ioloop.IOLoop.current().start()