diff --git a/.env.sample b/.env.sample index 9c39b19..9ee7bc0 100644 --- a/.env.sample +++ b/.env.sample @@ -6,12 +6,12 @@ ENV=dev # dev, prod # ========================= # DATABASE CONFIGURATION # ========================= -# URL de connexion SQLAlchemy -# Pour PyMySQL (synchrone) -DATABASE_URL=mysql+pymysql://user:password@localhost:3306/db_name - -# Pour aiomysql (async) -# DATABASE_URL=mysql+aiomysql://user:password@localhost:3306/mokpyo +# connexion SQLAlchemy +DATABASE_HOST=localhost +DATABASE_PORT=3306 +DATABASE_USER=user +DATABASE_PASSWORD=password +DATABASE_NAME=mokpyo # Pooling / Options SQLAlchemy (optionnel) # MAX_CONNECTIONS=10 diff --git a/.gitignore b/.gitignore index d2bb9d1..fc5bc06 100644 --- a/.gitignore +++ b/.gitignore @@ -179,3 +179,6 @@ cython_debug/ # git .git/ + +# custom tests files +*.test.py \ No newline at end of file diff --git a/app/api/v1/endpoints.py b/app/api/v1/endpoints.py index e8da2cb..98fe8b9 100644 --- a/app/api/v1/endpoints.py +++ b/app/api/v1/endpoints.py @@ -13,32 +13,3 @@ router = APIRouter() @router.get("/ping", tags=["Test"]) def ping(): return {"message": "pong"} - -# ========================= -# EXEMPLE UTILISATEUR -# ========================= -@router.get("/me", tags=["User"]) -def read_current_user(current_user: dict = Depends(get_current_user)): - """ - Retourne les infos de l'utilisateur connecté - """ - return {"user": current_user} - -# ========================= -# EXEMPLE AUTH -# ========================= -@router.post("/hash-password", tags=["Auth"]) -def test_hash_password(password: str): - """ - Exemple simple pour hasher un mot de passe - """ - hashed = hash_password(password) - return {"password": password, "hashed": hashed} - -@router.post("/verify-password", tags=["Auth"]) -def test_verify_password(password: str, hashed: str): - """ - Vérifie qu'un mot de passe correspond à un hash - """ - valid = verify_password(password, hashed) - return {"valid": valid} diff --git a/app/core/config.py b/app/core/config.py index 58c722c..ce7b81c 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -10,7 +10,12 @@ class Settings(BaseSettings): # ========================= # DATABASE # ========================= - DATABASE_URL: str + DATABASE_HOST: str + DATABASE_PORT: int + DATABASE_USER: str + DATABASE_PASSWORD: str + DATABASE_NAME: str + DATABASE_DRIVER: str # ========================= # FASTAPI @@ -61,6 +66,10 @@ class Settings(BaseSettings): # ========================= ORJSON_STRICT: bool = True + @property + def database_url(self) -> str: + return f"mysql+://{self.DATABASE_USER}:{self.DATABASE_PASSWORD}@{self.DATABASE_HOST}:{self.DATABASE_PORT}/{self.DATABASE_NAME}" + @property def access_token_expire(self) -> timedelta: return timedelta(minutes=self.ACCESS_TOKEN_EXPIRE_MINUTES) @@ -70,6 +79,4 @@ class Settings(BaseSettings): env_file_encoding = "utf-8" # Instance globale -settings = Settings() - -print("test") \ No newline at end of file +settings = Settings() \ No newline at end of file diff --git a/app/db/__init__.py b/app/db/__init__.py index c2d69a0..fda1ab0 100644 --- a/app/db/__init__.py +++ b/app/db/__init__.py @@ -8,3 +8,4 @@ __version__ = "1.0.0" from .engine import engine, async_engine from .session import SessionLocal, AsyncSessionLocal, get_db, get_async_db +from .models import Base diff --git a/app/db/engine.py b/app/db/engine.py index 99ee3c3..a9a7ad9 100644 --- a/app/db/engine.py +++ b/app/db/engine.py @@ -1,13 +1,13 @@ from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine -from sqlalchemy import create_engine +from sqlalchemy import create_engine, Engine from app.core.config import settings # ========================= # SYNCHRONOUS ENGINE # ========================= # Pour opérations sync classiques -engine = create_engine( - settings.DATABASE_URL.replace("+aiomysql", ""), # remove async part if present +engine: Engine = create_engine( + settings.database_url.replace('', "pymysql"), echo=settings.DEBUG, future=True ) @@ -17,7 +17,7 @@ engine = create_engine( # ========================= # Pour opérations async avec async SQLAlchemy async_engine: AsyncEngine = create_async_engine( - settings.DATABASE_URL if "+aiomysql" in settings.DATABASE_URL else settings.DATABASE_URL.replace("mysql+pymysql", "mysql+aiomysql"), + settings.database_url.replace('', "aiomysql"), echo=settings.DEBUG, future=True )