-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_handler.py
More file actions
36 lines (25 loc) · 1.04 KB
/
Copy pathlambda_handler.py
File metadata and controls
36 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""AWS Lambda entry point for the FastAPI server."""
from pathlib import Path
from typing import Any
from alembic import command
from alembic.config import Config
from loguru import logger
from mangum import Mangum
from db.seeds.languages import seed_languages_sync
from main import app
_alembic_config_path = Path(__file__).resolve().parent / "alembic.ini"
asgi_handler = Mangum(app)
def run_migrations_once() -> None:
logger.info("Running database migrations before Lambda request handling")
alembic_config = Config(str(_alembic_config_path))
command.upgrade(alembic_config, "head")
def handler(event: dict[str, Any], context: Any) -> dict[str, Any]:
"""Lambda handler"""
event_type = event.get("event_type")
if event_type == "Migration":
run_migrations_once()
return {"statusCode": 200, "body": "Migration run successfully"}
if event_type == "Seed_Languages":
seed_languages_sync()
return {"statusCode": 200, "body": "Languages seed successfully"}
return asgi_handler(event, context)