feat: implement chat messaging and Redis cache

This commit is contained in:
sinveria
2026-11-04 18:44:55 +03:00
parent e0e654dcf5
commit 527f0e8d74
8 changed files with 643 additions and 24 deletions
+17 -23
View File
@@ -1,14 +1,15 @@
from typing import Generator
from fastapi import Depends, Header, HTTPException
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from sqlalchemy.orm import Session
from app.db.session import SessionLocal
from app.db.models import User
from app.services.auth_service import AuthService, InvalidSessionError
def get_db() -> Generator[Session, None, None]:
bearer_scheme = HTTPBearer(auto_error=False)
def get_db():
db = SessionLocal()
try:
yield db
@@ -17,30 +18,23 @@ def get_db() -> Generator[Session, None, None]:
def get_current_user(
authorization: str | None = Header(default=None),
credentials: HTTPAuthorizationCredentials | None = Depends(bearer_scheme),
db: Session = Depends(get_db),
) -> User:
if not authorization or not authorization.startswith("Bearer "):
):
if credentials is None or credentials.scheme.lower() != "bearer":
raise HTTPException(
status_code=401,
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Bearer token required",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
)
token = authorization[7:].strip()
if not token:
raise HTTPException(
status_code=401,
detail="Bearer token required",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
headers={"WWW-Authenticate": "Bearer"},
)
try:
return AuthService(db).authenticate(token)
user = AuthService(db).authenticate(credentials.credentials)
return user
except InvalidSessionError:
raise HTTPException(
status_code=401,
detail="Invalid or expired session",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or expired token",
headers={"WWW-Authenticate": "Bearer"},
) from None