diff --git a/backend/app/core/config.py b/backend/app/core/config.py index be34107..0048dc7 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -3,44 +3,53 @@ class Settings(BaseSettings): # Application settings - APP_NAME: str = "My LLM App" - APP_VERSION: str = "1.0.0" + APP_NAME: str = "My App" + APP_VERSION: str = "0.1.0" # Username and Password for login USER_NAME: str = os.getenv('USER_NAME', '') PASSWORD: str = os.getenv('PASSWORD', '') - # API KEY - OPENROUTER_API_KEY: str = os.getenv('OPENROUTER_API_KEY', '') - + @property def DB_URL(self): if self.ENV_MODE == "dev": return self.DEV_DB_URL - return '{}://{}:{}@{}:{}/{}'.format( - self.DB_ENGINE, - self.DB_USERNAME, - self.DB_PASS, - self.DB_HOST, - self.DB_PORT, - self.DB_NAME - ) + else: + if self.DATABASE_URL: + return self.DATABASE_URL + else: + return '{}://{}:{}@{}:{}/{}'.format( + self.DB_ENGINE, + self.DB_USERNAME, + self.DB_PASS, + self.DB_HOST, + self.DB_PORT, + self.DB_NAME + ) + @property def ASYNC_DB_URL(self): if self.ENV_MODE == "dev": return "sqlite+aiosqlite:///./dev.db" - return '{}+asyncpg://{}:{}@{}:{}/{}'.format( - self.DB_ENGINE, - self.DB_USERNAME, - self.DB_PASS, - self.DB_HOST, - self.DB_PORT, - self.DB_NAME - ) - - def Get_API_BASE_URL(self): + else: + if self.DATABASE_URL: + URL_split = self.DATABASE_URL.split("://") + return f"{URL_split[0]}+asyncpg://{URL_split[1]}" + else: + return '{}+asyncpg://{}:{}@{}:{}/{}'.format( + self.DB_ENGINE, + self.DB_USERNAME, + self.DB_PASS, + self.DB_HOST, + self.DB_PORT, + self.DB_NAME + ) + + @property + def API_BASE_URL(self) -> str: if self.ENV_MODE == "dev": - self.API_BASE_URL = 'http://localhost:5000/' - return self.API_BASE_URL + return 'http://localhost:5000/' + return self.HOST_URL class DevSettings(Settings): # Environment mode: 'dev' or 'prod' @@ -63,8 +72,11 @@ class ProdSettings(Settings): DB_PORT: str = os.getenv('DB_PORT', '') DB_NAME: str = os.getenv('DB_NAME', '') - # Define API_BASE_URL based on environment mode - API_BASE_URL: str = os.getenv('API_BASE_URL', '') + # Extra Database settings for deploying on Railway; if you provide DATABASE_URL, the above settings will be ignored + DATABASE_URL: str = os.getenv('DATABASE_URL', '') + + # Define HOST_URL based on environment mode + HOST_URL : str = os.getenv('HOST_URL ', '') # Database settings for production model_config = SettingsConfigDict(env_file=".env", extra='allow') diff --git a/backend/app/core/constants.py b/backend/app/core/constants.py index 7059706..f5578e1 100644 --- a/backend/app/core/constants.py +++ b/backend/app/core/constants.py @@ -3,4 +3,4 @@ TEXT_API_QUOTA_LIMIT=20 CONCURRENCY_LIMIT=20 MEMORY_WINDOW_SIZE=3 -API_BASE_URL=settings.Get_API_BASE_URL() \ No newline at end of file +API_BASE_URL=settings.API_BASE_URL \ No newline at end of file diff --git a/backend/app/dependencies/database.py b/backend/app/dependencies/database.py index 683e36c..f306f03 100644 --- a/backend/app/dependencies/database.py +++ b/backend/app/dependencies/database.py @@ -9,11 +9,11 @@ Base = declarative_base() # Synchronous engine and session -sync_engine = create_engine(settings.DB_URL()) +sync_engine = create_engine(settings.DB_URL) SyncSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=sync_engine) # Asynchronous engine and session -async_engine = create_async_engine(settings.ASYNC_DB_URL(), echo=False, future=True) +async_engine = create_async_engine(settings.ASYNC_DB_URL, echo=False, future=True) AsyncSessionLocal = sessionmaker(bind=async_engine, expire_on_commit=False, class_=AsyncSession) def get_sync_db(): diff --git a/backend/app/main.py b/backend/app/main.py index 154009f..2b15c1e 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -8,7 +8,7 @@ from starlette.middleware.sessions import SessionMiddleware from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager -from backend.app.core.init_settings import args +from backend.app.core.init_settings import args, global_settings from backend.app.core.constants import API_BASE_URL from backend.app.api.v1.endpoints import ( user, @@ -54,7 +54,7 @@ async def lifespan(app: FastAPI): # Set Middleware # Define the allowed origins origins = [ - os.getenv("API_BASE_URL", ""), + global_settings.API_BASE_URL, "http://localhost", "http://localhost:5000", ]