-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
84 lines (67 loc) · 2.58 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""HandOut.
The entrance of HandOut.
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from .util.parse_config import parse_config
parse_config()
from . import handler
from .db import SessionGen, System
from .db import version as db_version
import os
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.orm.exc import NoResultFound
from tornado.ioloop import IOLoop
from tornado.web import Application, StaticFileHandler
from tornado.httpserver import HTTPServer
from tornado.options import options
def check_db():
with SessionGen() as session:
try:
db_info = System.by_key('db_version', session).one()
if db_info.value != db_version:
raise ValueError('db version error')
except (ProgrammingError, NoResultFound) as e:
print(e)
print('資料庫未初始化。請先初始化資料庫後再啟動系統。')
exit()
except ValueError as e:
print(e)
print('資料庫版本不符合,請更新或是重新初始化。')
exit()
def make_app():
return Application(
handlers = [
# Index Page
(r'/', handler.IndexHandler),
# (r'/login/?', handler.LoginHandler),
# (r'/logout/?', handler.LogoutHandler),
# (r'/signup/?', handler.SignupHandler),
# Query Presentation
(r'(/json)?/present/?', handler.QueryPresentHandler),
# View Presentation
(r'/present(?:/([0-9]+))?/?', handler.ViewPresentHandler),
# Submit new or exist Presentation
(r'/present/submit(?:/([0-9]+))?/?', handler.SubmitPresentHandler),
# Download ppt
(r'/download/(.*)', handler.FileHandler, {'path': os.path.join(os.path.dirname(__file__), 'file')}),
# Debug
(r'/debug/(.*)', StaticFileHandler, {'path': os.path.join(os.path.dirname(__file__), 'debug')})
],
template_path = os.path.join(os.path.dirname(__file__), 'template'),
static_path = os.path.join(os.path.dirname(__file__), 'static'),
cookie_secret = options.cookie_secret,
login_url = '/login',
xsrf_cookies = True,
debug = options.server_debug,
autoreload = False,
default_handler_class = handler.NotFoundHandler,
)
if __name__ == '__main__':
check_db()
server = HTTPServer(make_app(), xheaders=True)
server.listen(options.port)
IOLoop.current().start()