Skip to content

Commit 48754ca

Browse files
committed
adjustments
1 parent 800e707 commit 48754ca

3 files changed

Lines changed: 132 additions & 91 deletions

File tree

tkp_utils/tornado/__init__.py

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,2 @@
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-
13-
def make_application(
14-
# Server settings
15-
port='8080',
16-
debug=False,
17-
assets_dir=os.path.join(os.path.dirname(__file__), 'assets'),
18-
static_dir=os.path.join(os.path.dirname(__file__), 'assets', 'static'),
19-
cookie_secret='',
20-
# Handler settings
21-
basepath='/',
22-
apipath='/api/v1/',
23-
wspath='ws:0.0.0.0:{port}/',
24-
sqlalchemy_sessionmaker=None,
25-
UserSQLClass=None,
26-
APIKeySQLClass=None,
27-
user_id_field='id',
28-
apikey_id_field='id',
29-
user_apikeys_field='apikeys',
30-
apikey_user_field='user',
31-
user_admin_field='admin',
32-
user_admin_value=True,
33-
# extras
34-
extra_handlers=None,
35-
extra_context=None,
36-
api_revision='v1'):
37-
38-
extra_handlers = extra_handlers or []
39-
extra_context = extra_context or {}
40-
41-
# Port
42-
port = int(os.environ.get('PORT', port))
43-
44-
# Set websocket path
45-
wspath = wspath.format(port)
46-
context = {'sessionmaker': sqlalchemy_sessionmaker,
47-
'basepath': basepath,
48-
'wspath': wspath,
49-
'UserSQLClass': UserSQLClass,
50-
'APIKeySQLClass': APIKeySQLClass,
51-
'user_id_field': user_id_field,
52-
'apikey_id_field': apikey_id_field,
53-
'user_apikeys_field': user_apikeys_field,
54-
'apikey_user_field': apikey_user_field,
55-
'user_admin_field': user_admin_field,
56-
'user_admin_value': user_admin_value}
57-
58-
default_handlers = extra_handlers + [
59-
(r"/", HTMLOpenHandler, {'template': 'index.html', 'context': context}),
60-
(r"/index.html", HTMLOpenHandler, {'template': 'index.html', 'context': context}),
61-
(r"/api/{}/login".format(api_revision), LoginHandler, context),
62-
(r"/api/{}/logout".format(api_revision), LogoutHandler, context),
63-
(r"/api/{}/register".format(api_revision), RegisterHandler, context),
64-
(r"/api/{}/apikeys".format(api_revision), APIKeyHandler, context),
65-
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": static_dir}),
66-
(r"/(.*)", HTMLOpenHandler, {'template': '404.html', 'context': context})
67-
]
68-
69-
for _, handler, handler_kwargs in extra_handlers:
70-
if issubclass(handler, HTMLOpenHandler):
71-
if 'context' in handler_kwargs:
72-
handler_kwargs['context'].update(context)
73-
else:
74-
handler_kwargs['context'] = context
75-
76-
elif issubclass(handler, ServerHandler):
77-
handler_kwargs.update(context)
78-
79-
settings = {
80-
"cookie_secret": cookie_secret,
81-
"login_url": basepath + "login",
82-
"debug": debug,
83-
"template_path": os.path.join(assets_dir, 'templates'),
84-
}
85-
86-
application = tornado.web.Application(default_handlers, **settings)
87-
88-
logging.critical('LISTENING: %d', port)
89-
application.listen(port)
90-
return application
1+
from .application import make_application
2+
from .handlers import ServerHandler, HTMLOpenHandler, LoginHandler, LogoutHandler, RegisterHandler, APIKeyHandler # noqa: F401

tkp_utils/tornado/application.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

tkp_utils/tornado/handlers/html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def get_kwargs(handler, template_kwargs):
1313

1414
class HTMLOpenHandler(ServerHandler):
1515
def initialize(self, context=None, template=None, template_kwargs=None, **kwargs):
16-
super(HTMLOpenHandler, self).initialize(template=template, **context)
16+
super(HTMLOpenHandler, self).initialize(template=template, **(context or {}))
1717
self.template_kwargs = template_kwargs or {}
1818

1919
def get(self, *args):

0 commit comments

Comments
 (0)