mirror of
https://github.com/shadoll/scatcher.git
synced 2025-12-20 04:27:58 +00:00
initial commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.env
|
||||
.mypy_cache
|
||||
.pytest_cache
|
||||
12
Dockerfile
Normal file
12
Dockerfile
Normal file
@@ -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"]
|
||||
61
app.py
Normal file
61
app.py
Normal file
@@ -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
|
||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
fastapi
|
||||
Reference in New Issue
Block a user