Skip to content

Commit 17ebcc5

Browse files
committed
wip: setting refactor
1 parent 8f5c489 commit 17ebcc5

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ SQL_USER=user
88
SQL_PASS=secret
99
SQL_URL=postgresql+asyncpg://${SQL_USER}:${SQL_PASS}@${SQL_HOST}/${SQL_DB}
1010

11+
# Postgres
12+
POSTGRES_SERVER=db
13+
POSTGRES_PORT=5432
14+
POSTGRES_DB=devdb
15+
POSTGRES_TEST_DB=testdb
16+
POSTGRES_USER=user
17+
POSTGRES_PASSWORD=secret
18+
19+
# Redis
1120
REDIS_HOST=redis
1221
REDIS_PORT=6379
1322
REDIS_DB=2

app/api/nonsense.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,44 @@ async def import_nonsense(
107107
finally:
108108
# Ensure that the database session is closed, regardless of whether an error occurred or not
109109
await db_session.close()
110+
111+
112+
# TODO: add websocket to full text search postgres database for nonsense description
113+
114+
# To add a WebSocket to full text search a PostgreSQL database for the `nonsense` description, you can use the `websockets` library in Python. Here's a step-by-step plan:
115+
#
116+
# 1. Install the `websockets` library if you haven't done so already.
117+
# 2. Create a new WebSocket route in your FastAPI application.
118+
# 3. In the WebSocket route, accept a search query from the client.
119+
# 4. Use the search query to perform a full text search on the `nonsense` table in your PostgreSQL database.
120+
# 5. Send the search results back to the client through the WebSocket connection.
121+
#
122+
# Here's how you can implement this:
123+
#
124+
# ```python
125+
# import websockets
126+
# from fastapi import WebSocket
127+
# from sqlalchemy import text
128+
#
129+
# router = APIRouter()
130+
#
131+
# @router.websocket("/ws/nonsense")
132+
# async def websocket_endpoint(websocket: WebSocket):
133+
# await websocket.accept()
134+
# while True:
135+
# data = await websocket.receive_text()
136+
# query = text(f"""
137+
# SELECT * FROM nonsense
138+
# WHERE to_tsvector('english', description) @@ plainto_tsquery('english', :q)
139+
# """)
140+
# result = await db_session.execute(query, {"q": data})
141+
# await websocket.send_json(result.fetchall())
142+
# # ```
143+
#
144+
# This code creates a new WebSocket route at `/ws/nonsense`. When a client connects to this route and sends a message, the message is used as a search query in a full text search on the `nonsense` table. The search results are then sent back to the client through the WebSocket connection.
145+
#
146+
# Please note that this is a basic implementation and might need adjustments based on your specific needs. For example, you might want to add error handling, handle disconnections, or format the search results before sending them back to the client.
147+
#
148+
149+
# TODO: https://medium.com/@amitosh/full-text-search-fts-with-postgresql-and-sqlalchemy-edc436330a0c
150+
# TODO: https://www.postgresql.org/docs/13/textsearch-intro.html

app/config.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
import os
22

3-
from pydantic import PostgresDsn, RedisDsn
4-
from pydantic_settings import BaseSettings
3+
from pydantic import PostgresDsn, RedisDsn, computed_field
4+
from pydantic_core import MultiHostUrl
5+
from pydantic_settings import BaseSettings, SettingsConfigDict
56

67

78
class Settings(BaseSettings):
8-
asyncpg_url: PostgresDsn = os.getenv("SQL_URL")
9+
model_config = SettingsConfigDict(
10+
env_file=".env",
11+
env_ignore_empty=True,
12+
extra="ignore"
13+
)
14+
# asyncpg_url: PostgresDsn = os.getenv("SQL_URL")
915
redis_url: RedisDsn = os.getenv("REDIS_URL")
1016
jwt_algorithm: str = os.getenv("JWT_ALGORITHM")
1117
jwt_expire: int = os.getenv("JWT_EXPIRE")
1218

19+
SQL_USER: str
20+
SQL_PASS: str
21+
SQL_HOST: str
22+
SQL_DB: str
23+
24+
@computed_field
25+
@property
26+
def asyncpg_url(self) -> PostgresDsn:
27+
"""
28+
This is a computed field that generates a PostgresDsn URL for asyncpg.
29+
30+
The URL is built using the MultiHostUrl.build method, which takes the following parameters:
31+
- scheme: The scheme of the URL. In this case, it is "postgresql+asyncpg".
32+
- username: The username for the SQL database, retrieved from the SQL_USER environment variable.
33+
- password: The password for the SQL database, retrieved from the SQL_PASS environment variable.
34+
- host: The host of the SQL database, retrieved from the SQL_HOST environment variable.
35+
- path: The path of the SQL database, retrieved from the SQL_DB environment variable.
36+
37+
Returns:
38+
PostgresDsn: The constructed PostgresDsn URL for asyncpg.
39+
"""
40+
return MultiHostUrl.build(
41+
scheme="postgresql+asyncpg",
42+
username=self.SQL_USER,
43+
password=self.SQL_PASS,
44+
host=self.SQL_HOST,
45+
path=self.SQL_DB,
46+
)
47+
1348

1449
settings = Settings()

0 commit comments

Comments
 (0)