|
| 1 | +import os |
| 2 | +import tornado.ioloop |
| 3 | +import tornado.httpserver |
| 4 | +from tornado.web import RequestHandler |
| 5 | +from tornado.gen import coroutine |
| 6 | +from react.render import render_component |
| 7 | + |
| 8 | + |
| 9 | +comments = [] |
| 10 | + |
| 11 | +class IndexHandler(RequestHandler): |
| 12 | + @coroutine |
| 13 | + def get(self): |
| 14 | + rendered = render_component( |
| 15 | + os.path.join(os.getcwd(), 'static', 'js', 'CommentBox.jsx'), |
| 16 | + { |
| 17 | + 'comments': comments, |
| 18 | + 'url': '/comments', |
| 19 | + 'xsrf':self.xsrf_token |
| 20 | + }, |
| 21 | + to_static_markup=False, |
| 22 | + ) |
| 23 | + self.render('index.html', rendered=rendered) |
| 24 | + |
| 25 | + |
| 26 | +class CommentHandler(RequestHandler): |
| 27 | + @coroutine |
| 28 | + def post(self): |
| 29 | + comments.append({ |
| 30 | + 'author': self.get_argument('author'), |
| 31 | + 'text': self.get_argument('text'), |
| 32 | + }) |
| 33 | + self.redirect('/') |
| 34 | + |
| 35 | + |
| 36 | +urls = [ |
| 37 | + (r"/", IndexHandler), |
| 38 | + (r"/comments", CommentHandler), |
| 39 | + (r"/(.*)", tornado.web.StaticFileHandler, {"path":r"{0}".format(os.path.join(os.path.dirname(__file__),"static"))}), |
| 40 | +] |
| 41 | + |
| 42 | +settings = dict({ |
| 43 | + "template_path": os.path.join(os.path.dirname(__file__),"templates"), |
| 44 | + "static_path": os.path.join(os.path.dirname(__file__),"static"), |
| 45 | + "cookie_secret": os.urandom(12), |
| 46 | + "xsrf_cookies": True, |
| 47 | + "debug": True, |
| 48 | + "compress_response": True |
| 49 | +}) |
| 50 | + |
| 51 | +application = tornado.web.Application(urls,**settings) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + server = tornado.httpserver.HTTPServer(application) |
| 56 | + server.listen(8000) |
| 57 | + tornado.ioloop.IOLoop.instance().start() |
| 58 | + |
0 commit comments