♻️(summary) reorganize API code
I totally reorganized the API code, to gain in clarity.
This commit is contained in:
committed by
aleb_the_flash
parent
b12b14b277
commit
35741a1bc1
@@ -167,7 +167,7 @@ celery:
|
||||
command:
|
||||
- "celery"
|
||||
- "-A"
|
||||
- "summary.celery_worker"
|
||||
- "summary.core.celery_worker"
|
||||
- "worker"
|
||||
- "--pool=solo"
|
||||
- "--loglevel=info"
|
||||
|
||||
@@ -280,7 +280,7 @@ celery:
|
||||
command:
|
||||
- "celery"
|
||||
- "-A"
|
||||
- "summary.celery_worker"
|
||||
- "summary.core.celery_worker"
|
||||
- "worker"
|
||||
- "--pool=solo"
|
||||
- "--loglevel=info"
|
||||
|
||||
@@ -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:
|
||||
|
||||
1
src/summary/summary/api/__init__.py
Normal file
1
src/summary/summary/api/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Summary Api package."""
|
||||
17
src/summary/summary/api/health.py
Normal file
17
src/summary/summary/api/health.py
Normal 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
|
||||
9
src/summary/summary/api/main.py
Normal file
9
src/summary/summary/api/main.py
Normal 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"])
|
||||
1
src/summary/summary/api/route/__init__.py
Normal file
1
src/summary/summary/api/route/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Summary Api Route package."""
|
||||
34
src/summary/summary/api/route/tasks.py
Normal file
34
src/summary/summary/api/route/tasks.py
Normal 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}
|
||||
1
src/summary/summary/core/__init__.py
Normal file
1
src/summary/summary/core/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Summary Core package."""
|
||||
@@ -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__)
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user