mirror of
https://github.com/shadoll/scatcher.git
synced 2025-12-20 03:26:59 +00:00
Refactor request history storage and add Docker Compose file
This commit is contained in:
20
app.py
20
app.py
@@ -4,23 +4,26 @@ import json
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
LIMIT = 10
|
||||
HISTORY_LIMIT = 10
|
||||
HISTORY_FILE = "storage/requests.json"
|
||||
|
||||
def store_last_request(last_request, filename='requests.json'):
|
||||
|
||||
def store_last_request(last_request, filename=HISTORY_FILE):
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
with open(filename, "r") as f:
|
||||
data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
data = []
|
||||
|
||||
data.append(last_request)
|
||||
|
||||
if len(data) > LIMIT:
|
||||
if len(data) > HISTORY_LIMIT:
|
||||
data.pop(0)
|
||||
|
||||
with open(filename, 'w') as f:
|
||||
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):
|
||||
|
||||
@@ -35,25 +38,26 @@ async def webhook_handler(request: Request, response: Response):
|
||||
}
|
||||
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:
|
||||
with open(HISTORY_FILE, "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:
|
||||
with open(HISTORY_FILE, "r") as f:
|
||||
data = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
data = []
|
||||
|
||||
20
compose.yml
Normal file
20
compose.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
networks:
|
||||
apps-proxy:
|
||||
external: true
|
||||
name: apps-proxy
|
||||
|
||||
services:
|
||||
catcher:
|
||||
image: shadoll/scatcher
|
||||
volumes:
|
||||
- ./storage:/app/storage
|
||||
networks:
|
||||
- "apps-proxy"
|
||||
labels:
|
||||
traefik.enable: true
|
||||
traefik.docker.network: apps-proxy
|
||||
traefik.http.routers.catcher.entrypoints: websecure
|
||||
traefik.http.routers.catcher.rule: Host(`catcher.rs.shadoll.dev`)
|
||||
traefik.http.routers.catcher.tls: true
|
||||
traefik.http.routers.catcher.tls.certresolver: letsencrypt
|
||||
traefik.http.services.catcher.loadbalancer.server.port: 5000
|
||||
Reference in New Issue
Block a user