initial commit
This commit is contained in:
0
src/glitchup_bot/api/__init__.py
Normal file
0
src/glitchup_bot/api/__init__.py
Normal file
17
src/glitchup_bot/api/app.py
Normal file
17
src/glitchup_bot/api/app.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from glitchup_bot.api.webhook import router as webhook_router
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
application = FastAPI(title="GlitchUp Bot", version="0.1.0")
|
||||
application.include_router(webhook_router)
|
||||
|
||||
@application.get("/health")
|
||||
async def health() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
return application
|
||||
|
||||
|
||||
app = create_app()
|
||||
25
src/glitchup_bot/api/schemas.py
Normal file
25
src/glitchup_bot/api/schemas.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class WebhookField(BaseModel):
|
||||
title: str
|
||||
value: str
|
||||
short: bool = False
|
||||
|
||||
|
||||
class WebhookAttachment(BaseModel):
|
||||
title: str
|
||||
title_link: str | None = None
|
||||
text: str | None = None
|
||||
image_url: str | None = None
|
||||
color: str | None = None
|
||||
fields: list[WebhookField] | None = None
|
||||
mrkdown_in: list[str] | None = None
|
||||
|
||||
|
||||
class GlitchTipWebhookPayload(BaseModel):
|
||||
text: str
|
||||
attachments: list[WebhookAttachment] = Field(default_factory=list)
|
||||
|
||||
def is_uptime_alert(self) -> bool:
|
||||
return "uptime" in self.text.lower()
|
||||
25
src/glitchup_bot/api/webhook.py
Normal file
25
src/glitchup_bot/api/webhook.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Header, HTTPException, Request
|
||||
|
||||
from glitchup_bot.config import settings
|
||||
from glitchup_bot.services.alert_processor import process_webhook_payload
|
||||
|
||||
router = APIRouter(prefix="/webhooks", tags=["webhooks"])
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.post("/glitchtip")
|
||||
async def glitchtip_webhook(
|
||||
request: Request,
|
||||
x_webhook_secret: str | None = Header(None),
|
||||
):
|
||||
if settings.webhook_secret and x_webhook_secret != settings.webhook_secret:
|
||||
raise HTTPException(status_code=403, detail="Invalid webhook secret")
|
||||
|
||||
payload = await request.json()
|
||||
logger.info("Received GlitchTip webhook: %s", payload.get("text", "unknown"))
|
||||
|
||||
await process_webhook_payload(payload)
|
||||
|
||||
return {"status": "accepted"}
|
||||
Reference in New Issue
Block a user