-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
executable file
·144 lines (121 loc) · 5.21 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
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
#!/usr/bin/env python
#
# python
import types
import re
import os.path
import logging
from mongokit import Connection, Document as mongokit_Document
# tornado
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
# app
import settings
from utils.routes import route
from utils.git import get_git_revision
################################################################################
define("debug", default=False, help="run in debug mode", type=bool)
define("port", default=8000, help="run on the given port", type=int)
define("database_name", default=settings.DEFAULT_DATABASE_NAME, help="database name")
define("prefork", default=False, help="pre-fork across all CPUs", type=bool)
define("showurls", default=False, help="Show all routed URLs", type=bool)
define("dont_combine", default=False, help="Don't combine static resources", type=bool)
class Application(tornado.web.Application):
def __init__(self,
database_name=None,
xsrf_cookies=True,
optimize_static_content=None):
ui_modules_map = {}
for app_name in settings.APPS:
_ui_modules = __import__('apps.%s' % app_name, globals(), locals(),
['ui_modules'], -1)
try:
ui_modules = _ui_modules.ui_modules
except AttributeError:
# this app simply doesn't have a ui_modules.py file
continue
for name in [x for x in dir(ui_modules) if re.findall('[A-Z]\w+', x)]:
thing = getattr(ui_modules, name)
try:
if issubclass(thing, tornado.web.UIModule):
ui_modules_map[name] = thing
except TypeError:
# most likely a builtin class or something
pass
try:
cdn_prefix = [x.strip() for x in file('cdn_prefix.conf')
if x.strip() and not x.strip().startswith('#')][0]
#logging.info("Using %r as static URL prefix" % cdn_prefix)
except (IOError, IndexError):
cdn_prefix = None
# unless explicitly set, then if in debug mode, disable optimization
# of static content
if optimize_static_content is None:
optimize_static_content = not options.debug
handlers = route.get_routes()
app_settings = dict(
title=settings.TITLE,
template_path=os.path.join(os.path.dirname(__file__), "apps", "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
ui_modules=ui_modules_map,
xsrf_cookies=xsrf_cookies,
cookie_secret=settings.COOKIE_SECRET,
login_url=settings.LOGIN_URL,
debug=options.debug,
optimize_static_content=optimize_static_content,
git_revision=get_git_revision(),
email_backend=options.debug and \
'utils.send_mail.backends.console.EmailBackend' \
or 'utils.send_mail.backends.smtp.EmailBackend',
webmaster=settings.WEBMASTER,
admin_emails=settings.ADMIN_EMAILS,
CLOSURE_LOCATION=os.path.join(os.path.dirname(__file__),
"static", "compiler.jar"),
YUI_LOCATION=os.path.join(os.path.dirname(__file__),
"static", "yuicompressor-2.4.2.jar"),
cdn_prefix=cdn_prefix,
)
tornado.web.Application.__init__(self, handlers, **app_settings)
# Have one global connection to the blog DB across all handlers
self.database_name = database_name and database_name or options.database_name
self.con = Connection() # XXX: needs more options for host/username/password
model_classes = []
for app_name in settings.APPS:
_models = __import__('apps.%s' % app_name, globals(), locals(),
['models'], -1)
try:
models = _models.models
except AttributeError:
# this app simply doesn't have a models.py file
continue
for name in [x for x in dir(models) if re.findall('[A-Z]\w+', x)]:
thing = getattr(models, name)
if issubclass(thing, mongokit_Document):
model_classes.append(thing)
self.con.register(model_classes)
for app_name in settings.APPS:
__import__('apps.%s' % app_name, globals(), locals(), ['handlers'], -1)
def main(): # pragma: no cover
tornado.options.parse_command_line()
if options.showurls:
for each in route.get_routes():
print each._path.ljust(60),
print each.handler_class.__name__
return
http_server = tornado.httpserver.HTTPServer(Application())
print "Starting tornado on port", options.port
if options.prefork:
print "\tpre-forking"
http_server.bind(options.port)
http_server.start()
else:
http_server.listen(options.port)
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()