From 6175fafd20cc223c5ded6aeee3e981ba7f479418 Mon Sep 17 00:00:00 2001 From: gnat Date: Wed, 14 Aug 2024 11:33:02 -0700 Subject: [PATCH] add support for configuration --- src/main.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.py b/src/main.py index 15539bc..c31bef0 100644 --- a/src/main.py +++ b/src/main.py @@ -4,8 +4,12 @@ import threading import socket import ssl import os +import yaml -os.chdir('site') +with open('./config.yaml', 'r') as f: + config = yaml.safe_load(f.read()) + +os.chdir(config['file-dir']) def handle_client(client: socket.socket, addr: Tuple[str, int]) -> None: request = bytes() @@ -22,16 +26,16 @@ def handle_client(client: socket.socket, addr: Tuple[str, int]) -> None: .send(client) def main() -> None: - http_thread = threading.Thread(name='http', target=serve, args=('0.0.0.0', 6000, handle_client)) - https_thread = threading.Thread(name='https', target=serve, args=('0.0.0.0', 6001, handle_client), kwargs=dict(wrapper=lambda socket: [ + http_thread = threading.Thread(name='http', target=serve, args=('0.0.0.0', config['http-port'], handle_client)) + https_thread = threading.Thread(name='https', target=serve, args=('0.0.0.0', config['https-port'], handle_client), kwargs=dict(wrapper=lambda socket: [ ctx:=ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER), - ctx.load_cert_chain(certfile='./badcert.pem', keyfile='./badkey.pem'), + ctx.load_cert_chain(certfile=config['ssl-cert'], keyfile=config['ssl-key']), ctx.wrap_socket(socket, server_side=True) ][-1] )) - #http_thread.start() - https_thread.start() + http_thread.start() if config['http-port'] else None + https_thread.start() if config['https-port'] else None if __name__ == '__main__': main()