Skip to content

Commit 59e2a0e

Browse files
committed
refactor project settings
1 parent d23c2f4 commit 59e2a0e

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

app/config.py

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,20 @@
11
import os
22
from functools import lru_cache
33

4-
from pydantic import BaseSettings
5-
6-
from app.utils import get_logger
7-
8-
logger = get_logger(__name__)
4+
from pydantic import BaseSettings, PostgresDsn
95

106

117
class Settings(BaseSettings):
12-
"""
13-
14-
BaseSettings, from Pydantic, validates the data so that when we create an instance of Settings,
15-
environment and testing will have types of str and bool, respectively.
16-
17-
Parameters:
18-
pg_user (str):
19-
pg_pass (str):
20-
pg_database: (str):
21-
pg_test_database: (str):
22-
asyncpg_url: AnyUrl:
23-
asyncpg_test_url: AnyUrl:
24-
25-
Returns:
26-
instance of Settings
27-
28-
"""
29-
30-
pg_user: str = os.getenv("SQL_USER", "")
31-
pg_pass: str = os.getenv("POSTGRES_PASSWORD", "")
32-
pg_host: str = os.getenv("SQL_HOST", "")
33-
pg_database: str = os.getenv("SQL_DB", "")
34-
asyncpg_url: str = (
35-
f"postgresql+asyncpg://{pg_user}:{pg_pass}@{pg_host}:5432/{pg_database}"
8+
asyncpg_url: PostgresDsn = PostgresDsn.build(
9+
scheme="postgresql+asyncpg",
10+
user=os.getenv("SQL_USER"),
11+
password=os.getenv("POSTGRES_PASSWORD"),
12+
host=os.getenv("SQL_HOST"),
13+
port="5432",
14+
path=f"/{os.getenv('SQL_DB') or ''}",
3615
)
3716

38-
jwt_secret_key: str = os.getenv("SECRET_KEY", "")
39-
jwt_algorithm: str = os.getenv("ALGORITHM", "")
40-
jwt_access_toke_expire_minutes: int = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 1)
41-
4217

4318
@lru_cache
4419
def get_settings():
45-
logger.info("Loading config settings from the environment...")
4620
return Settings()

app/database.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from collections.abc import AsyncGenerator
22

3-
from fastapi.encoders import jsonable_encoder
43
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
54
from sqlalchemy.orm import sessionmaker
65

@@ -10,13 +9,11 @@
109
logger = get_logger(__name__)
1110

1211
global_settings = config.get_settings()
13-
url = global_settings.asyncpg_url
1412

1513
engine = create_async_engine(
16-
url,
14+
global_settings.asyncpg_url,
1715
future=True,
1816
echo=True,
19-
json_serializer=jsonable_encoder,
2017
)
2118

2219
# expire_on_commit=False will prevent attributes from being expired

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
logger = get_logger(__name__)
99

10-
app = FastAPI(title="Stuff And Nonsense API", version="0.4")
10+
app = FastAPI(title="Stuff And Nonsense API", version="0.5")
1111

1212
app.include_router(stuff_router)
1313
app.include_router(nonsense_router)

0 commit comments

Comments
 (0)