From 4c0d4ffc47daeac20f2cb4adac2df97eaf5d34c5 Mon Sep 17 00:00:00 2001 From: Min RK Date: Mon, 26 Jun 2017 12:25:33 +0200 Subject: [PATCH] add default hub route via Proxy.add_route instead of relying on default target --- jupyterhub/app.py | 1 + jupyterhub/proxy.py | 18 ++++++++++++++++-- jupyterhub/tests/test_proxy.py | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/jupyterhub/app.py b/jupyterhub/app.py index 52451a2c..f222d4d1 100644 --- a/jupyterhub/app.py +++ b/jupyterhub/app.py @@ -1433,6 +1433,7 @@ class JupyterHub(Application): self.exit(1) else: self.log.info("Not starting proxy") + yield self.proxy.add_hub_route(self.hub) # start the service(s) for service_name, service in self._service_map.items(): diff --git a/jupyterhub/proxy.py b/jupyterhub/proxy.py index 3d25cc23..e9398ff8 100644 --- a/jupyterhub/proxy.py +++ b/jupyterhub/proxy.py @@ -107,6 +107,10 @@ class Proxy(LoggingConfigurable): - Checks host value vs host-based routing. - Ensures trailing slash on path. """ + if routespec == '/': + # / is the default route. + # don't check host-based routing + return routespec # check host routing host_route = not routespec.startswith('/') if host_route and not self.host_routing: @@ -281,6 +285,11 @@ class Proxy(LoggingConfigurable): user_routes = {r['data']['user'] for r in routes.values() if 'user' in r['data']} futures = [] db = self.db + + if '/' not in routes: + self.log.warning("Adding missing default route") + self.add_hub_route(self.app.hub) + for orm_user in db.query(User): user = user_dict[orm_user] if user.running: @@ -315,9 +324,15 @@ class Proxy(LoggingConfigurable): for f in futures: yield f + def add_hub_route(self, hub): + """Add the default route for the Hub""" + self.log.info("Adding default route for Hub: / => %s", hub.host) + return self.add_route('/', self.hub.host) + @gen.coroutine def restore_routes(self): self.log.info("Setting up routes on new proxy") + yield self.add_hub_route(self.app.hub) yield self.add_all_users(self.app.users) yield self.add_all_services(self.app.services) self.log.info("New proxy back up and good to go") @@ -379,7 +394,6 @@ class ConfigurableHTTPProxy(Proxy): '--port', str(public_server.port), '--api-ip', api_server.ip, '--api-port', str(api_server.port), - '--default-target', self.hub.host, '--error-target', url_path_join(self.hub.url, 'error'), ] if self.app.subdomain_host: @@ -464,7 +478,7 @@ class ConfigurableHTTPProxy(Proxy): path = self.validate_routespec(routespec) # CHP always wants to start with / if not path.startswith('/'): - path = path + '/' + path = '/' + path # BUG: CHP doesn't seem to like trailing slashes on some endpoints (DELETE) if path != '/' and path.endswith('/'): path = path.rstrip('/') diff --git a/jupyterhub/tests/test_proxy.py b/jupyterhub/tests/test_proxy.py index b270d738..a8d641c7 100644 --- a/jupyterhub/tests/test_proxy.py +++ b/jupyterhub/tests/test_proxy.py @@ -43,7 +43,7 @@ def test_external_proxy(request, io_loop): '--port', str(app.port), '--api-ip', proxy_ip, '--api-port', str(proxy_port), - '--default-target', 'http://%s:%i' % (app.hub_ip, app.hub_port), + '--log-level=debug', ] if app.subdomain_host: cmd.append('--host-routing') @@ -90,7 +90,7 @@ def test_external_proxy(request, io_loop): routes = io_loop.run_sync(app.proxy.get_all_routes) - assert list(routes.keys()) == ['/'] + assert list(routes.keys()) == [] # poke the server to update the proxy r = api_request(app, 'proxy', method='post')