-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibt2.py
executable file
·749 lines (653 loc) · 28.6 KB
/
ibt2.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""I'll Be There, 2 (ibt2) - an oversimplified attendees registration system.
Copyright 2016-2019 Davide Alberani <[email protected]>
RaspiBO <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import re
import time
import logging
import datetime
from operator import itemgetter
import itertools
import tornado.httpserver
import tornado.ioloop
import tornado.options
from tornado.options import define, options
import tornado.web
from tornado import gen, escape
import utils
import monco
API_VERSION = '1.1'
class BaseException(Exception):
"""Base class for ibt2 custom exceptions.
:param message: text message
:type message: str
:param status: numeric http status code
:type status: int"""
def __init__(self, message, status=400):
super(BaseException, self).__init__(message)
self.message = message
self.status = status
class InputException(BaseException):
"""Exception raised by errors in input handling."""
pass
class BaseHandler(tornado.web.RequestHandler):
"""Base class for request handlers."""
# Cache currently connected users.
_users_cache = {}
# set of documents we're managing (a collection in MongoDB or a table in a SQL database)
document = None
collection = None
# A property to access the first value of each argument.
arguments = property(lambda self: dict([(k, v[0].decode('utf-8'))
for k, v in self.request.arguments.items()]))
# Arguments suitable for a query on MongoDB.
clean_arguments = property(lambda self: self._clean_dict(self.arguments))
_re_split_salt = re.compile(r'\$(?P<salt>.+)\$(?P<hash>.+)')
@property
def clean_body(self):
"""Return a clean dictionary from a JSON body, suitable for a query on MongoDB.
:returns: a clean copy of the body arguments
:rtype: dict"""
data = escape.json_decode(self.request.body or '{}')
return self._clean_dict(data)
def _clean_dict(self, data):
"""Filter a dictionary (in place) to remove unwanted keywords in db queries.
:param data: dictionary to clean
:type data: dict"""
if isinstance(data, dict):
for key in list(data.keys()):
if (isinstance(key, str) and key.startswith('$')) or key in ('_id', 'created_by', 'created_at',
'updated_by', 'updated_at', 'isRegistered'):
del data[key]
return data
def write_error(self, status_code, **kwargs):
"""Default error handler."""
if isinstance(kwargs.get('exc_info', (None, None))[1], BaseException):
exc = kwargs['exc_info'][1]
status_code = exc.status
message = exc.message
else:
message = 'internal error'
self.build_error(message, status=status_code)
def is_api(self):
"""Return True if the path is from an API call."""
return self.request.path.startswith('/v%s' % API_VERSION)
def initialize(self, **kwargs):
"""Add every passed (key, value) as attributes of the instance."""
for key, value in kwargs.items():
setattr(self, key, value)
@property
def current_user(self):
"""Retrieve current user ID from the secure cookie."""
current_user = self.get_secure_cookie("user")
if isinstance(current_user, bytes):
current_user = current_user.decode('utf-8')
return current_user
@property
def current_user_info(self):
"""Information about the current user.
:returns: full information about the current user
:rtype: dict"""
current_user = self.current_user
if current_user in self._users_cache:
return self._users_cache[current_user]
user_info = {}
if current_user:
user_info['_id'] = current_user
user = self.db.getOne('users', {'_id': user_info['_id']})
if user:
user_info = user
user_info['isRegistered'] = True
self._users_cache[current_user] = user_info
return user_info
def is_registered(self):
"""Check if the current user is registered.
:returns: True if a registered user is logged in
:rtype: bool"""
return self.current_user_info.get('isRegistered')
def is_admin(self):
"""Check if the current user is an admin.
:returns: True if the logged in user is an admin
:rtype: bool"""
return self.current_user_info.get('isAdmin')
def user_authorized(self, username, password):
"""Check if a combination of username/password is valid.
:param username: username or email
:type username: str
:param password: password
:type password: str
:returns: tuple like (bool_user_is_authorized, dict_user_info)
:rtype: dict"""
query = [{'username': username}, {'email': username}]
res = self.db.query('users', query)
if not res:
return (False, {})
user = res[0]
db_password = user.get('password') or ''
if not db_password:
return (False, {})
match = self._re_split_salt.match(db_password)
if not match:
return (False, {})
salt = match.group('salt')
if utils.hash_password(password, salt=salt) == db_password:
return (True, user)
return (False, {})
def build_error(self, message='', status=400):
"""Build and write an error message.
:param message: textual message
:type message: str
:param status: HTTP status code
:type status: int
"""
self.set_status(status)
self.write({'error': True, 'message': message})
def has_permission(self, owner_id):
"""Check if the given owner_id matches with the current user or the logged in user is an admin; if not,
build an error reply.
:param owner_id: owner ID to check against
:type owner_id: str, ObjectId, None
:returns: if the logged in user has the permission
:rtype: bool"""
if (owner_id and str(self.current_user) != str(owner_id) and not
self.is_admin()):
self.build_error(status=401, message='insufficient permissions: must be the owner or admin')
return False
return True
def logout(self):
"""Remove the secure cookie used fro authentication."""
if self.current_user in self._users_cache:
del self._users_cache[self.current_user]
self.clear_cookie("user")
def add_access_info(self, doc):
"""Add created/updated by/at to a document (modified in place and returned).
:param doc: the doc to be updated
:type doc: dict
:returns: the updated document
:rtype: dict"""
user_id = self.current_user
now = datetime.datetime.utcnow()
if 'created_by' not in doc:
doc['created_by'] = user_id
if 'created_at' not in doc:
doc['created_at'] = now
doc['updated_by'] = user_id
doc['updated_at'] = now
return doc
@staticmethod
def update_global_settings(db_connector, settings=None):
"""Update global settings from the db.
:param db_connector: database connector
:type db_connector: Monco instance
:param settings: the dict to update (in place, and returned)
:type settings: dict
:returns: the updated dictionary, also modified in place
:rtype: dict"""
if settings is None:
settings = {}
settings.clear()
for setting in db_connector.query('settings'):
if not ('_id' in setting and 'value' in setting):
continue
settings[setting['_id']] = setting['value']
return settings
class RootHandler(BaseHandler):
"""Handler for the / path."""
app_path = os.path.join(os.path.dirname(__file__), "dist")
@gen.coroutine
def get(self, *args, **kwargs):
# serve the ./app/index.html file
with open(self.app_path + "/index.html", 'r') as fd:
self.write(fd.read())
class AttendeesHandler(BaseHandler):
document = 'attendee'
collection = 'attendees'
@gen.coroutine
def get(self, id_=None, **kwargs):
if id_:
output = self.db.getOne(self.collection, {'_id': id_})
else:
output = {self.collection: self.db.query(self.collection, self.clean_arguments)}
self.write(output)
@gen.coroutine
def post(self, **kwargs):
data = self.clean_body
for key in 'name', 'group', 'day':
value = (data.get(key) or '').strip()
if not value:
return self.build_error(status=404, message="%s can't be empty" % key)
data[key] = value
self.add_access_info(data)
doc = self.db.add(self.collection, data)
self.write(doc)
@gen.coroutine
def put(self, id_, **kwargs):
data = self.clean_body
doc = self.db.getOne(self.collection, {'_id': id_}) or {}
if not doc:
return self.build_error(status=404, message='unable to access the resource')
if not self.has_permission(doc.get('created_by')):
return
if self.global_settings.get('protectUnregistered') and not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be admin')
self.add_access_info(data)
merged, doc = self.db.update(self.collection, {'_id': id_}, data)
self.write(doc)
@gen.coroutine
def delete(self, id_=None, **kwargs):
if id_ is None:
return self.build_error(status=404, message='unable to access the resource')
doc = self.db.getOne(self.collection, {'_id': id_}) or {}
if not doc:
return self.build_error(status=404, message='unable to access the resource')
if not self.has_permission(doc.get('created_by')):
return
if self.global_settings.get('protectUnregistered') and not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be admin')
howMany = self.db.delete(self.collection, id_)
self.write({'success': True, 'deleted entries': howMany.get('n')})
class DaysHandler(BaseHandler):
"""Handle requests for Days."""
def _summarize(self, days):
res = []
for day in days:
res.append({'day': day['day'], 'groups_count': len(day.get('groups', []))})
return res
@gen.coroutine
def get(self, day=None, **kwargs):
params = self.clean_arguments
summary = params.get('summary', False)
if summary:
del params['summary']
start = params.get('start')
if start:
del params['start']
end = params.get('end')
if end:
del params['end']
base = {}
groupsDetails = {}
if day:
params['day'] = day
base = self.db.getOne('days', {'day': day})
groupsDetails = dict([(x['group'], x) for x in self.db.query('groups', {'day': day})])
else:
if start:
params['day'] = {'$gte': start}
if end:
if 'day' not in params:
params['day'] = {}
if end.count('-') == 0:
end += '-13'
elif end.count('-') == 1:
end += '-31'
params['day'].update({'$lte': end})
results = self.db.query('attendees', params)
days = []
dayData = {}
try:
sortedDays = []
for result in results:
if not ('day' in result and 'group' in result and 'name' in result):
self.logger.warn('unable to parse entry; dayData: %s', dayData)
continue
sortedDays.append(result)
sortedDays = sorted(sortedDays, key=itemgetter('day'))
for d, dayItems in itertools.groupby(sortedDays, key=itemgetter('day')):
dayData = {'day': d, 'groups': []}
for group, attendees in itertools.groupby(sorted(dayItems, key=itemgetter('group')),
key=itemgetter('group')):
attendees = sorted(attendees, key=itemgetter('_id'))
groupData = groupsDetails.get(group) or {}
groupData.update({'group': group, 'attendees': attendees})
dayData['groups'].append(groupData)
days.append(dayData)
except Exception as e:
self.logger.warn('unable to parse entry; dayData: %s error: %s', dayData, e)
if summary:
days = self._summarize(days)
if not day:
self.write({'days': days})
elif days:
base.update(days[0])
self.write(base)
else:
self.write(base)
class DaysInfoHandler(BaseHandler):
"""Handle requests for Days info."""
@gen.coroutine
def put(self, day='', **kwargs):
day = day.strip()
if not day:
return self.build_error(status=404, message='unable to access the resource')
data = self.clean_body
if 'notes' in data and self.global_settings.get('protectDayNotes') and not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be admin')
data['day'] = day
self.add_access_info(data)
merged, doc = self.db.update('days', {'day': day}, data)
self.write(doc)
class GroupsHandler(BaseHandler):
"""Handle requests for Groups."""
@gen.coroutine
def put(self, day='', group='', **kwargs):
day = day.strip()
group = group.strip()
if not (day and group):
return self.build_error(status=404, message='unable to access the resource')
data = self.clean_body
newName = (data.get('newName') or '').strip()
if newName:
if self.global_settings.get('protectGroupName') and not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be admin')
query = {'day': day, 'group': group}
data = {'group': newName}
self.db.updateMany('attendees', query, data)
self.db.updateMany('groups', query, data)
self.write({'success': True})
else:
self.write({'success': False})
@gen.coroutine
def delete(self, day='', group='', **kwargs):
day = day.strip()
group = group.strip()
if not (day and group):
return self.build_error(status=404, message='unable to access the resource')
if not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be admin')
query = {'day': day, 'group': group}
howMany = self.db.delete('attendees', query)
self.db.delete('groups', query)
self.write({'success': True, 'deleted entries': howMany.get('n')})
class GroupsInfoHandler(BaseHandler):
"""Handle requests for Groups Info."""
@gen.coroutine
def put(self, day='', group='', **kwargs):
day = day.strip()
group = group.strip()
if not (day and group):
return self.build_error(status=404, message='unable to access the resource')
data = self.clean_body
if 'notes' in data and self.global_settings.get('protectGroupNotes') and not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be admin')
data['day'] = day
data['group'] = group
self.add_access_info(data)
merged, doc = self.db.update('groups', {'day': day, 'group': group}, data)
self.write(doc)
class UsersHandler(BaseHandler):
"""Handle requests for Users."""
document = 'user'
collection = 'users'
@gen.coroutine
def get(self, id_=None, **kwargs):
if id_:
if not self.has_permission(id_):
return
output = self.db.getOne(self.collection, {'_id': id_})
if 'password' in output:
del output['password']
else:
if not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be an admin')
output = {self.collection: self.db.query(self.collection, self.clean_arguments)}
for user in output['users']:
if 'password' in user:
del user['password']
self.write(output)
@gen.coroutine
def post(self, **kwargs):
data = self.clean_body
username = (data.get('username') or '').strip()
password = (data.get('password') or '').strip()
email = (data.get('email') or '').strip()
if not (username and password):
raise InputException('missing username or password')
res = self.db.query('users', {'username': username})
if res:
raise InputException('username already exists')
data['username'] = username
data['password'] = utils.hash_password(password)
data['email'] = email
self.add_access_info(data)
if 'isAdmin' in data and not self.is_admin():
del data['isAdmin']
doc = self.db.add(self.collection, data)
if 'password' in doc:
del doc['password']
self.write(doc)
@gen.coroutine
def put(self, id_=None, **kwargs):
data = self.clean_body
if id_ is None:
return self.build_error(status=404, message='unable to access the resource')
if not self.has_permission(id_):
return
if 'username' in data:
del data['username']
if 'isAdmin' in data and (str(self.current_user) == id_ or not self.is_admin()):
del data['isAdmin']
if 'password' in data:
password = (data['password'] or '').strip()
if password:
data['password'] = utils.hash_password(password)
else:
del data['password']
self.add_access_info(data)
merged, doc = self.db.update(self.collection, {'_id': id_}, data)
if 'password' in doc:
del doc['password']
self.write(doc)
@gen.coroutine
def delete(self, id_=None, **kwargs):
if id_ is None:
return self.build_error(status=404, message='unable to access the resource')
if not self.has_permission(id_):
return self.build_error(status=401, message='insufficient permissions: must be admin')
if id_ == self.current_user:
return self.build_error(status=401, message='unable to delete the current user; ask an admin')
doc = self.db.getOne(self.collection, {'_id': id_})
if not doc:
return self.build_error(status=404, message='unable to access the resource')
if doc.get('username') == 'admin':
return self.build_error(status=401, message='unable to delete the admin user')
howMany = self.db.delete(self.collection, id_)
if id_ in self._users_cache:
del self._users_cache[id_]
self.write({'success': True, 'deleted entries': howMany.get('n')})
class CurrentUserHandler(BaseHandler):
"""Handle requests for information about the logged in user."""
@gen.coroutine
def get(self, **kwargs):
user_info = self.current_user_info or {}
if 'password' in user_info:
del user_info['password']
self.write(user_info)
class SettingsHandler(BaseHandler):
"""Handle global settings."""
collection = 'settings'
def get(self, id_=None):
query = {}
if id_ is not None:
query['_id'] = id_
res = self.db.query(self.collection, query)
res = dict((i.get('_id'), i.get('value')) for i in res if '_id' in i and isinstance(i.get('_id'), str))
if id_ is not None:
res = {id_: res.get(id_)}
self.write(res)
def post(self, id_=None):
if not self.is_admin():
return self.build_error(status=401, message='insufficient permissions: must be an admin')
data = self.clean_body
if id_ is not None:
# if we access a specific resource, we assume the data is in {_id: value} format
if id_ not in data:
return self.build_error(status=404, message='incomplete data')
return
data = {id_: data[id_]}
for key, value in data.items():
if self.db.get(self.collection, key):
self.db.update(self.collection, {'_id': key}, {'value': value})
else:
self.db.add(self.collection, {'_id': key, 'value': value})
settings = SettingsHandler.update_global_settings(self.db, self.global_settings)
self.write(settings)
put = post
class LoginHandler(RootHandler):
"""Handle user authentication requests."""
@gen.coroutine
def get(self, **kwargs):
# show the login page
if self.is_api():
self.set_status(401)
self.write({'error': True,
'message': 'authentication required'})
@gen.coroutine
def post(self, *args, **kwargs):
# authenticate a user
try:
password = self.get_body_argument('password')
username = self.get_body_argument('username')
except tornado.web.MissingArgumentError:
data = self.clean_body
username = data.get('username')
password = data.get('password')
if not (username and password):
self.set_status(401)
self.write({'error': True, 'message': 'missing username or password'})
return
authorized, user = self.user_authorized(username, password)
if authorized and 'username' in user and '_id' in user:
id_ = str(user['_id'])
username = user['username']
logging.info('successful login for user %s (id: %s)' % (username, id_))
self.set_secure_cookie("user", id_)
user_info = self.current_user_info
if 'password' in user_info:
del user_info['password']
self.write(user_info)
return
logging.info('login failed for user %s' % username)
self.set_status(401)
self.write({'error': True, 'message': 'wrong username and password'})
class LogoutHandler(BaseHandler):
"""Handle user logout requests."""
@gen.coroutine
def get(self, **kwargs):
# log the user out
logging.info('logout')
self.logout()
self.write({'error': False, 'message': 'logged out'})
def run():
"""Run the Tornado web application."""
# command line arguments; can also be written in a configuration file,
# specified with the --config argument.
define("port", default=3000, help="run on the given port", type=int)
define("address", default='', help="bind the server at the given address", type=str)
define("ssl_cert", default=os.path.join(os.path.dirname(__file__), 'ssl', 'ibt2_cert.pem'),
help="specify the SSL certificate to use for secure connections")
define("ssl_key", default=os.path.join(os.path.dirname(__file__), 'ssl', 'ibt2_key.pem'),
help="specify the SSL private key to use for secure connections")
define("mongo_url", default=None,
help="URL to MongoDB server", type=str)
define("db_name", default='ibt2',
help="Name of the MongoDB database to use", type=str)
define("debug", default=False, help="run in debug mode")
define("config", help="read configuration file",
callback=lambda path: tornado.options.parse_config_file(path, final=False))
tornado.options.parse_command_line()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if options.debug:
logger.setLevel(logging.DEBUG)
ssl_options = {}
if os.path.isfile(options.ssl_key) and os.path.isfile(options.ssl_cert):
ssl_options = dict(certfile=options.ssl_cert, keyfile=options.ssl_key)
# database backend connector
_count = 0
while True:
try:
db_connector = monco.Monco(url=options.mongo_url, dbName=options.db_name)
break
except Exception:
time.sleep(1)
_count += 1
if _count > 20:
raise
# global settings stored in the db
global_settings = {}
BaseHandler.update_global_settings(db_connector, global_settings)
init_params = dict(db=db_connector, listen_port=options.port, logger=logger,
ssl_options=ssl_options, global_settings=global_settings)
# If not present, we store a user 'admin' with password 'ibt2' into the database.
if not db_connector.query('users', {'username': 'admin'}):
db_connector.add('users',
{'username': 'admin', 'password': utils.hash_password('ibt2'),
'isAdmin': True})
# If present, use the cookie_secret stored into the database.
cookie_secret = db_connector.get('server_settings', 'server_cookie_secret')
if cookie_secret:
cookie_secret = cookie_secret['value']
else:
# the salt guarantees its uniqueness
cookie_secret = utils.hash_password('__COOKIE_SECRET__')
db_connector.add('server_settings',
{'_id': 'server_cookie_secret', 'value': cookie_secret})
_days_path = r"/days/?(?P<day>[\d_-]+)?"
_days_info_path = r"/days/(?P<day>[\d_-]+)/info"
_groups_path = r"/days/(?P<day>[\d_-]+)/groups/(?P<group>.+?)"
_groups_info_path = r"/days/(?P<day>[\d_-]+)/groups/(?P<group>.+?)/info"
_attendees_path = r"/attendees/?(?P<id_>[\w\d_-]+)?"
_current_user_path = r"/users/current/?"
_users_path = r"/users/?(?P<id_>[\w\d_-]+)?/?(?P<resource>[\w\d_-]+)?/?(?P<resource_id>[\w\d_-]+)?"
_settings_path = r"/settings/?(?P<id_>[\w\d_ -]+)?/?"
application = tornado.web.Application([
(_attendees_path, AttendeesHandler, init_params),
(r'/v%s%s' % (API_VERSION, _attendees_path), AttendeesHandler, init_params),
(_groups_info_path, GroupsInfoHandler, init_params),
(r'/v%s%s' % (API_VERSION, _groups_info_path), GroupsInfoHandler, init_params),
(_groups_path, GroupsHandler, init_params),
(r'/v%s%s' % (API_VERSION, _groups_path), GroupsHandler, init_params),
(_days_path, DaysHandler, init_params),
(r'/v%s%s' % (API_VERSION, _days_path), DaysHandler, init_params),
(_days_info_path, DaysInfoHandler, init_params),
(r'/v%s%s' % (API_VERSION, _days_info_path), DaysInfoHandler, init_params),
(_current_user_path, CurrentUserHandler, init_params),
(r'/v%s%s' % (API_VERSION, _current_user_path), CurrentUserHandler, init_params),
(_users_path, UsersHandler, init_params),
(r'/v%s%s' % (API_VERSION, _users_path), UsersHandler, init_params),
(_settings_path, SettingsHandler, init_params),
(r'/v%s%s' % (API_VERSION, _settings_path), SettingsHandler, init_params),
(r"/(?:index.html)?", RootHandler, init_params),
(r'/login', LoginHandler, init_params),
(r'/v%s/login' % API_VERSION, LoginHandler, init_params),
(r'/logout', LogoutHandler),
(r'/v%s/logout' % API_VERSION, LogoutHandler),
(r'/?(.*)', tornado.web.StaticFileHandler, {"path": "dist"})
],
static_path=os.path.join(os.path.dirname(__file__), "dist/static"),
cookie_secret=cookie_secret,
login_url='/login',
debug=options.debug)
http_server = tornado.httpserver.HTTPServer(application, ssl_options=ssl_options or None)
logger.info('Start serving on %s://%s:%d', 'https' if ssl_options else 'http',
options.address if options.address else '127.0.0.1',
options.port)
http_server.listen(options.port, options.address)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
try:
run()
except KeyboardInterrupt:
print('Stop server')