Skip to content

Commit

Permalink
Extract address from JUPYTERHUB_SERVICE_URL
Browse files Browse the repository at this point in the history
  • Loading branch information
jwindgassen committed Sep 25, 2024
1 parent e561072 commit c0d457d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
42 changes: 33 additions & 9 deletions jupyter_server_proxy/standalone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
import argparse
import logging
import os
from urllib.parse import urlparse

from tornado import ioloop
from tornado.httpserver import HTTPServer
from tornado.log import app_log as log
from tornado.log import enable_pretty_logging, gen_log

from .activity import start_activity_update
from .proxy import configure_ssl, make_proxy_app, get_port_from_env
from .proxy import configure_ssl, make_proxy_app


def _default_address_and_port() -> tuple[str, int]:
"""
Get the Address and Port for the Proxy, either from JUPYTERHUB_SERVICE_URL or default values.
See https://github.com/jupyterhub/jupyterhub/blob/4.x/jupyterhub/singleuser/mixins.py#L266-L284.
"""
address = "127.0.0.1"
port = 8888

if os.environ.get("JUPYTERHUB_SERVICE_URL"):
url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"])

if url.hostname:
address = url.hostname

if url.port:
port = url.port
elif url.scheme == "http":
port = 80
elif url.scheme == "https":
port = 443

return address, port


def run(
command: list[str],
port: int,
address: str,
port: int | None,
address: str | None,
server_port: int,
socket_path: str | None,
socket_auto: bool,
Expand All @@ -34,8 +59,9 @@ def run(
log.setLevel(logging.DEBUG)
gen_log.setLevel(logging.DEBUG)

if not port:
port = get_port_from_env()
address_port_default = _default_address_and_port()
address = address or address_port_default[0]
port = port or address_port_default[1]

if skip_authentication:
log.warn("Disabling Authentication with JuypterHub Server!")
Expand Down Expand Up @@ -84,18 +110,16 @@ def main():
parser.add_argument(
"-p",
"--port",
default=0,
type=int,
dest="port",
help="Port for the proxy server to listen on (0 for JupyterHub default).",
help="Set port for the proxy server to listen on. Will use 'JUPYTERHUB_SERVICE_URL' or '127.0.0.1' by default.",
)
parser.add_argument(
"-a",
"--address",
default="localhost",
type=str,
dest="address",
help="Address for the proxy server to listen on.",
help="Set address for the proxy server to listen on. Will use 'JUPYTERHUB_SERVICE_URL' or '8888' by default.",
)
parser.add_argument(
"-s",
Expand Down
14 changes: 0 additions & 14 deletions jupyter_server_proxy/standalone/proxy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import re
from logging import Logger
from urllib.parse import urlparse

from jupyterhub import __version__ as __jh_version__
from jupyterhub.services.auth import HubOAuthCallbackHandler, HubOAuthenticated
Expand Down Expand Up @@ -148,16 +147,3 @@ def __init__(self, *args, **kwargs):
)

return app


# https://github.com/jupyterhub/jupyterhub/blob/2.0.0rc3/jupyterhub/singleuser/mixins.py#L340-L349
def get_port_from_env():
if os.environ.get("JUPYTERHUB_SERVICE_URL"):
url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"])
if url.port:
return url.port
elif url.scheme == "http":
return 80
elif url.scheme == "https":
return 443
return 8888

0 comments on commit c0d457d

Please sign in to comment.