Some style fixes
This commit is contained in:
@@ -210,6 +210,7 @@ def is_debug_mode() -> bool:
|
|||||||
return True
|
return True
|
||||||
raise ValueError(f"Failed to parse SPOOLMAN_DEBUG_MODE variable: Unknown debug mode '{debug_mode}'.")
|
raise ValueError(f"Failed to parse SPOOLMAN_DEBUG_MODE variable: Unknown debug mode '{debug_mode}'.")
|
||||||
|
|
||||||
|
|
||||||
def is_cors_defined() -> bool:
|
def is_cors_defined() -> bool:
|
||||||
"""Get whether CORS is enabled from environment variables.
|
"""Get whether CORS is enabled from environment variables.
|
||||||
|
|
||||||
@@ -221,10 +222,8 @@ def is_cors_defined() -> bool:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
cors = os.getenv("SPOOLMAN_CORS_ORIGIN", "FALSE").upper()
|
cors = os.getenv("SPOOLMAN_CORS_ORIGIN", "FALSE").upper()
|
||||||
if cors in {"FALSE", "0"}:
|
return cors not in {"FALSE", "0"}
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def get_cors_origin() -> Optional[list[str]]:
|
def get_cors_origin() -> Optional[list[str]]:
|
||||||
"""Get the CORS origin from environment variables.
|
"""Get the CORS origin from environment variables.
|
||||||
@@ -240,6 +239,7 @@ def get_cors_origin() -> Optional[list[str]]:
|
|||||||
return None
|
return None
|
||||||
return cors.split(",")
|
return cors.split(",")
|
||||||
|
|
||||||
|
|
||||||
def is_automatic_backup_enabled() -> bool:
|
def is_automatic_backup_enabled() -> bool:
|
||||||
"""Get whether automatic backup is enabled from environment variables.
|
"""Get whether automatic backup is enabled from environment variables.
|
||||||
|
|
||||||
|
|||||||
@@ -103,13 +103,23 @@ window.SPOOLMAN_BASE_PATH = "{base_path}";
|
|||||||
# Mount the client side app
|
# Mount the client side app
|
||||||
app.mount(base_path, app=SinglePageApplication(directory="client/dist", base_path=env.get_base_path()))
|
app.mount(base_path, app=SinglePageApplication(directory="client/dist", base_path=env.get_base_path()))
|
||||||
|
|
||||||
# Allow all origins if in debug mode
|
|
||||||
if env.is_debug_mode() or env.is_cors_defined():
|
def add_cors_middleware() -> None:
|
||||||
if(env.is_cors_defined()):
|
"""Add CORS middleware to the FastAPI app based on environment settings."""
|
||||||
origins = env.get_cors_origin()
|
origins = []
|
||||||
else:
|
if env.is_debug_mode():
|
||||||
origins = ["*"]
|
|
||||||
logger.warning("Running in debug mode, allowing all origins.")
|
logger.warning("Running in debug mode, allowing all origins.")
|
||||||
|
origins = ["*"]
|
||||||
|
elif env.is_cors_defined():
|
||||||
|
cors_origins = env.get_cors_origin()
|
||||||
|
if cors_origins:
|
||||||
|
logger.info("CORS origins defined: %s", cors_origins)
|
||||||
|
origins = cors_origins
|
||||||
|
else:
|
||||||
|
logger.warning("CORS origins are not defined, no CORS will be applied.")
|
||||||
|
|
||||||
|
if not origins:
|
||||||
|
return
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
@@ -121,6 +131,9 @@ if env.is_debug_mode() or env.is_cors_defined():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
add_cors_middleware()
|
||||||
|
|
||||||
|
|
||||||
def add_file_logging() -> None:
|
def add_file_logging() -> None:
|
||||||
"""Add file logging to the root logger."""
|
"""Add file logging to the root logger."""
|
||||||
# Define a file logger with log rotation
|
# Define a file logger with log rotation
|
||||||
|
|||||||
Reference in New Issue
Block a user