Add swipe proxy endpoint
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2023-08-18 04:08:02 +03:00
parent 13faabf01f
commit 094eafa9da
2 changed files with 25 additions and 0 deletions

View File

@ -84,6 +84,30 @@ async def view_longpoll(request):
await response.send("data: response-generator-ended\n\n")
return
@app.post("/swipe")
async def forward_swipe(request):
key = request.headers.get("KEY")
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
return text("Invalid token", status=401)
data = request.json
doors = []
if key == DOORBOY_SECRET_FLOOR:
doors.extend(["backdoor, frontdoor, grounddoor"])
if key == DOORBOY_SECRET_WORKSHOP:
doors.append("workshopdoor")
if data.get("door") not in doors:
return text("Not allowed", 403)
async with httpx.AsyncClient() as client:
r = await client.post(SWIPE_URI, json=data, headers={
"Content-Type": "application/json",
"Authorization": f"Basic {INVENTORY_API_KEY}"
})
if r.status_code == 200:
return text("ok")
else:
return text("Failed", 500)
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0", port=5000, single_process=True, access_log=True)

View File

@ -30,5 +30,6 @@ services:
FLOOR_ACCESS_GROUP: "k-space:floor"
WORKSHOP_ACCESS_GROUP: "k-space:workshop"
CARD_URI: "https://inventory-app-72zn4.codemowers.ee/cards"
SWIPE_URI: "https://inventory-app-72zn4.codemowers.ee/m/doorboy/swipe"
build:
context: .