|
| 1 | +import os.path |
| 2 | +import logging |
| 3 | +import tornado.ioloop |
| 4 | +import tornado.web |
| 5 | +from .handlers import ServerHandler, \ |
| 6 | + HTMLOpenHandler, \ |
| 7 | + LoginHandler, \ |
| 8 | + LogoutHandler, \ |
| 9 | + RegisterHandler, \ |
| 10 | + APIKeyHandler |
| 11 | + |
| 12 | +DEFAULT_API_REVISION = 'v1' |
| 13 | +DEFAULT_BASEPATH = '/' |
| 14 | +DEFAULT_API_PATH = '/api/{}' |
| 15 | + |
| 16 | + |
| 17 | +class TornadoApplication(object): |
| 18 | + def __init__(self): |
| 19 | + # Server settings |
| 20 | + self._port = '8080' |
| 21 | + self._debug = False |
| 22 | + self._assets_dir = os.path.join(os.path.dirname(__file__), 'assets') |
| 23 | + self._static_dir = os.path.join(os.path.dirname(__file__), 'assets', 'static') |
| 24 | + self._cookie_secret = '' |
| 25 | + # Handler settings |
| 26 | + self._basepath = '/' |
| 27 | + self._apipath = '/api/v1/' |
| 28 | + self._wspath = 'ws:0.0.0.0:{port}/' |
| 29 | + self._sqlalchemy_sessionmaker = None |
| 30 | + self._UserSQLClass = None |
| 31 | + self._APIKeySQLClass = None |
| 32 | + self._user_id_field = 'id' |
| 33 | + self._apikey_id_field = 'id' |
| 34 | + self._user_apikeys_field = 'apikeys' |
| 35 | + self._apikey_user_field = 'user' |
| 36 | + self._user_admin_field = 'admin' |
| 37 | + self._user_admin_value = True |
| 38 | + # extra |
| 39 | + self._extra_handlers = None |
| 40 | + self._extra_context = None |
| 41 | + self._api_revision = 'v1' |
| 42 | + |
| 43 | + @property |
| 44 | + def port(self): return self._port |
| 45 | + |
| 46 | + @port.setter |
| 47 | + def port(self, value): |
| 48 | + # update wspath if applicable |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +def make_application( |
| 53 | + # Server settings |
| 54 | + port='8080', |
| 55 | + debug=False, |
| 56 | + assets_dir=os.path.join(os.path.dirname(__file__), 'assets'), |
| 57 | + static_dir=os.path.join(os.path.dirname(__file__), 'assets', 'static'), |
| 58 | + cookie_secret='', |
| 59 | + # Handler settings |
| 60 | + basepath='/', |
| 61 | + apipath='/api/v1/', |
| 62 | + wspath='ws:0.0.0.0:{port}/', |
| 63 | + sqlalchemy_sessionmaker=None, |
| 64 | + UserSQLClass=None, |
| 65 | + APIKeySQLClass=None, |
| 66 | + user_id_field='id', |
| 67 | + apikey_id_field='id', |
| 68 | + user_apikeys_field='apikeys', |
| 69 | + apikey_user_field='user', |
| 70 | + user_admin_field='admin', |
| 71 | + user_admin_value=True, |
| 72 | + # extras |
| 73 | + extra_handlers=None, |
| 74 | + extra_context=None, |
| 75 | + api_revision='v1'): |
| 76 | + |
| 77 | + extra_handlers = extra_handlers or [] |
| 78 | + extra_context = extra_context or {} |
| 79 | + |
| 80 | + # Port |
| 81 | + port = int(os.environ.get('PORT', port)) |
| 82 | + |
| 83 | + # Set websocket path |
| 84 | + wspath = wspath.format(port) |
| 85 | + context = {'sessionmaker': sqlalchemy_sessionmaker, |
| 86 | + 'basepath': basepath, |
| 87 | + 'wspath': wspath, |
| 88 | + 'UserSQLClass': UserSQLClass, |
| 89 | + 'APIKeySQLClass': APIKeySQLClass, |
| 90 | + 'user_id_field': user_id_field, |
| 91 | + 'apikey_id_field': apikey_id_field, |
| 92 | + 'user_apikeys_field': user_apikeys_field, |
| 93 | + 'apikey_user_field': apikey_user_field, |
| 94 | + 'user_admin_field': user_admin_field, |
| 95 | + 'user_admin_value': user_admin_value} |
| 96 | + |
| 97 | + default_handlers = extra_handlers + [ |
| 98 | + (r"/", HTMLOpenHandler, {'template': 'index.html', 'context': context}), |
| 99 | + (r"/index.html", HTMLOpenHandler, {'template': 'index.html', 'context': context}), |
| 100 | + (r"/api/{}/login".format(api_revision), LoginHandler, context), |
| 101 | + (r"/api/{}/logout".format(api_revision), LogoutHandler, context), |
| 102 | + (r"/api/{}/register".format(api_revision), RegisterHandler, context), |
| 103 | + (r"/api/{}/apikeys".format(api_revision), APIKeyHandler, context), |
| 104 | + (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": static_dir}), |
| 105 | + (r"/(.*)", HTMLOpenHandler, {'template': '404.html', 'context': context}) |
| 106 | + ] |
| 107 | + |
| 108 | + for _, handler, handler_kwargs in extra_handlers: |
| 109 | + if issubclass(handler, HTMLOpenHandler): |
| 110 | + if 'context' in handler_kwargs: |
| 111 | + handler_kwargs['context'].update(context) |
| 112 | + else: |
| 113 | + handler_kwargs['context'] = context |
| 114 | + |
| 115 | + elif issubclass(handler, ServerHandler): |
| 116 | + handler_kwargs.update(context) |
| 117 | + |
| 118 | + settings = { |
| 119 | + "cookie_secret": cookie_secret, |
| 120 | + "login_url": basepath + "login", |
| 121 | + "debug": debug, |
| 122 | + "template_path": os.path.join(assets_dir, 'templates'), |
| 123 | + } |
| 124 | + |
| 125 | + application = tornado.web.Application(default_handlers, **settings) |
| 126 | + |
| 127 | + logging.critical('LISTENING: %d', port) |
| 128 | + application.listen(port) |
| 129 | + return application |
0 commit comments