Boilerplate FastAPI & Dockerfile + NGINX

This commit is contained in:
2025-09-22 17:58:16 +01:00
parent f3fbed5298
commit bf600b8e42
35 changed files with 650 additions and 0 deletions

33
app/api/deps.py Normal file
View File

@@ -0,0 +1,33 @@
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer
from jose import jwt, JWTError
from app.core.config import settings
from app.db.session import get_session
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.user import User
from sqlalchemy import select
bearer = HTTPBearer()
async def get_db() -> AsyncSession:
async for s in get_session():
yield s
async def get_current_user(token=Depends(bearer), db: AsyncSession = Depends(get_db)) -> User:
try:
payload = jwt.decode(token.credentials, settings.SECRET_KEY, algorithms=["HS256"])
email: str = payload.get("sub")
except JWTError:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
res = await db.execute(select(User).where(User.email == email))
user = res.scalar_one_or_none()
if not user:
raise HTTPException(status_code=401, detail="User not found")
return user
def require_role(*roles: str):
async def _dep(user: User = Depends(get_current_user)):
if user.role not in roles:
raise HTTPException(status_code=403, detail="Insufficient role")
return user
return _dep