commit e9ad2f7c64b72e17bb8e5be9ecdf7285c9c35a13 Author: sHa Date: Tue Mar 12 16:13:30 2024 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4974bf7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +.mypy_cache +.pytest_cache diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1905e55 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.12-alpine + +WORKDIR /app +ENV PYTHONPATH=/app + +COPY ./requirements.txt ./requirements.txt + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..4c8ca44 --- /dev/null +++ b/app.py @@ -0,0 +1,61 @@ +from fastapi import FastAPI, Request, Response, status +from datetime import datetime +import json + +app = FastAPI() + +LIMIT = 10 + +def store_last_request(last_request, filename='requests.json'): + try: + with open(filename, 'r') as f: + data = json.load(f) + except (FileNotFoundError, json.JSONDecodeError): + data = [] + + data.append(last_request) + + if len(data) > LIMIT: + data.pop(0) + + with open(filename, 'w') as f: + json.dump(data, f) + +@app.post("/", status_code=status.HTTP_200_OK) +async def webhook_handler(request: Request, response: Response): + + payload = await request.body() + + last_request = { + "method": request.method, + "data": payload.decode("utf-8"), + "headers": dict(request.headers), + "url": request.url, + "time": datetime.now().isoformat(), + } + store_last_request(last_request) + + + response.status_code = status.HTTP_200_OK + + return {"status": "ok"} + +@app.get("/__last_request__", status_code=status.HTTP_200_OK) +async def last_requests(): + try: + with open('requests.json', 'r') as f: + data = json.load(f) + except (FileNotFoundError, json.JSONDecodeError): + data = [] + + return data[-1:] + +@app.get("/__history__", status_code=status.HTTP_200_OK) +async def history(): + try: + with open('requests.json', 'r') as f: + data = json.load(f) + except (FileNotFoundError, json.JSONDecodeError): + data = [] + + return data diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6b0b939 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +fastapi