Skip to content

Commit f8bce56

Browse files
committed
config: async driver
1 parent ed4a9fc commit f8bce56

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

backend/qllm/core/config.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from enum import Enum
33
from typing import Annotated, Any, List, Literal, Optional
44

5-
from pydantic import AnyUrl, BeforeValidator, HttpUrl, PostgresDsn
6-
from pydantic_core import MultiHostUrl
5+
from pydantic import AnyUrl, BeforeValidator, HttpUrl
76
from pydantic_settings import BaseSettings
87

98

@@ -18,6 +17,15 @@ def parse_cors(v: Any) -> List[str] | str:
1817
raise ValueError(v)
1918

2019

20+
def replace_postgresql_asyncpg(url: str) -> str:
21+
"""
22+
Replaces `postgresql://` with `postgresql+asyncpg://` in the URL.
23+
"""
24+
if "postgresql://" in url:
25+
return url.replace("postgresql://", "postgresql+asyncpg://")
26+
return url
27+
28+
2129
class AppEnvironment(str, Enum):
2230
"""
2331
Enum for app environments.
@@ -36,6 +44,7 @@ class ApplicationSetting(BaseSettings):
3644
PROJECT_NAME: str = "QLLM"
3745
API_PREFIX: str = "/api/v1"
3846
SECRET_KEY: str = secrets.token_urlsafe(32)
47+
PORT: int = 10000
3948
ACCESS_TOKEN_EXPIRE_MINUTES: int = 11520
4049
UVICORN_WORKER_COUNT: Optional[int] = None
4150
BACKEND_CORS_ORIGINS: Annotated[
@@ -53,7 +62,7 @@ class ApplicationSetting(BaseSettings):
5362
ALGORITHM: str = "HS256"
5463

5564
# Database config
56-
DATABASE_URL: str
65+
DATABASE_URL: Annotated[str, BeforeValidator(replace_postgresql_asyncpg)]
5766

5867
# LLM Config
5968
LLM_TEMPERATURE: float = 0.5

backend/qllm/core/database.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
DATABASE_URL = settings.DATABASE_URL
66

7-
if "postgresql://" in DATABASE_URL:
8-
DATABASE_URL = DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://")
97

108
print("DATABASE_URL: ", DATABASE_URL)
119
engine = create_async_engine(DATABASE_URL, echo=True)

backend/qllm/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ def __setup_logging(log_level: str):
4545

4646
@asynccontextmanager
4747
async def lifespan(app: FastAPI):
48-
import os
49-
from pathlib import Path
50-
5148
# first wait for DB to be connectable
5249
await check_database_connection()
5350

@@ -145,7 +142,7 @@ def start():
145142
uvicorn.run(
146143
"qllm.main:app",
147144
host="0.0.0.0",
148-
port=8000,
145+
port=settings.PORT,
149146
reload=live_reload,
150147
workers=settings.UVICORN_WORKER_COUNT,
151148
)

0 commit comments

Comments
 (0)