-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
63 lines (45 loc) · 1.42 KB
/
server.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
from json import dumps as json_dumps
from json import loads as json_loads
from os import path
from tornado import gen
from tornado import ioloop
from tornado import web
from tornado.httpclient import AsyncHTTPClient
http_client = AsyncHTTPClient()
SSR_URL = "http://localhost:8081/render"
@gen.coroutine
def ssr(path, page_data):
"""Post to an external Node service to render React components to HTML."""
if isinstance(page_data, basestring):
page_data = json_loads(page_data)
body = json_dumps(dict(
page_data=page_data,
path=path,
))
try:
response = yield http_client.fetch(SSR_URL,
headers={"content-type": "application/json"},
request_timeout=0.5,
method="POST",
body=body)
except Exception as err:
"""The fetch is intentionally fail-soft."""
raise gen.Return(None)
raise gen.Return(response.body)
class HelloHandler(web.RequestHandler):
@gen.coroutine
@web.removeslash
def get(self):
page_data = dict(
message="Hello world"
)
app_html = yield ssr("/hello", page_data)
self.render("hello.html", app_html=app_html, page_data=json_dumps(page_data))
application = web.Application([
(r"/hello", HelloHandler),
],
static_path=path.join(path.dirname(__file__), "static")
)
if __name__ == "__main__":
application.listen(8080)
ioloop.IOLoop.current().start()