-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1111df3
commit e00cc04
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[start] | ||
cmd = "fastapi run" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fastapi==0.111.1 |