forked from ireaderlab/zkdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
executable file
·86 lines (74 loc) · 2.19 KB
/
init.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (c) 2014,掌阅科技
All rights reserved.
摘 要: init.py
创 建 者: WangLichao
创建日期: 2014-10-13
'''
import os
# tornado
import tornado.httpserver
import tornado.ioloop
# lib
from lib import load
from lib import uimodule, uimethods
from lib.utils import logger
from lib.utils import pyshell
# conf
from conf import log
from conf.settings import (
LOG_ITEMS,
OPTIONS,
)
class Application(tornado.web.Application):
"""应用程序启动初始化
"""
def __init__(self):
routes = load('handler')
settings = {
'static_path': os.path.join(os.path.dirname(__file__), "static"),
'template_path': os.path.join(os.path.dirname(__file__), "tpl"),
'xsrf_cookies': True,
'cookie_secret': 'tokyo',
'site_title': 'zkdash',
'ui_modules': uimodule,
'ui_methods': uimethods,
'debug': OPTIONS.debug,
}
tornado.web.Application.__init__(self, routes, **settings)
def log_request(self, handler):
"""重写tornado request日志
"""
status = handler.get_status()
if status < 400:
if handler.request.uri[0:7] == '/static':
return
log_method = log.info
elif status < 500:
log_method = log.warning
else:
log_method = log.error
request_time = 1000.0 * handler.request.request_time()
if request_time > 30 or OPTIONS.debug or status >= 400:
log_method("%d %s %.2fms",
status,
handler._request_summary(),
request_time)
def make_clean():
'''清理pyc文件
'''
pyshell.shell("find . -name '*.pyc' | xargs rm -rf", debug=True)
def main():
"""主程序入口
"""
logger.init_logger(LOG_ITEMS, suffix=OPTIONS.port)
application = Application()
http_server = tornado.httpserver.HTTPServer(application,
xheaders=True)
http_server.listen(OPTIONS.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
make_clean()
main()