cp
This commit is contained in:
parent
6a29cba09f
commit
3278f13910
@ -7,6 +7,10 @@ LOG_LEVEL=INFO
|
|||||||
CORS_ORIGINS=*
|
CORS_ORIGINS=*
|
||||||
|
|
||||||
# Database Settings
|
# 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)
|
# Google Cloud Firestore Settings (used when DATABASE_TYPE=firestore)
|
||||||
# Path to service account credentials file (optional, uses application default credentials if not set)
|
# Path to service account credentials file (optional, uses application default credentials if not set)
|
||||||
|
|||||||
4
main.py
4
main.py
@ -40,8 +40,8 @@ app.add_middleware(
|
|||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=settings.CORS_ORIGINS,
|
allow_origins=settings.CORS_ORIGINS,
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||||
allow_headers=["*"],
|
allow_headers=["Content-Type", "Authorization", "X-API-Key"],
|
||||||
)
|
)
|
||||||
|
|
||||||
# Include API routers
|
# Include API routers
|
||||||
|
|||||||
@ -12,7 +12,7 @@ class Settings(BaseSettings):
|
|||||||
ENVIRONMENT: str = "development"
|
ENVIRONMENT: str = "development"
|
||||||
|
|
||||||
# CORS settings
|
# 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")
|
@field_validator("CORS_ORIGINS", mode="before")
|
||||||
def assemble_cors_origins(cls, v):
|
def assemble_cors_origins(cls, v):
|
||||||
@ -22,6 +22,7 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
# Firestore settings
|
# Firestore settings
|
||||||
FIRESTORE_PROJECT_ID: str = os.getenv("FIRESTORE_PROJECT_ID", "")
|
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")
|
FIRESTORE_CREDENTIALS_FILE: str = os.getenv("FIRESTORE_CREDENTIALS_FILE", "firestore-credentials.json")
|
||||||
|
|
||||||
# Google Cloud Storage settings
|
# Google Cloud Storage settings
|
||||||
|
|||||||
@ -9,20 +9,27 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
client = None
|
client = None
|
||||||
|
database_name = None
|
||||||
|
|
||||||
def connect_to_database(self):
|
def connect_to_database(self):
|
||||||
"""Create database connection."""
|
"""Create database connection."""
|
||||||
try:
|
try:
|
||||||
# Print project ID for debugging
|
# Store database name
|
||||||
logger.info(f"Attempting to connect to Firestore with project ID: {settings.FIRESTORE_PROJECT_ID}")
|
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)
|
# First try using Application Default Credentials (for Cloud environments)
|
||||||
try:
|
try:
|
||||||
logger.info("Attempting to connect using Application Default Credentials")
|
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
|
# Test connection by trying to access a collection
|
||||||
self.client.collection('test').limit(1).get()
|
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
|
return
|
||||||
except Exception as adc_error:
|
except Exception as adc_error:
|
||||||
logger.error(f"Application Default Credentials failed: {adc_error}", exc_info=True)
|
logger.error(f"Application Default Credentials failed: {adc_error}", exc_info=True)
|
||||||
@ -50,13 +57,14 @@ class Database:
|
|||||||
# Initialize Firestore client
|
# Initialize Firestore client
|
||||||
self.client = firestore.Client(
|
self.client = firestore.Client(
|
||||||
project=settings.FIRESTORE_PROJECT_ID,
|
project=settings.FIRESTORE_PROJECT_ID,
|
||||||
|
database=self.database_name,
|
||||||
credentials=credentials
|
credentials=credentials
|
||||||
)
|
)
|
||||||
|
|
||||||
# Test connection by trying to access a collection
|
# Test connection by trying to access a collection
|
||||||
self.client.collection('test').limit(1).get()
|
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:
|
except Exception as e:
|
||||||
logger.error(f"Failed to connect to Firestore: {e}", exc_info=True)
|
logger.error(f"Failed to connect to Firestore: {e}", exc_info=True)
|
||||||
raise
|
raise
|
||||||
|
|||||||
@ -31,14 +31,14 @@ class FirestoreProvider:
|
|||||||
if settings.GCS_CREDENTIALS_FILE and os.path.exists(settings.GCS_CREDENTIALS_FILE):
|
if settings.GCS_CREDENTIALS_FILE and os.path.exists(settings.GCS_CREDENTIALS_FILE):
|
||||||
self.client = firestore.Client.from_service_account_json(
|
self.client = firestore.Client.from_service_account_json(
|
||||||
settings.GCS_CREDENTIALS_FILE,
|
settings.GCS_CREDENTIALS_FILE,
|
||||||
database='imagedb' # Use the specific database ID
|
settings.FIRESTORE_DATABASE_NAME
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Use application default credentials with specific database
|
# 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
|
self._db = self.client
|
||||||
logger.info("Connected to Firestore database: imagedb")
|
logger.info(f"Connected to Firestore database: {settings.FIRESTORE_DATABASE_NAME}")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to connect to Firestore: {e}")
|
logger.error(f"Failed to connect to Firestore: {e}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user