-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog_app.py
104 lines (79 loc) · 3.26 KB
/
blog_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#coding: utf8
import os
from tornado import options, httputil, wsgi
import settings
from app.urls import urls
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.escape
from tornado.util import unicode_type
import logging
from tornado.platform.asyncio import AsyncIOMainLoop
import asyncio
import uvloop
def utf8(value):
"""Converts a string argument to a byte string.
If the argument is already a byte string or None, it is returned unchanged.
Otherwise it must be a unicode string and is encoded as utf8.
"""
if value is None or value == 'None':
value = ""
if isinstance(value, tornado.escape._UTF8_TYPES):
return value
if not isinstance(value, unicode_type):
raise TypeError(
"Expected bytes, unicode, or None; got %r" % type(value)
)
return value.encode("utf-8")
tornado.escape.utf8 = utf8
options.define('port', default=8001, type=int, help=u'系统监听端口,默认:8001')
options.define('admin_port', default=0, type=int, help=u'Admin监听端口,默认:8001')
options.define('debug', default='true', type=str, help=u'是否是调试模式,默认:true')
options.define('tmpl', default='default', type=str, help=u'模板,默认:default')
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
'-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)
def ping_db():
from app.models import Tag
Tag.select().limit(1)
print('*'*10, 'ping db')
def main():
options.parse_command_line()
address, port, admin_port = '0.0.0.0', options.options.port, options.options.admin_port
debug = options.options.debug == 'true'
tmpl = options.options.tmpl
tornado_env = settings.builder_tornado_env(tmpl)
tornado_env['debug'] = debug
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
AsyncIOMainLoop().install()
ioloop = asyncio.get_event_loop()
# ioloop = tornado.ioloop.IOLoop.instance()
# def admin_listen():
# if not admin_port:
# return
# wsgi_app = wsgi.WSGIContainer(WSGIHandler())
# print (tornado_env['static_path'])
# tornado_app = tornado.web.Application([
# (r'/static/admin/(.*)', tornado.web.StaticFileHandler, {"path": os.path.join(BASE_DIR, 'admin', 'static', 'admin')}),
# (r'/static/media/(.*)', tornado.web.StaticFileHandler, {"path": MEDIA_DIR_NAME}),
# (r'/admin/(.*)', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
# ])
# tornado.httpserver.HTTPServer(tornado_app).listen(admin_port)
# print('run admin platform on (%s:%s)' % (address, admin_port))
def app_listen():
application = tornado.web.Application(urls, **tornado_env)
http_server = tornado.httpserver.HTTPServer(application, xheaders=True)
http_server.listen(port, address)
print('server run on (%s:%s),tmpl is "%s"' % (address, port, tmpl))
# admin_listen()
app_listen()
tornado.ioloop.PeriodicCallback(ping_db, int(settings.db_ping_seconds * 1000)).start()
try:
# ioloop.start()
ioloop.run_forever()
except KeyboardInterrupt:
ioloop.stop()
ioloop.close()
if __name__ == '__main__':
main()