#!/usr/bin/env python3 import os import json import uuid import uvicorn import pika from fastapi import FastAPI, Body, Header, HTTPException from fastapi.middleware.cors import CORSMiddleware from worker import main as rabbit_worker app = FastAPI() API_KEY = os.getenv("SECRET_KEY") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/") async def root(): return {"status": "ok", "message": "API is running"} @app.post("/process") async def process_pdf( input_json: dict = Body(...), x_api_key: str = Header(None, alias="X-API-Key") ): if not x_api_key: raise HTTPException(status_code=401, detail="API key is missing") if x_api_key != API_KEY: raise HTTPException(status_code=401, detail="Invalid API key") # Connect to RabbitMQ rabbit_url = os.getenv("RABBITMQ_URL") connection = pika.BlockingConnection(pika.URLParameters(rabbit_url)) channel = connection.channel() channel.queue_declare(queue="ml_server", durable=True) channel.basic_publish( exchange="", routing_key="ml_server", body=json.dumps(input_json), properties=pika.BasicProperties( headers={"process": "topic_extraction"} ) ) connection.close() return { "message": "Job queued", "request_id": input_json.get("headers", {}).get("request_id", str(uuid.uuid4())) } if __name__ == "__main__": rabbit_worker() uvicorn.run(app, host="0.0.0.0", port=8000)