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
|
|
|
|
2021-03-17 07:58:03 +00:00
|
|
|
DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
2023-07-30 10:17:14 +00:00
|
|
|
CARD_URI = os.environ["CARD_URI"]
|
2023-07-30 10:05:02 +00:00
|
|
|
CARD_KUBE_GROUP = os.environ["CARD_KUBE_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
|
|
|
|
2022-02-21 20:10:57 +00:00
|
|
|
assert len(DOORBOY_SECRET) >= 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):
|
2022-02-21 20:10:57 +00:00
|
|
|
if request.headers.get("KEY") != DOORBOY_SECRET:
|
2021-04-03 14:09:51 +00:00
|
|
|
return text("how about no")
|
2023-07-30 10:05:02 +00:00
|
|
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
2023-07-30 10:17:14 +00:00
|
|
|
r = await client.get(CARD_URI, params={
|
2023-07-30 10:05:02 +00:00
|
|
|
"group": CARD_KUBE_GROUP
|
|
|
|
})
|
|
|
|
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):
|
2022-02-21 20:10:57 +00:00
|
|
|
response = await request.respond(content_type="text/event-stream")
|
|
|
|
if request.headers.get("KEY") != DOORBOY_SECRET:
|
|
|
|
return text("Invalid token")
|
|
|
|
|
|
|
|
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"])
|
|
|
|
except pymongo.errors.PyMongoError:
|
|
|
|
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)
|