Modernize whole stack
This commit is contained in:
77
doorboy.py
77
doorboy.py
@@ -1,35 +1,43 @@
|
||||
from datetime import datetime
|
||||
from sanic import Sanic, response
|
||||
from sanic.response import text, json, stream
|
||||
from sanic import Sanic
|
||||
from sanic.response import text, json
|
||||
from motor.motor_asyncio import AsyncIOMotorClient
|
||||
import asyncio
|
||||
import pymongo
|
||||
import os
|
||||
import const
|
||||
|
||||
app = Sanic(__name__)
|
||||
|
||||
DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
||||
MONGO_URI = os.getenv("MONGO_URI",
|
||||
"mongodb://127.0.0.1:27017/default?replicaSet=rs0")
|
||||
|
||||
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.ctx.db = AsyncIOMotorClient(MONGO_URI).get_default_database()
|
||||
|
||||
|
||||
@app.route("/allowed")
|
||||
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")
|
||||
allowed_names = []
|
||||
async for obj in app.db.member.find({"enabled": True}):
|
||||
async for obj in app.ctx.db.member.find({"enabled": True}):
|
||||
allowed_names.append(obj["_id"])
|
||||
allowed_uids = []
|
||||
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 }):
|
||||
flt = {
|
||||
"token.uid_hash": {"$exists": True},
|
||||
"inventory.owner": {"$exists": True},
|
||||
"token.enabled": {"$exists": True}
|
||||
}
|
||||
prj = {
|
||||
"inventory.owner": True,
|
||||
"token.uid_hash": True
|
||||
}
|
||||
async for obj in app.ctx.db.inventory.find(flt, prj):
|
||||
if obj["inventory"].pop("owner").get("foreign_id") in allowed_names:
|
||||
del obj["_id"]
|
||||
del obj["inventory"]
|
||||
@@ -37,31 +45,30 @@ async def view_doorboy_uids(request):
|
||||
return json({"allowed_uids": allowed_uids})
|
||||
|
||||
|
||||
@app.route("/longpoll")
|
||||
@app.route("/longpoll", stream=True)
|
||||
async def view_longpoll(request):
|
||||
if request.headers.get('KEY') != DOORBOY_SECRET:
|
||||
return text("how about no")
|
||||
response = await request.respond(content_type="text/event-stream")
|
||||
if request.headers.get("KEY") != DOORBOY_SECRET:
|
||||
return text("Invalid token")
|
||||
|
||||
async def g(response):
|
||||
await response.write("data: response-generator-started\n\n")
|
||||
pipeline = [
|
||||
{
|
||||
'$match': {
|
||||
'operationType': "insert",
|
||||
# 'component': 'doorboy',
|
||||
# 'type': 'open-door'
|
||||
}
|
||||
}
|
||||
]
|
||||
try:
|
||||
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":
|
||||
await response.write("data: %s\n\n" % event["fullDocument"]["door"])
|
||||
except pymongo.errors.PyMongoError:
|
||||
return
|
||||
return stream(g, content_type="text/event-stream")
|
||||
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
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=False, host='0.0.0.0', port=5000)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=False, host="0.0.0.0", port=5000)
|
||||
|
||||
Reference in New Issue
Block a user