From 02de5993e68231cc8171db0f7bd986ae4429403d Mon Sep 17 00:00:00 2001 From: Chris Margach Date: Thu, 10 Apr 2025 13:42:30 +0900 Subject: [PATCH] allow passing of cert and key locations to uvicorn via package --- whisperlivekit/basic_server.py | 42 ++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/whisperlivekit/basic_server.py b/whisperlivekit/basic_server.py index ef979d7..110219a 100644 --- a/whisperlivekit/basic_server.py +++ b/whisperlivekit/basic_server.py @@ -8,7 +8,8 @@ from whisperlivekit.audio_processor import AudioProcessor import asyncio import logging -import os +import os, sys +import argparse logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") logging.getLogger().setLevel(logging.WARNING) @@ -71,16 +72,37 @@ async def websocket_endpoint(websocket: WebSocket): def main(): """Entry point for the CLI command.""" import uvicorn - + + parser = argparse.ArgumentParser(description="Run the WhisperLiveKit server.") + parser.add_argument("--ssl-certfile", type=str, help="Path to the SSL certificate file.") + parser.add_argument("--ssl-keyfile", type=str, help="Path to the SSL private key file.") + args, unknown = parser.parse_known_args() + + ssl_kwargs = {} + if args.ssl_certfile or args.ssl_keyfile: + if not (args.ssl_certfile and args.ssl_keyfile): + raise ValueError("Both --ssl-certfile and --ssl-keyfile must be specified together.") + ssl_kwargs = { + "ssl_certfile": args.ssl_certfile, + "ssl_keyfile": args.ssl_keyfile + } + # Remove uvicorn-specific args from the context to avoid tripping up other parts of the stack + sys.argv = [sys.argv[0]] + unknown + temp_kit = WhisperLiveKit(transcription=False, diarization=False) - - uvicorn.run( - "whisperlivekit.basic_server:app", - host=temp_kit.args.host, - port=temp_kit.args.port, - reload=True, - log_level="info" - ) + + uvicorn_kwargs = { + "app": "whisperlivekit.basic_server:app", + "host":temp_kit.args.host, + "port":temp_kit.args.port, + "reload": True, + "log_level": "info", + } + + if ssl_kwargs: + uvicorn_kwargs = {**uvicorn_kwargs, **ssl_kwargs} + + uvicorn.run(**uvicorn_kwargs) if __name__ == "__main__": main()