mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
refactor project settings
This commit is contained in:
parent
d23c2f46cb
commit
59e2a0e878
@ -1,46 +1,20 @@
|
|||||||
import os
|
import os
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
from pydantic import BaseSettings
|
from pydantic import BaseSettings, PostgresDsn
|
||||||
|
|
||||||
from app.utils import get_logger
|
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
"""
|
asyncpg_url: PostgresDsn = PostgresDsn.build(
|
||||||
|
scheme="postgresql+asyncpg",
|
||||||
BaseSettings, from Pydantic, validates the data so that when we create an instance of Settings,
|
user=os.getenv("SQL_USER"),
|
||||||
environment and testing will have types of str and bool, respectively.
|
password=os.getenv("POSTGRES_PASSWORD"),
|
||||||
|
host=os.getenv("SQL_HOST"),
|
||||||
Parameters:
|
port="5432",
|
||||||
pg_user (str):
|
path=f"/{os.getenv('SQL_DB') or ''}",
|
||||||
pg_pass (str):
|
|
||||||
pg_database: (str):
|
|
||||||
pg_test_database: (str):
|
|
||||||
asyncpg_url: AnyUrl:
|
|
||||||
asyncpg_test_url: AnyUrl:
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
instance of Settings
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
pg_user: str = os.getenv("SQL_USER", "")
|
|
||||||
pg_pass: str = os.getenv("POSTGRES_PASSWORD", "")
|
|
||||||
pg_host: str = os.getenv("SQL_HOST", "")
|
|
||||||
pg_database: str = os.getenv("SQL_DB", "")
|
|
||||||
asyncpg_url: str = (
|
|
||||||
f"postgresql+asyncpg://{pg_user}:{pg_pass}@{pg_host}:5432/{pg_database}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
jwt_secret_key: str = os.getenv("SECRET_KEY", "")
|
|
||||||
jwt_algorithm: str = os.getenv("ALGORITHM", "")
|
|
||||||
jwt_access_toke_expire_minutes: int = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 1)
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
def get_settings():
|
def get_settings():
|
||||||
logger.info("Loading config settings from the environment...")
|
|
||||||
return Settings()
|
return Settings()
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
from collections.abc import AsyncGenerator
|
from collections.abc import AsyncGenerator
|
||||||
|
|
||||||
from fastapi.encoders import jsonable_encoder
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
@ -10,13 +9,11 @@ from app.utils import get_logger
|
|||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
global_settings = config.get_settings()
|
global_settings = config.get_settings()
|
||||||
url = global_settings.asyncpg_url
|
|
||||||
|
|
||||||
engine = create_async_engine(
|
engine = create_async_engine(
|
||||||
url,
|
global_settings.asyncpg_url,
|
||||||
future=True,
|
future=True,
|
||||||
echo=True,
|
echo=True,
|
||||||
json_serializer=jsonable_encoder,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# expire_on_commit=False will prevent attributes from being expired
|
# expire_on_commit=False will prevent attributes from being expired
|
||||||
|
@ -7,7 +7,7 @@ from app.utils import get_logger
|
|||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
app = FastAPI(title="Stuff And Nonsense API", version="0.4")
|
app = FastAPI(title="Stuff And Nonsense API", version="0.5")
|
||||||
|
|
||||||
app.include_router(stuff_router)
|
app.include_router(stuff_router)
|
||||||
app.include_router(nonsense_router)
|
app.include_router(nonsense_router)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user