/
handlers.py
71 lines (48 loc) · 1.92 KB
/
handlers.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 os
from tornado import web
from urllib.parse import urlunparse, urlparse
from notebook.base.handlers import IPythonHandler
from nbserverproxy.handlers import SuperviseAndProxyHandler
here = os.path.dirname(os.path.abspath(__file__))
class AddSlashHandler(IPythonHandler):
"""Handler for adding trailing slash to URLs that need them"""
@web.authenticated
def get(self, *args):
src = urlparse(self.request.uri)
dest = src._replace(path=src.path + "/")
self.redirect(urlunparse(dest))
def _find_stencila_js(name):
"""Find a stencila.js file in the bundled stencila npm package"""
return os.path.join(here, "node_modules", "nbstencilaproxy", name)
# define our proxy handler for proxying the application
class StencilaProxyHandler(SuperviseAndProxyHandler):
name = "stencila"
def get_env(self):
return {"STENCILA_PORT": str(self.port), "BASE_URL": self.state["base_url"]}
def get_cmd(self):
return ["node", _find_stencila_js("stencila.js")]
class StencilaHostProxyHandler(SuperviseAndProxyHandler):
name = "stencila-host"
def get_env(self):
return {"STENCILA_HOST_PORT": str(self.port)}
def get_cmd(self):
return ["node", _find_stencila_js("stencila-host.js")]
def setup_handlers(app):
app.log.info("Enabling stencila proxy")
app.web_app.add_handlers(
".*",
[
(
app.base_url + "stencila/(.*)",
StencilaProxyHandler,
dict(state=dict(base_url=app.base_url, notebook_dir=app.notebook_dir)),
),
(
app.base_url + "stencila-host/(.*)",
StencilaHostProxyHandler,
dict(state=dict(base_url=app.base_url, notebook_dir=app.notebook_dir)),
),
(app.base_url + "stencila", AddSlashHandler),
(app.base_url + "stencila-host", AddSlashHandler),
],
)