Spaces:
Paused
Paused
File size: 4,834 Bytes
1bc3f18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, RedirectResponse
import json
from datetime import datetime
app = FastAPI()
# In-memory storage
webhook_logs = []
@app.post("/webhook")
async def webhook(request: Request):
body = await request.body()
try:
body_json = json.loads(body)
body_pretty = json.dumps(body_json, indent=2)
except Exception:
body_pretty = body.decode("utf-8", errors="ignore")
webhook_logs.insert(0, {
"time": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"),
"headers": dict(request.headers),
"body": body_pretty
})
del webhook_logs[50:]
return {"status": "ok"}
@app.post("/clear")
async def clear_logs():
webhook_logs.clear()
return RedirectResponse("/", status_code=303)
@app.get("/", response_class=HTMLResponse)
async def dashboard():
items = ""
for i, log in enumerate(webhook_logs):
items += f"""
<div class="entry">
<div class="entry-header">
<span class="badge">#{i + 1}</span>
<span class="time">{log["time"]}</span>
</div>
<details>
<summary>Headers</summary>
<pre>{json.dumps(log["headers"], indent=2)}</pre>
</details>
<details open>
<summary>Body</summary>
<pre>{log["body"]}</pre>
</details>
</div>
"""
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>Webhook Viewer</title>
<meta http-equiv="refresh" content="3">
<style>
body {{
margin: 0;
font-family: system-ui, -apple-system, BlinkMacSystemFont;
background: #0b1020;
color: #e5e7eb;
}}
header {{
position: sticky;
top: 0;
background: #020617;
padding: 15px 20px;
border-bottom: 1px solid #1e293b;
display: flex;
justify-content: space-between;
align-items: center;
}}
header h1 {{
margin: 0;
font-size: 20px;
}}
.controls {{
display: flex;
gap: 10px;
align-items: center;
}}
.count {{
background: #1e293b;
padding: 4px 10px;
border-radius: 999px;
font-size: 12px;
}}
button {{
background: #dc2626;
border: none;
color: white;
padding: 6px 12px;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
}}
button:hover {{
background: #b91c1c;
}}
main {{
padding: 20px;
}}
.entry {{
background: #020617;
border: 1px solid #1e293b;
border-radius: 10px;
padding: 15px;
margin-bottom: 16px;
}}
.entry-header {{
display: flex;
gap: 10px;
align-items: center;
margin-bottom: 10px;
}}
.badge {{
background: #2563eb;
color: white;
padding: 3px 8px;
border-radius: 6px;
font-size: 12px;
}}
.time {{
font-size: 12px;
color: #94a3b8;
}}
details summary {{
cursor: pointer;
font-weight: 600;
margin: 8px 0;
}}
pre {{
background: #020617;
border: 1px solid #1e293b;
padding: 10px;
border-radius: 6px;
overflow-x: auto;
color: #38bdf8;
font-size: 13px;
}}
.empty {{
text-align: center;
color: #64748b;
margin-top: 50px;
}}
</style>
</head>
<body>
<header>
<h1>📡 Webhook Viewer</h1>
<div class="controls">
<span class="count">{len(webhook_logs)} requests</span>
<form method="post" action="/clear">
<button type="submit">Clear</button>
</form>
</div>
</header>
<main>
{items if items else "<p class='empty'>No webhooks received yet</p>"}
</main>
</body>
</html>
"""
return html |