Fix MongoDB event loop handling
This commit is contained in:
		
							
								
								
									
										17
									
								
								doorboy.py
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								doorboy.py
									
									
									
									
									
								
							| @@ -9,22 +9,27 @@ import const | ||||
|  | ||||
| app = Sanic(__name__) | ||||
|  | ||||
| mongodb = AsyncIOMotorClient(const.MONGO_URI) | ||||
| db = mongodb.get_default_database() | ||||
|  | ||||
| DOORBOY_SECRET = os.environ["DOORBOY_SECRET"] | ||||
|  | ||||
| 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") | ||||
| async def view_doorboy_uids(request): | ||||
|     if request.headers.get('KEY') != DOORBOY_SECRET: | ||||
|         return text("how about no") | ||||
|     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_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: | ||||
|             del obj["_id"] | ||||
|             del obj["inventory"] | ||||
| @@ -49,7 +54,7 @@ async def view_longpoll(request): | ||||
|             } | ||||
|         ] | ||||
|         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") | ||||
|                 async for event in stream: | ||||
|                     if event["fullDocument"].get("type") == "open-door": | ||||
|   | ||||
		Reference in New Issue
	
	Block a user