~nora/fastapi-sandbox

3a2a89385ca64f73008eeb5ba87d5563a8ee41df — nora 1 year, 3 months ago 2c5bd74
Add settings
2 files changed, 24 insertions(+), 5 deletions(-)

A app/config.py
M app/database.py
A app/config.py => app/config.py +20 -0
@@ 0,0 1,20 @@
from functools import lru_cache

from pydantic import BaseSettings, EmailStr


class Settings(BaseSettings):
    app_name: str = "fastapi-sandbox"
    admin_email: EmailStr | None
    database_url: str = "sqlite:///db.sqlite3"

    class Config:
        env_file = ".env"


@lru_cache()
def get_settings():
    return Settings()


settings = get_settings()

M app/database.py => app/database.py +4 -5
@@ 1,12 1,11 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///db.sqlite3"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
from .config import settings

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
CHECK_SAME_THREAD = False if settings.database_url.startswith("sqlite") else True

engine = create_engine(settings.database_url, connect_args={"check_same_thread": CHECK_SAME_THREAD})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()