This commit is contained in:
johnpccd 2025-05-24 06:38:15 +02:00
parent 6a29cba09f
commit 3278f13910
5 changed files with 24 additions and 11 deletions

View File

@ -7,6 +7,10 @@ LOG_LEVEL=INFO
CORS_ORIGINS=*
# Database Settings
# Firestore settings
FIRESTORE_PROJECT_ID=gen-lang-client-0424120530
FIRESTORE_DATABASE_NAME=imagedb
FIRESTORE_CREDENTIALS_FILE=firestore-credentials.json
# Google Cloud Firestore Settings (used when DATABASE_TYPE=firestore)
# Path to service account credentials file (optional, uses application default credentials if not set)

View File

@ -40,8 +40,8 @@ app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["Content-Type", "Authorization", "X-API-Key"],
)
# Include API routers

View File

@ -12,7 +12,7 @@ class Settings(BaseSettings):
ENVIRONMENT: str = "development"
# CORS settings
CORS_ORIGINS: List[str] = ["*"]
CORS_ORIGINS: List[str] = ["http://localhost:3000", "http://localhost:8000", "http://127.0.0.1:8000", "http://127.0.0.1:3000", "https://localhost:3000", "https://localhost:8000"]
@field_validator("CORS_ORIGINS", mode="before")
def assemble_cors_origins(cls, v):
@ -22,6 +22,7 @@ class Settings(BaseSettings):
# Firestore settings
FIRESTORE_PROJECT_ID: str = os.getenv("FIRESTORE_PROJECT_ID", "")
FIRESTORE_DATABASE_NAME: str = os.getenv("FIRESTORE_DATABASE_NAME", "sereact-db")
FIRESTORE_CREDENTIALS_FILE: str = os.getenv("FIRESTORE_CREDENTIALS_FILE", "firestore-credentials.json")
# Google Cloud Storage settings

View File

@ -9,20 +9,27 @@ logger = logging.getLogger(__name__)
class Database:
client = None
database_name = None
def connect_to_database(self):
"""Create database connection."""
try:
# Print project ID for debugging
logger.info(f"Attempting to connect to Firestore with project ID: {settings.FIRESTORE_PROJECT_ID}")
# Store database name
self.database_name = settings.FIRESTORE_DATABASE_NAME
# Print project ID and database name for debugging
logger.info(f"Attempting to connect to Firestore with project ID: {settings.FIRESTORE_PROJECT_ID}, database: {self.database_name}")
# First try using Application Default Credentials (for Cloud environments)
try:
logger.info("Attempting to connect using Application Default Credentials")
self.client = firestore.Client(project=settings.FIRESTORE_PROJECT_ID)
self.client = firestore.Client(
project=settings.FIRESTORE_PROJECT_ID,
database=self.database_name
)
# Test connection by trying to access a collection
self.client.collection('test').limit(1).get()
logger.info(f"Connected to Firestore project using Application Default Credentials: {settings.FIRESTORE_PROJECT_ID}")
logger.info(f"Connected to Firestore project using Application Default Credentials: {settings.FIRESTORE_PROJECT_ID}, database: {self.database_name}")
return
except Exception as adc_error:
logger.error(f"Application Default Credentials failed: {adc_error}", exc_info=True)
@ -50,13 +57,14 @@ class Database:
# Initialize Firestore client
self.client = firestore.Client(
project=settings.FIRESTORE_PROJECT_ID,
database=self.database_name,
credentials=credentials
)
# Test connection by trying to access a collection
self.client.collection('test').limit(1).get()
logger.info(f"Connected to Firestore project using credentials file: {settings.FIRESTORE_PROJECT_ID}")
logger.info(f"Connected to Firestore project using credentials file: {settings.FIRESTORE_PROJECT_ID}, database: {self.database_name}")
except Exception as e:
logger.error(f"Failed to connect to Firestore: {e}", exc_info=True)
raise

View File

@ -31,14 +31,14 @@ class FirestoreProvider:
if settings.GCS_CREDENTIALS_FILE and os.path.exists(settings.GCS_CREDENTIALS_FILE):
self.client = firestore.Client.from_service_account_json(
settings.GCS_CREDENTIALS_FILE,
database='imagedb' # Use the specific database ID
settings.FIRESTORE_DATABASE_NAME
)
else:
# Use application default credentials with specific database
self.client = firestore.Client(database='imagedb')
self.client = firestore.Client(database=settings.FIRESTORE_DATABASE_NAME)
self._db = self.client
logger.info("Connected to Firestore database: imagedb")
logger.info(f"Connected to Firestore database: {settings.FIRESTORE_DATABASE_NAME}")
return True
except Exception as e:
logger.error(f"Failed to connect to Firestore: {e}")