forked from ireaderlab/zkdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
executable file
·128 lines (109 loc) · 2.78 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (c) 2014,掌阅科技
All rights reserved.
摘 要: init.py
创 建 者: WangLichao
创建日期: 2014-10-13
'''
import os,sys
# tornado
import tornado.httpserver
import tornado.ioloop
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,
'login_url': '/login',
}
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 daemonize():
stdin = '/dev/null'
stderr = '/dev/null'
stdout = '/dev/null'
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir(os.path.split(os.path.realpath(__file__))[0])
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(stdin, 'r')
so = file(stdout, 'a+')
se = file(stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def make_clean():
'''清理pyc文件
'''
pyshell.shell("find . -name '*.pyc' | xargs rm -rf", debug=True)
def main():
"""主程序入口
"""
# 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,
)
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()