2022-12-17 10:46:39 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-02-21 20:10:57 +00:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.response import text, json
|
2022-12-17 10:46:39 +00:00
|
|
|
from sanic_prometheus import monitor
|
2021-04-02 09:10:34 +00:00
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient
|
2023-07-30 10:05:02 +00:00
|
|
|
import httpx
|
2021-04-02 09:10:34 +00:00
|
|
|
import pymongo
|
2021-03-17 07:58:03 +00:00
|
|
|
import os
|
|
|
|
|
2021-04-02 09:10:34 +00:00
|
|
|
app = Sanic(__name__)
|
2022-12-17 10:46:39 +00:00
|
|
|
monitor(app).expose_endpoint()
|
2021-04-02 09:10:34 +00:00
|
|
|
|
2023-08-01 21:32:17 +00:00
|
|
|
INVENTORY_API_KEY = os.environ["INVENTORY_API_KEY"]
|
2023-08-01 19:21:57 +00:00
|
|
|
DOORBOY_SECRET_FLOOR = os.environ["DOORBOY_SECRET_FLOOR"]
|
|
|
|
DOORBOY_SECRET_WORKSHOP = os.environ["DOORBOY_SECRET_WORKSHOP"]
|
2023-07-30 10:17:14 +00:00
|
|
|
CARD_URI = os.environ["CARD_URI"]
|
2023-08-01 19:21:57 +00:00
|
|
|
FLOOR_ACCESS_GROUP = os.environ["FLOOR_ACCESS_GROUP"]
|
|
|
|
WORKSHOP_ACCESS_GROUP = os.environ["WORKSHOP_ACCESS_GROUP"]
|
2022-02-21 20:10:57 +00:00
|
|
|
MONGO_URI = os.getenv("MONGO_URI",
|
|
|
|
"mongodb://127.0.0.1:27017/default?replicaSet=rs0")
|
2021-03-17 07:58:03 +00:00
|
|
|
|
2023-08-01 19:21:57 +00:00
|
|
|
assert len(DOORBOY_SECRET_FLOOR) >= 10
|
|
|
|
assert len(DOORBOY_SECRET_WORKSHOP) >= 10
|
2021-03-17 07:58:03 +00:00
|
|
|
|
2021-10-26 17:17:48 +00:00
|
|
|
|
|
|
|
@app.listener("before_server_start")
|
|
|
|
async def setup_db(app, loop):
|
|
|
|
# TODO: find cleaner way to do this, for more see
|
|
|
|
# https://github.com/sanic-org/sanic/issues/919
|
2022-02-21 20:10:57 +00:00
|
|
|
app.ctx.db = AsyncIOMotorClient(MONGO_URI).get_default_database()
|
2021-10-26 17:17:48 +00:00
|
|
|
|
2021-03-17 07:58:03 +00:00
|
|
|
@app.route("/allowed")
|
2021-04-02 09:10:34 +00:00
|
|
|
async def view_doorboy_uids(request):
|
2023-08-01 19:21:57 +00:00
|
|
|
key = request.headers.get("KEY")
|
2023-08-01 20:02:58 +00:00
|
|
|
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
|
2021-04-03 14:09:51 +00:00
|
|
|
return text("how about no")
|
2023-07-30 10:05:02 +00:00
|
|
|
|
2023-08-10 16:42:18 +00:00
|
|
|
groups = []
|
2023-08-01 19:21:57 +00:00
|
|
|
if key == DOORBOY_SECRET_FLOOR:
|
2023-08-10 16:42:18 +00:00
|
|
|
groups.append(FLOOR_ACCESS_GROUP)
|
|
|
|
if key == DOORBOY_SECRET_WORKSHOP:
|
|
|
|
groups.append(WORKSHOP_ACCESS_GROUP)
|
|
|
|
if not groups:
|
2023-08-01 19:21:57 +00:00
|
|
|
return "fail", 500
|
2023-07-30 10:05:02 +00:00
|
|
|
async with httpx.AsyncClient() as client:
|
2023-08-01 19:21:57 +00:00
|
|
|
r = await client.post(CARD_URI, json={
|
2023-08-10 16:42:18 +00:00
|
|
|
"groups": groups
|
2023-08-01 21:32:17 +00:00
|
|
|
}, headers={
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": f"Basic {INVENTORY_API_KEY}"
|
|
|
|
})
|
2023-07-30 10:05:02 +00:00
|
|
|
j = r.json()
|
2021-03-17 07:58:03 +00:00
|
|
|
allowed_uids = []
|
2023-07-30 10:05:02 +00:00
|
|
|
for obj in j:
|
|
|
|
allowed_uids.append({
|
|
|
|
"token": obj["token"]
|
|
|
|
})
|
2021-04-03 14:09:51 +00:00
|
|
|
return json({"allowed_uids": allowed_uids})
|
2021-03-17 07:58:03 +00:00
|
|
|
|
|
|
|
|
2022-02-21 20:10:57 +00:00
|
|
|
@app.route("/longpoll", stream=True)
|
2021-04-02 09:10:34 +00:00
|
|
|
async def view_longpoll(request):
|
2023-08-10 16:35:43 +00:00
|
|
|
key = request.headers.get("KEY")
|
2023-08-01 20:02:58 +00:00
|
|
|
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
|
2022-02-21 20:10:57 +00:00
|
|
|
return text("Invalid token")
|
|
|
|
|
2023-08-10 17:45:24 +00:00
|
|
|
response = await request.respond(content_type="text/event-stream")
|
2022-02-21 20:10:57 +00:00
|
|
|
await response.send("data: response-generator-started\n\n")
|
|
|
|
pipeline = [
|
|
|
|
{
|
|
|
|
"$match": {
|
|
|
|
"operationType": "insert",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
async with app.ctx.db.eventlog.watch(pipeline) as stream:
|
|
|
|
await response.send("data: watch-stream-opened\n\n")
|
|
|
|
async for event in stream:
|
|
|
|
if event["fullDocument"].get("type") == "open-door":
|
|
|
|
await response.send("data: %s\n\n" %
|
|
|
|
event["fullDocument"]["door"])
|
2023-08-10 17:45:24 +00:00
|
|
|
except pymongo.errors.PyMongoError as e:
|
|
|
|
print(e)
|
|
|
|
await response.send("data: response-generator-ended\n\n")
|
2022-02-21 20:10:57 +00:00
|
|
|
return
|
2021-03-17 07:58:03 +00:00
|
|
|
|
|
|
|
|
2022-02-21 20:10:57 +00:00
|
|
|
if __name__ == "__main__":
|
2022-12-17 13:24:13 +00:00
|
|
|
app.run(debug=False, host="0.0.0.0", port=5000, single_process=True, access_log=True)
|