From be0de4136596a81f55dbe41bc3c809d8c6787059 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Prescott" Date: Tue, 4 Jun 2024 13:39:31 +0200 Subject: [PATCH] Lightly server https (#1552) closes lig-5082 - allow to pass ssl certs to lightly-serve to start it in HTTPS mode --- lightly/cli/config/lightly-serve.yaml | 3 ++- lightly/cli/serve_cli.py | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lightly/cli/config/lightly-serve.yaml b/lightly/cli/config/lightly-serve.yaml index 29518110d..ce1f21ce0 100644 --- a/lightly/cli/config/lightly-serve.yaml +++ b/lightly/cli/config/lightly-serve.yaml @@ -2,7 +2,8 @@ input_mount: '' # Path to the input directory. lightly_mount: '' # Path to the lightly directory. host: 'localhost' # Hostname for serving the data. port: 3456 # Port for serving the data. - +ssl_cert: '' # Path to the SSL certificate. +ssl_key: '' # Path to the SSL key. # Disable Hydra log directories. defaults: diff --git a/lightly/cli/serve_cli.py b/lightly/cli/serve_cli.py index f0c36fcf4..54d0fdb3e 100644 --- a/lightly/cli/serve_cli.py +++ b/lightly/cli/serve_cli.py @@ -1,3 +1,4 @@ +import ssl import sys from pathlib import Path @@ -19,9 +20,14 @@ def lightly_serve(cfg): lightly_mount: Path to the Lightly directory. host: - Hostname for serving the data (defaults to localhost). + Hostname for serving the data (defaults to localhost). If you want to expose it to the internet or your local network, use '0.0.0.0'. + See our docs on lightly-serve for more information: https://docs.lightly.ai/docs/local-storage#view-the-local-data-securely-over-the-networkvpn port: Port for serving the data (defaults to 3456). + ssl_key: + Optional path to the ssl key file. + ssl_cert: + Optional path to the ssl cert file. Examples: >>> lightly-serve input_mount=data/ lightly_mount=lightly/ port=3456 @@ -52,6 +58,16 @@ def lightly_serve(cfg): host=cfg.host, port=cfg.port, ) + + # setup https/ssl if key or cert are provided + if cfg.ssl_key or cfg.ssl_cert: + httpd.socket = ssl.wrap_socket( + httpd.socket, + keyfile=Path(cfg.ssl_key) if cfg.ssl_key else None, + certfile=Path(cfg.ssl_cert) if cfg.ssl_cert else None, + server_side=True, + ) + print( f"Starting server, listening at '{bcolors.OKBLUE}{httpd.server_name}:{httpd.server_port}{bcolors.ENDC}'" )