Skip to content

Commit 4b4dbe2

Browse files
committed
Replace indexpath with mappath: maps request → proxied paths
1 parent 29298fa commit 4b4dbe2

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

docs/server-process.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ pairs.
9090
automatically select an unused port.
9191

9292

93+
#. **mappath**
94+
95+
Map request paths to proxied paths.
96+
Either a dictionary of request paths to proxied paths,
97+
or a callable that takes parameter ``path`` and returns the proxied path.
98+
99+
93100
#. **launcher_entry**
94101

95102
A dictionary with options on if / how an entry in the classic Jupyter Notebook

jupyter_server_proxy/config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections import namedtuple
1010
from .utils import call_with_asked_args
1111

12-
def _make_serverproxy_handler(name, command, environment, timeout, absolute_url, port, indexpage):
12+
def _make_serverproxy_handler(name, command, environment, timeout, absolute_url, port, mappath):
1313
"""
1414
Create a SuperviseAndProxyHandler subclass with given parameters
1515
"""
@@ -21,7 +21,7 @@ def __init__(self, *args, **kwargs):
2121
self.proxy_base = name
2222
self.absolute_url = absolute_url
2323
self.requested_port = port
24-
self.indexpage = indexpage
24+
self.mappath = mappath
2525

2626
@property
2727
def process_args(self):
@@ -83,7 +83,7 @@ def make_handlers(base_url, server_processes):
8383
sp.timeout,
8484
sp.absolute_url,
8585
sp.port,
86-
sp.indexpage,
86+
sp.mappath,
8787
)
8888
handlers.append((
8989
ujoin(base_url, sp.name, r'(.*)'), handler, dict(state={}),
@@ -95,7 +95,7 @@ def make_handlers(base_url, server_processes):
9595

9696
LauncherEntry = namedtuple('LauncherEntry', ['enabled', 'icon_path', 'title'])
9797
ServerProcess = namedtuple('ServerProcess', [
98-
'name', 'command', 'environment', 'timeout', 'absolute_url', 'port', 'indexpage', 'launcher_entry'])
98+
'name', 'command', 'environment', 'timeout', 'absolute_url', 'port', 'mappath', 'launcher_entry'])
9999

100100
def make_server_process(name, server_process_config):
101101
le = server_process_config.get('launcher_entry', {})
@@ -106,7 +106,7 @@ def make_server_process(name, server_process_config):
106106
timeout=server_process_config.get('timeout', 5),
107107
absolute_url=server_process_config.get('absolute_url', False),
108108
port=server_process_config.get('port', 0),
109-
indexpage=server_process_config.get('indexpage', ''),
109+
mappath=server_process_config.get('mappath', ''),
110110
launcher_entry=LauncherEntry(
111111
enabled=le.get('enabled', True),
112112
icon_path=le.get('icon_path'),
@@ -147,10 +147,10 @@ class ServerProxy(Configurable):
147147
port
148148
Set the port that the service will listen on. The default is to automatically select an unused port.
149149
150-
indexpage
151-
If the root of the service is requested return this page instead.
152-
This is often referred to in web-server configurations as the index
153-
page.
150+
mappath
151+
Map request paths to proxied paths.
152+
Either a dictionary of request paths to proxied paths,
153+
or a callable that takes parameter ``path`` and returns the proxied path.
154154
155155
launcher_entry
156156
A dictionary of various options for entries in classic notebook / jupyterlab launchers.

jupyter_server_proxy/handlers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from notebook.utils import url_path_join
1717
from notebook.base.handlers import IPythonHandler, utcnow
1818

19+
from .utils import call_with_asked_args
1920
from .websocket import WebSocketHandlerMixin, pingable_ws_connect
2021
from simpervisor import SupervisedProcess
2122

@@ -340,7 +341,7 @@ class SuperviseAndProxyHandler(LocalProxyHandler):
340341

341342
def __init__(self, *args, **kwargs):
342343
self.requested_port = 0
343-
self.indexpage = ''
344+
self.mappath = {}
344345
super().__init__(*args, **kwargs)
345346

346347
def initialize(self, state):
@@ -436,8 +437,11 @@ async def ensure_process(self):
436437
async def proxy(self, port, path):
437438
if not path.startswith('/'):
438439
path = '/' + path
439-
if self.indexpage and (path == '/' or path.startswith == '/?'):
440-
path = '/' + self.indexpage + path[1:]
440+
if self.mappath:
441+
if callable(self.mappath):
442+
path = call_with_asked_args(self.mappath, {'path': path})
443+
else:
444+
path = self.mappath.get(path, path)
441445

442446
await self.ensure_process()
443447

0 commit comments

Comments
 (0)