Fix MongoDB event loop handling
This commit is contained in:
parent
c61f8175e6
commit
ff1dd40c4d
17
doorboy.py
17
doorboy.py
@ -9,22 +9,27 @@ import const
|
|||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
|
|
||||||
mongodb = AsyncIOMotorClient(const.MONGO_URI)
|
|
||||||
db = mongodb.get_default_database()
|
|
||||||
|
|
||||||
DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
||||||
|
|
||||||
assert len(DOORBOY_SECRET) > 10
|
assert len(DOORBOY_SECRET) > 10
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
app.db = AsyncIOMotorClient(const.MONGO_URI).get_default_database()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/allowed")
|
@app.route("/allowed")
|
||||||
async def view_doorboy_uids(request):
|
async def view_doorboy_uids(request):
|
||||||
if request.headers.get('KEY') != DOORBOY_SECRET:
|
if request.headers.get('KEY') != DOORBOY_SECRET:
|
||||||
return text("how about no")
|
return text("how about no")
|
||||||
allowed_names = []
|
allowed_names = []
|
||||||
async for obj in db.member.find({"enabled": True}):
|
async for obj in app.db.member.find({"enabled": True}):
|
||||||
allowed_names.append(obj["_id"])
|
allowed_names.append(obj["_id"])
|
||||||
allowed_uids = []
|
allowed_uids = []
|
||||||
async for obj in db.inventory.find({"token.uid_hash": {"$exists":True}, "inventory.owner": {"$exists":True}, "token.enabled": {"$exists":True}}, {"inventory.owner": True, "token.uid_hash": True }):
|
async for obj in app.db.inventory.find({"token.uid_hash": {"$exists":True}, "inventory.owner": {"$exists":True}, "token.enabled": {"$exists":True}}, {"inventory.owner": True, "token.uid_hash": True }):
|
||||||
if obj["inventory"].pop("owner").get("foreign_id") in allowed_names:
|
if obj["inventory"].pop("owner").get("foreign_id") in allowed_names:
|
||||||
del obj["_id"]
|
del obj["_id"]
|
||||||
del obj["inventory"]
|
del obj["inventory"]
|
||||||
@ -49,7 +54,7 @@ async def view_longpoll(request):
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
async with db.eventlog.watch(pipeline) as stream:
|
async with app.db.eventlog.watch(pipeline) as stream:
|
||||||
await response.write("data: watch-stream-opened\n\n")
|
await response.write("data: watch-stream-opened\n\n")
|
||||||
async for event in stream:
|
async for event in stream:
|
||||||
if event["fullDocument"].get("type") == "open-door":
|
if event["fullDocument"].get("type") == "open-door":
|
||||||
|
Loading…
Reference in New Issue
Block a user