Skip to content

Commit

Permalink
Run HTTPS server on any port
Browse files Browse the repository at this point in the history
Setup redirect service only on port 443
  • Loading branch information
dormant-user committed Feb 16, 2024
1 parent 31f50b0 commit 5cd74c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ doc_gen/_static
temp*.py
test.py
*.json

# Certificates
*.pem
19 changes: 8 additions & 11 deletions pystream/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,19 @@ async def start(**kwargs) -> None:
}

if config.env.cert_file and config.env.key_file: # Additional config for HTTPS
if config.env.video_port != 443:
logger.warning("cert_file and key_file were detected, but port was set to %d, swapping it to HTTPS [443]",
config.env.video_port)
argument_dict["port"] = 443
argument_dict["ssl_keyfile"] = config.env.key_file
argument_dict["ssl_certfile"] = config.env.cert_file
uvicorn_config = uvicorn.Config(**argument_dict)
# Initiate a dedicated server listening to port 80 and redirect it to 443
# app.add_middleware(HTTPSRedirectMiddleware)
# is an alternate option but it won't have the server listening on port 80
# so only internal relative links for CSS and JS will work, not user level redirect
http_redirect = dict(app=f"{secure.__name__}:app", port=80, host=config.env.video_host)
process = Process(target=uvicorn.run, kwargs=(http_redirect))
process.start()
logger.info("SSL redirect service started on PID: %d", process.pid)
elif argument_dict["port"] == 443:
if config.env.video_port == 443:
http_redirect = dict(app=f"{secure.__name__}:app", port=80, host=config.env.video_host)
process = Process(target=uvicorn.run, kwargs=(http_redirect))
process.start()
logger.info("SSL redirect service started on PID: %d", process.pid)
elif config.env.video_port == 443:
raise RuntimeWarning(
"'video_port' was set to 443, however 'cert_file' and 'key_file' are missing."
)
Expand All @@ -123,14 +120,14 @@ async def start(**kwargs) -> None:
await uvicorn_server.serve()
except ssl.SSLError as error:
logger.critical(error)
if argument_dict["port"] == 443:
if config.env.video_port == 443:
logger.error("Failed to start server on HTTPS")
process.kill()
raise

logger.info("Initiating shutdown tasks")
await shutdown_tasks()

if argument_dict["port"] == 443:
if config.env.video_port == 443:
logger.info("Shutting down SSL redirect service")
process.kill()

0 comments on commit 5cd74c7

Please sign in to comment.