diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dac7cba --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.venv/ +__pycache__/ +*.pyc +.env +*.egg-info/ +dist/ +build/ +.pytest_cache/ diff --git a/app/core/__init__.py b/app/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 0000000..aa11988 --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,32 @@ +from sqlalchemy.engine import URL +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + SQL_SERVER_HOST: str + SQL_SERVER_PORT: int = 1433 + SQL_SERVER_DATABASE: str + SQL_SERVER_USERNAME: str + SQL_SERVER_PASSWORD: str + SQL_SERVER_DRIVER: str = "{ODBC Driver 18 for SQL Server}" + SQL_SERVER_TRUST_SERVER_CERTIFICATE: str = "yes" + + @property + def database_url(self) -> URL: + return URL.create( + "mssql+pyodbc", + username=self.SQL_SERVER_USERNAME, + password=self.SQL_SERVER_PASSWORD, + host=self.SQL_SERVER_HOST, + port=self.SQL_SERVER_PORT, + database=self.SQL_SERVER_DATABASE, + query={ + "driver": self.SQL_SERVER_DRIVER.strip("{}"), + "TrustServerCertificate": self.SQL_SERVER_TRUST_SERVER_CERTIFICATE, + }, + ) + + model_config = SettingsConfigDict(env_file=".env", extra="ignore") + + +settings = Settings() diff --git a/app/core/database.py b/app/core/database.py new file mode 100644 index 0000000..580e772 --- /dev/null +++ b/app/core/database.py @@ -0,0 +1,25 @@ +from typing import Generator + +from sqlalchemy import create_engine +from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker + +from app.core.config import settings + +engine = create_engine( + settings.database_url, + pool_pre_ping=True, + pool_recycle=1800, +) +SessionLocal = sessionmaker(bind=engine) + + +class Base(DeclarativeBase): + pass + + +def get_db() -> Generator[Session, None, None]: + db = SessionLocal() + try: + yield db + finally: + db.close() diff --git a/requirements.txt b/requirements.txt index 50b0ab1..90f2f39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,8 @@ fastapi>=0.115.0 uvicorn[standard]>=0.34.0 +sqlalchemy>=2.0.0 +pyodbc>=5.2.0 +pydantic-settings>=2.0.0 +python-dotenv>=1.0.0 +pytest>=8.0.0 +httpx>=0.28.0