forked from markfinger/python-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (46 loc) · 1.46 KB
/
Copy pathapp.py
File metadata and controls
58 lines (46 loc) · 1.46 KB
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
import os
import tornado.ioloop
import tornado.httpserver
from tornado.web import RequestHandler
from tornado.gen import coroutine
from react.render import render_component
comments = []
class IndexHandler(RequestHandler):
@coroutine
def get(self):
rendered = render_component(
os.path.join(os.getcwd(), 'static', 'js', 'CommentBox.jsx'),
{
'comments': comments,
'url': '/comments',
'xsrf':self.xsrf_token
},
to_static_markup=False,
)
self.render('index.html', rendered=rendered)
class CommentHandler(RequestHandler):
@coroutine
def post(self):
comments.append({
'author': self.get_argument('author'),
'text': self.get_argument('text'),
})
self.redirect('/')
urls = [
(r"/", IndexHandler),
(r"/comments", CommentHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path":r"{0}".format(os.path.join(os.path.dirname(__file__),"static"))}),
]
settings = dict({
"template_path": os.path.join(os.path.dirname(__file__),"templates"),
"static_path": os.path.join(os.path.dirname(__file__),"static"),
"cookie_secret": os.urandom(12),
"xsrf_cookies": True,
"debug": True,
"compress_response": True
})
application = tornado.web.Application(urls,**settings)
if __name__ == "__main__":
server = tornado.httpserver.HTTPServer(application)
server.listen(8000)
tornado.ioloop.IOLoop.instance().start()