diff --git a/backend/main.py b/backend/main.py index 6d2217a..eaf969b 100644 --- a/backend/main.py +++ b/backend/main.py @@ -55,8 +55,26 @@ if not config_file.exists(): with open(config_file) as f: config = json.load(f) -cert_path = config.get("ssl_cert_path", "ssl/cert.pem") -key_path = config.get("ssl_key_path", "ssl/key.pem") +# Set SSL paths based on environment +if environment in ["test", "dev"]: + cert_path = config.get("ssl_cert_path", "ssl/cert.pem") + key_path = config.get("ssl_key_path", "ssl/key.pem") +elif environment == "prod": + cert_path = config.get("SSL_CERT_PATH") + key_path = config.get("SSL_KEY_PATH") + # Validate production SSL paths + if not cert_path or not key_path: + raise ValueError( + "SSL_CERT_PATH and SSL_KEY_PATH must be set in config_prod.json" + " for production." + ) + if not Path(cert_path).exists() or not Path(key_path).exists(): + raise FileNotFoundError( + f"Missing SSL files in production. Ensure the following files exist:\n" + f"SSL Certificate: {cert_path}\nSSL Key: {key_path}" + ) +else: + raise ValueError(f"Unknown environment: {environment}") # Generate SSL Key and Certificate if not exist (only for development) if environment == "dev": @@ -146,20 +164,14 @@ if __name__ == "__main__": port = int(os.getenv("PORT", 8000)) is_ci = os.getenv("CI", "false").lower() == "true" - # Development or Test environment - if environment in ["test", "dev"]: - cert_path = "ssl/cert.pem" - key_path = "ssl/key.pem" - host = "127.0.0.1" - else: - cert_path = os.getenv("VITE_SSL_CERT_PATH", "ssl/prod-cert.pem") - key_path = os.getenv("VITE_SSL_KEY_PATH", "ssl/prod-key.pem") - host = "0.0.0.0" - def run_server(): + print(f"[INFO] Starting server in {environment} environment...") + print(f"[INFO] SSL Certificate Path: {cert_path}") + print(f"[INFO] SSL Key Path: {key_path}") + print(f"[INFO] Running on port {port}") uvicorn.run( app, - host=host, + host="127.0.0.1" if environment in ["dev", "test"] else "0.0.0.0", port=port, log_level="debug", ssl_keyfile=key_path,