File size: 2,591 Bytes
58de40c
 
 
bc5091e
58de40c
 
f477f87
 
58de40c
f477f87
58de40c
 
 
 
 
048c3fc
5a007ca
58de40c
 
 
abaeb0b
 
 
 
 
 
 
048c3fc
 
58de40c
 
f477f87
 
58de40c
 
 
 
 
 
d1ed6b1
f477f87
e9bcee8
abaeb0b
49be7fc
048c3fc
0e508c8
 
 
048c3fc
 
0e508c8
048c3fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
config.py

Global configuration and logger setup for the project.

Key Features:
- Uses environment variables defined in the system (Docker in production).
- Loads a `.env` file only in development to simulate production variables locally.
- Configures the logger for consistent logging across all modules.
- Dynamically enables DEBUG logging in development and INFO logging in production (unless overridden).
"""

# Standard Library Imports
import logging
import os
from pathlib import Path

# Third-Party Library Imports
from dotenv import load_dotenv

# Determine the environment (defaults to "dev" if not explicitly set)
APP_ENV = os.getenv("APP_ENV", "dev").lower()
if APP_ENV not in {"dev", "prod"}:
    APP_ENV = "dev"


# In development, load environment variables from .env file (not used in production)
if APP_ENV == "dev" and Path(".env").exists():
    load_dotenv(".env", override=True)


# Enable debug mode if in development (or if explicitly set in env variables)
DEBUG = APP_ENV == "dev" or os.getenv("DEBUG", "false").lower() == "true"

# Configure the logger
logging.basicConfig(
    level=logging.DEBUG if DEBUG else logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger: logging.Logger = logging.getLogger("tts_arena")
logger.info(f'App running in "{APP_ENV}" mode.')
logger.info(f'Debug mode is {"enabled" if DEBUG else "disabled"}.')

if DEBUG:
    logger.debug("DEBUG mode enabled.")


# Define the directory for audio files relative to the project root
AUDIO_DIR = Path.cwd() / "static" / "audio"
AUDIO_DIR.mkdir(parents=True, exist_ok=True)
logger.info(f"Audio directory set to {AUDIO_DIR}")


def validate_env_var(var_name: str) -> str:
    """
    Validates that an environment variable is set and returns its value.

    Args:
        var_name (str): The name of the environment variable to validate.

    Returns:
        str: The value of the environment variable.

    Raises:
        ValueError: If the environment variable is not set.

    Examples:
        >>> import os
        >>> os.environ["EXAMPLE_VAR"] = "example_value"
        >>> validate_env_var("EXAMPLE_VAR")
        'example_value'

        >>> validate_env_var("MISSING_VAR")
        Traceback (most recent call last):
          ...
        ValueError: MISSING_VAR is not set. Please ensure it is defined in your environment variables.
    """
    value = os.environ.get(var_name, "")
    if not value:
        raise ValueError(
            f"{var_name} is not set. Please ensure it is defined in your environment variables."
        )
    return value