Skip to content

Commit

Permalink
✨ Add redis configuration (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 authored Jul 23, 2024
1 parent c976dee commit 7bf790d
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 5 deletions.
19 changes: 18 additions & 1 deletion backend/app/api/deps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from collections.abc import Generator
from typing import Annotated
from typing import Annotated, Any

import jwt
import redis
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jwt.exceptions import InvalidTokenError
Expand All @@ -28,6 +29,22 @@ def get_db() -> Generator[Session, None, None]:
TokenDep = Annotated[str, Depends(reusable_oauth2)]


def get_redis() -> Generator["redis.Redis[Any]", None, None]:
pool = redis.ConnectionPool(
host=settings.REDIS_SERVER,
port=settings.REDIS_PORT,
db=settings.REDIS_DB,
password=settings.REDIS_PASSWORD,
)

redis_instance = redis.Redis(connection_pool=pool)

yield redis_instance


RedisDep = Annotated["redis.Redis[Any]", Depends(get_redis)]


def get_current_user(session: SessionDep, token: TokenDep) -> User:
try:
payload = jwt.decode(
Expand Down
22 changes: 21 additions & 1 deletion backend/app/api/routes/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from pydantic.networks import EmailStr

from app.api.deps import get_first_superuser
from app.api.deps import RedisDep, get_first_superuser
from app.models import Message
from app.utils import generate_test_email, send_email

Expand All @@ -24,3 +25,22 @@ def test_email(email_to: EmailStr) -> Message:
html_content=email_data.html_content,
)
return Message(message="Test email sent")


class HealthCheckResponse(BaseModel):
redis: bool


@router.get("/health-check/")
async def health_check(redis: RedisDep) -> HealthCheckResponse:
"""
Health check.
"""
is_redis_available: bool = False

try:
is_redis_available = redis.ping()
except Exception:
pass

return HealthCheckResponse(redis=is_redis_available)
5 changes: 5 additions & 0 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
path=self.POSTGRES_DB,
)

REDIS_SERVER: str
REDIS_PORT: int = 6379
REDIS_PASSWORD: str | None = None
REDIS_DB: int = 0

SMTP_TLS: bool = True
SMTP_SSL: bool = False
SMTP_PORT: int = 587
Expand Down
Loading

0 comments on commit 7bf790d

Please sign in to comment.