forked from urbn/CaesiumQuickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (57 loc) · 2.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
import tornado.ioloop
import tornado.web
from tornado.options import options
import tornado.httpserver
from caesium.handler import BaseRestfulMotorHandler
from caesium.document import BaseAsyncMotorDocument, AsyncRevisionStackManager
from settings import settings
import logging
class CommentHandler(BaseRestfulMotorHandler):
"""
An example handler for a Comment resource
"""
#JSON Schema definition is used for validation of post/put bodies
SCHEMA = {
"title":"Example Comment Schema",
"type": "object",
"required": ["title", "username", "text"],
"properties" : {
"title": {
"type": "string",
},
"username": {
"type": "string",
},
"text": {
"type": "string",
}
}
}
def initialize(self):
super(self.__class__, self).initialize()
self.object_name = "comment"
self.client = BaseAsyncMotorDocument(self.object_name, self.settings, schema=self.SCHEMA)
url_patterns = [
(r"/comment", CommentHandler),
(r"/comment/([0-9a-zA-Z]+)", CommentHandler),
]
class App(tornado.web.Application):
def __init__(self):
"""App wrapper constructor, global objects within our Tornado platform should be managed here."""
self.logger = logging.getLogger(self.__class__.__name__)
tornado.web.Application.__init__(self, url_patterns, **settings)
#Document publisher, this allows for patches to be applied
document_publisher = tornado.ioloop.PeriodicCallback(AsyncRevisionStackManager(settings).publish,
settings['scheduler']["timeout_in_milliseconds"],
io_loop=tornado.ioloop.IOLoop.current()
)
document_publisher.start()
application = App()
if __name__ == "__main__":
logger = logging.getLogger()
http_server = tornado.httpserver.HTTPServer(application, xheaders=True)
http_server.listen(options.port)
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
logger.info("\nStopping server on port %s" % options.port)