57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks
|
|
import logging
|
|
import os
|
|
import json
|
|
from queue_manager import JobQueue
|
|
|
|
# Logging Setup
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger("connector-webhook")
|
|
|
|
app = FastAPI(title="SuperOffice Connector Webhook", version="2.0")
|
|
queue = JobQueue()
|
|
|
|
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET", "changeme")
|
|
|
|
@app.post("/webhook")
|
|
async def receive_webhook(request: Request, background_tasks: BackgroundTasks):
|
|
"""
|
|
Endpoint for SuperOffice Webhooks.
|
|
"""
|
|
# 1. Verify Secret (Basic Security)
|
|
# SuperOffice puts signature in headers, but for custom webhook we might just use query param or header
|
|
# Let's assume for now a shared secret in header 'X-SuperOffice-Signature' or similar
|
|
# Or simply a secret in the URL: /webhook?token=...
|
|
|
|
token = request.query_params.get("token")
|
|
if token != WEBHOOK_SECRET:
|
|
logger.warning(f"Invalid webhook token attempt: {token}")
|
|
raise HTTPException(403, "Invalid Token")
|
|
|
|
try:
|
|
payload = await request.json()
|
|
logger.info(f"Received webhook payload: {payload}")
|
|
|
|
event_type = payload.get("Event", "unknown")
|
|
|
|
# Add to local Queue
|
|
queue.add_job(event_type, payload)
|
|
|
|
return {"status": "queued"}
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing webhook: {e}", exc_info=True)
|
|
raise HTTPException(500, "Internal Server Error")
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|
|
|
|
@app.get("/stats")
|
|
def stats():
|
|
return queue.get_stats()
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("webhook_app:app", host="0.0.0.0", port=8000, reload=True)
|