Skip to content

Commit ba0fdae

Browse files
authored
Cache API settings helper (#23)
* Cache API settings helper * Reuse cached Hygraph service
1 parent 2942907 commit ba0fdae

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

backend/api/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Configuration settings for the FastAPI application."""
22

3+
from functools import lru_cache
34
from typing import Any, Dict
45

56
from pydantic import Field
@@ -45,6 +46,13 @@ class Settings(BaseSettings):
4546
)
4647

4748

49+
@lru_cache()
50+
def get_settings() -> Settings:
51+
"""Return a cached instance of :class:`Settings`."""
52+
53+
return Settings()
54+
55+
4856
def get_fastapi_settings() -> Dict[str, Any]:
4957
"""Get FastAPI application settings.
5058

backend/api/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlalchemy import create_engine
88
from sqlalchemy.orm import DeclarativeBase, sessionmaker
99

10-
from api.config import Settings
10+
from api.config import get_settings
1111

1212

1313
class Base(DeclarativeBase):

backend/api/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from fastapi.responses import JSONResponse
99
from starlette.middleware.cors import CORSMiddleware
1010

11-
from api.config import Settings
11+
from api.config import get_settings
1212
from api.routes import router as api_router
1313
from api.routes_materials import router as materials_router
1414
from api.routes_modules import router as modules_router
@@ -24,7 +24,7 @@
2424
logger = logging.getLogger(__name__)
2525

2626
# Initialize settings
27-
settings = Settings()
27+
settings = get_settings()
2828

2929
# Create the FastAPI app
3030
app = FastAPI(

backend/api/routes_sync.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sqlalchemy.exc import IntegrityError
2020
from sqlalchemy.orm import Session
2121

22+
from api.config import get_settings
2223
from api.db import get_db
2324
from api.models import SyncEvent
2425
from api.security import require_write_token, validate_hygraph_request
@@ -32,7 +33,15 @@
3233
logger = logging.getLogger(__name__)
3334
router = APIRouter(prefix="/api/sync", tags=["sync"])
3435

36+
# Using hygraph_token as webhook secret in MVP; adjust if separate secret provided
37+
settings = get_settings()
38+
_hygraph_service = HygraphService(webhook_secret=settings.hygraph_token)
3539

40+
41+
def get_hygraph_service() -> HygraphService:
42+
"""Provide the shared Hygraph service instance."""
43+
44+
return _hygraph_service
3645
def _error_envelope(code: str, message: str, details: Optional[dict] = None) -> Dict[str, Any]:
3746
return {"ok": False, "error": {"code": code, "message": message, "details": details or {}}}
3847

0 commit comments

Comments
 (0)