Added a debug mode

This commit is contained in:
Donkie
2023-05-14 18:54:05 +02:00
parent 24083b42f9
commit ecc34f3669
2 changed files with 30 additions and 1 deletions

View File

@@ -176,3 +176,19 @@ def get_logging_level() -> int:
if log_level_str == "CRITICAL":
return logging.CRITICAL
raise ValueError(f"Failed to parse SPOOLMAN_LOGGING_LEVEL variable: Unknown logging level '{log_level_str}'.")
def is_debug_mode() -> bool:
"""Get whether debug mode is enabled from environment variables.
Returns False if no environment variable was set for debug mode.
Returns:
bool: Whether debug mode is enabled.
"""
debug_mode = os.getenv("SPOOLMAN_DEBUG_MODE", "FALSE").upper()
if debug_mode == "FALSE" or debug_mode == "0":
return False
if debug_mode == "TRUE" or debug_mode == "1":
return True
raise ValueError(f"Failed to parse SPOOLMAN_DEBUG_MODE variable: Unknown debug mode '{debug_mode}'.")

View File

@@ -5,6 +5,7 @@ from pathlib import Path
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from platformdirs import user_data_dir
from sqlalchemy import URL
@@ -41,10 +42,22 @@ root_logger.addHandler(console)
logger = logging.getLogger(__name__)
# Setup FastAPI
app = FastAPI(debug=True)
app = FastAPI(debug=env.is_debug_mode())
app.mount("/api/v1", v1_app)
app.mount("/", StaticFiles(directory="client/dist", html=True), name="client")
# Allow all origins if in debug mode
if env.is_debug_mode():
logger.warning("Running in debug mode, allowing all origins.")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_connection_url() -> URL:
"""Construct the connection URL for the database based on environment variables."""