Refactor OpenAPI client script and backend server logic.

Simplify and streamline OpenAPI client generation and backend startup logic. Improved error handling, environment configuration, and self-signed SSL certificate management. Added support for generating OpenAPI schema via command-line argument.
This commit is contained in:
GotthardG 2024-12-17 15:34:47 +01:00
parent f49f329015
commit 25528811dc

View File

@ -55,8 +55,26 @@ if not config_file.exists():
with open(config_file) as f: with open(config_file) as f:
config = json.load(f) config = json.load(f)
cert_path = config.get("ssl_cert_path", "ssl/cert.pem") # Set SSL paths based on environment
key_path = config.get("ssl_key_path", "ssl/key.pem") 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) # Generate SSL Key and Certificate if not exist (only for development)
if environment == "dev": if environment == "dev":
@ -146,20 +164,14 @@ if __name__ == "__main__":
port = int(os.getenv("PORT", 8000)) port = int(os.getenv("PORT", 8000))
is_ci = os.getenv("CI", "false").lower() == "true" 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(): 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( uvicorn.run(
app, app,
host=host, host="127.0.0.1" if environment in ["dev", "test"] else "0.0.0.0",
port=port, port=port,
log_level="debug", log_level="debug",
ssl_keyfile=key_path, ssl_keyfile=key_path,