-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmos.py
49 lines (37 loc) · 1.53 KB
/
cosmos.py
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
import logging
from dotenv import load_dotenv
from fastapi import HTTPException
from pydantic_settings import BaseSettings
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Load environment variables from .env file
load_dotenv()
class Settings(BaseSettings):
COSMOS_CONNECTION_STRING: str
DATABASE_NAME: str
CONTAINER_NAME: str
class Config:
env_file = ".env_cosmosdb"
def initialize_settings():
try:
# Create an instance of Settings
local_settings = Settings()
# Check if the required fields are set
if not local_settings.COSMOS_CONNECTION_STRING or not local_settings.DATABASE_NAME or not local_settings.CONTAINER_NAME:
logger.error("One or more required environment variables are missing.")
raise HTTPException(status_code=500,
detail="Configuration error: Required environment variables are missing.")
logger.info("Settings loaded successfully.")
return local_settings
except FileNotFoundError:
logger.critical(".env file not found.")
raise HTTPException(status_code=500, detail="Configuration error: .env file not found.")
except Exception as e:
logger.critical(f"Error loading settings: {e}")
raise HTTPException(status_code=500, detail="Configuration error: An error occurred while loading settings.")
# Initialize settings
settings = initialize_settings()