Skip to content

Commit

Permalink
add webhook distributer
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixTJDietrich committed Aug 1, 2024
1 parent 1111df3 commit e00cc04
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/webhook-distributor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Webhook Distributor

A simple webhook distributor that forwards incoming webhooks to multiple destinations.

## Usage

### Register a destination

To register a destination, send a POST request to `/register` with the following JSON payload:

```json
{
"url": "<destination url>"
}
```

### Send a webhook

To send a webhook, send a POST request to `/webhook`. The webhook will be forwarded to all registered destinations.
43 changes: 43 additions & 0 deletions server/webhook-distributor/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Set
from fastapi import FastAPI, Request
from pydantic import BaseModel
import httpx

app = FastAPI()

# In-memory list to store registered destinations
destinations: Set[str] = set()

# TODO: Get existing destinations, i.e. deployments and preview deployments -> Ask in Coolify Discord
# For now each server has to register itself

class Destination(BaseModel):
url: str


@app.post("/register")
def register_destination(destination: Destination):
destinations.add(destination.url)
return { "message": "Destination registered successfully" }


@app.post("/webhook")
async def send_webhook(request: Request):
payload = await request.json()
failed_destinations = []

async with httpx.AsyncClient() as client:
for destination in destinations:
try:
await client.post(destination, json=payload)
except:
failed_destinations.append(destination)

for destination in failed_destinations:
destinations.remove(destination)

return {
"message": "Webhook sent successfully",
"failed": len(failed_destinations),
"success_destinations": len(destinations),
}
2 changes: 2 additions & 0 deletions server/webhook-distributor/nixpacks.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[start]
cmd = "fastapi run"
1 change: 1 addition & 0 deletions server/webhook-distributor/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastapi==0.111.1

0 comments on commit e00cc04

Please sign in to comment.