♻️(summary) reorganize API code

I totally reorganized the API code, to gain in clarity.
This commit is contained in:
lebaudantoine
2024-11-30 00:17:12 +01:00
committed by aleb_the_flash
parent b12b14b277
commit 35741a1bc1
14 changed files with 83 additions and 49 deletions

View File

@@ -167,7 +167,7 @@ celery:
command:
- "celery"
- "-A"
- "summary.celery_worker"
- "summary.core.celery_worker"
- "worker"
- "--pool=solo"
- "--loglevel=info"

View File

@@ -280,7 +280,7 @@ celery:
command:
- "celery"
- "-A"
- "summary.celery_worker"
- "summary.core.celery_worker"
- "worker"
- "--pool=solo"
- "--loglevel=info"

View File

@@ -19,7 +19,7 @@ services:
celery_worker:
container_name: celery_worker
build: .
command: celery -A summary.celery_worker worker --pool=solo --loglevel=debug
command: celery -A summary.core.celery_worker worker --pool=solo --loglevel=debug
volumes:
- .:/app
depends_on:

View File

@@ -0,0 +1 @@
"""Summary Api package."""

View File

@@ -0,0 +1,17 @@
"""API routes related to application health checking."""
from fastapi import APIRouter
router = APIRouter()
@router.get("/__heartbeat__")
async def heartbeat():
"""Health check endpoint for monitoring."""
return
@router.get("/__lbheartbeat__")
async def lbheartbeat():
"""Health check endpoint for load balancer."""
return

View File

@@ -0,0 +1,9 @@
"""API routes."""
from fastapi import APIRouter, Depends
from summary.api.route import tasks
from summary.core.security import verify_token
api_router = APIRouter(dependencies=[Depends(verify_token)])
api_router.include_router(tasks.router, tags=["tasks"])

View File

@@ -0,0 +1 @@
"""Summary Api Route package."""

View File

@@ -0,0 +1,34 @@
"""API routes related to application tasks."""
from celery.result import AsyncResult
from fastapi import APIRouter
from pydantic import BaseModel
from summary.core.celery_worker import process_audio_transcribe_summarize
class TaskCreation(BaseModel):
"""Task data."""
filename: str
email: str
sub: str
router = APIRouter(prefix="/tasks")
@router.post("/")
async def create_task(request: TaskCreation):
"""Create a task."""
task = process_audio_transcribe_summarize.delay(
request.filename, request.email, request.sub
)
return {"id": task.id, "message": "Task created"}
@router.get("/{task_id}")
async def get_task_status(task_id: str):
"""Check task status by ID."""
task = AsyncResult(task_id)
return {"id": task_id, "status": task.status}

View File

@@ -0,0 +1 @@
"""Summary Core package."""

View File

@@ -12,11 +12,10 @@ from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
from .config import Settings
from .prompt import get_instructions
settings = Settings()
from summary.core.config import get_settings
from summary.core.prompt import get_instructions
settings = get_settings()
logger = get_task_logger(__name__)

View File

@@ -10,8 +10,10 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""Configuration settings loaded from environment variables and .env file."""
app_name: str = "Awesome API"
model_config = SettingsConfigDict(env_file=".env")
app_name: str = "app"
app_api_v1_str: str = "/api/v1"
app_api_token: str
# Celery settings

View File

@@ -1,46 +1,16 @@
"""Application endpoint."""
"""Application."""
from celery.result import AsyncResult
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from fastapi import FastAPI
from .celery_worker import process_audio_transcribe_summarize
from .security import verify_token
from summary.api import health
from summary.api.main import api_router
from summary.core.config import get_settings
app = FastAPI()
settings = get_settings()
app = FastAPI(
title=settings.app_name,
)
@app.get("/__heartbeat__")
async def heartbeat():
"""Health check endpoint for monitoring."""
return {"status": "ok"}
@app.get("/__lbheartbeat__")
async def lbheartbeat():
"""Health check endpoint for load balancer."""
return {"status": "ok"}
class NotificationRequest(BaseModel):
"""Notification data."""
filename: str
email: str
sub: str
@app.post("/push")
async def notify(request: NotificationRequest, token: str = Depends(verify_token)):
"""Push a notification."""
task = process_audio_transcribe_summarize.delay(
request.filename, request.email, request.sub
)
return {"task_id": task.id, "message": "Notification sent"}
@app.get("/status/{task_id}")
async def get_status(task_id: str, token: str = Depends(verify_token)):
"""Check task status by ID."""
task = AsyncResult(task_id)
return {"task_id": task_id, "status": task.status}
app.include_router(api_router, prefix=settings.app_api_v1_str)
app.include_router(health.router)