clean up requirements, small tweak
This commit is contained in:
parent
e38e753eea
commit
c9018003ec
34
main.py
34
main.py
@ -1,17 +1,13 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sanic import Sanic, response
|
from sanic import Sanic, response
|
||||||
import uvloop, asyncio
|
from sanic.response import text, json, stream
|
||||||
from motor.motor_asyncio import AsyncIOMotorClient
|
from motor.motor_asyncio import AsyncIOMotorClient
|
||||||
|
import uvloop, asyncio
|
||||||
import pymongo
|
import pymongo
|
||||||
import hashlib
|
|
||||||
import jinja2
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import smtplib
|
|
||||||
import const
|
import const
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) # swap default event loop to uvloop
|
|
||||||
|
|
||||||
#mongodb = MongoClient('mongodb://172.21.27.1,172.21.27.2,172.21.27.3:27017/', replicaSet="kspace-mongo-set").kspace_accounting
|
#mongodb = MongoClient('mongodb://172.21.27.1,172.21.27.2,172.21.27.3:27017/', replicaSet="kspace-mongo-set").kspace_accounting
|
||||||
mongodb = AsyncIOMotorClient(const.MONGO_URI)
|
mongodb = AsyncIOMotorClient(const.MONGO_URI)
|
||||||
@ -24,24 +20,26 @@ assert len(DOORBOY_SECRET) > 10
|
|||||||
@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 "how about no"
|
return text("how about no")
|
||||||
allowed_names = [o["_id"] for o in await mongodb.member.find({"enabled": True}).to_list()]
|
allowed_names = []
|
||||||
|
async for obj in mongodb.member.find({"enabled": True}):
|
||||||
|
allowed_names.append(obj["_id"])
|
||||||
allowed_uids = []
|
allowed_uids = []
|
||||||
for obj in await mongodb.inventory.find({"token.uid_hash": {"$exists":True}, "inventory.owner_id": {"$exists":True}, "token.enabled": True}, {"inventory.owner_id": True, "token.uid_hash": True }):
|
async for obj in mongodb.inventory.find({"token.uid_hash": {"$exists":True}, "inventory.owner_id": {"$exists":True}, "token.enabled": True}, {"inventory.owner_id": True, "token.uid_hash": True }):
|
||||||
if obj["inventory"].pop("owner_id") in allowed_names:
|
if obj["inventory"].pop("owner_id") in allowed_names:
|
||||||
del obj["_id"]
|
del obj["_id"]
|
||||||
del obj["inventory"]
|
del obj["inventory"]
|
||||||
allowed_uids.append(obj)
|
allowed_uids.append(obj)
|
||||||
return response.json(allowed_uids=allowed_uids)
|
return json({"allowed_uids": allowed_uids})
|
||||||
|
|
||||||
|
|
||||||
@app.route("/longpoll")
|
@app.route("/longpoll")
|
||||||
async def view_longpoll(request):
|
async def view_longpoll(request):
|
||||||
if request.headers.get('KEY') != DOORBOY_SECRET:
|
if request.headers.get('KEY') != DOORBOY_SECRET:
|
||||||
return "how about no"
|
return text("how about no")
|
||||||
|
|
||||||
async def g():
|
async def g(response):
|
||||||
yield "data: response-generator-started\n\n"
|
await response.write("data: response-generator-started\n\n")
|
||||||
pipeline = [
|
pipeline = [
|
||||||
{
|
{
|
||||||
'$match': {
|
'$match': {
|
||||||
@ -53,13 +51,15 @@ async def view_longpoll(request):
|
|||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
async with mongodb.eventlog.watch(pipeline) as stream:
|
async with mongodb.eventlog.watch(pipeline) as stream:
|
||||||
yield "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":
|
||||||
yield "data: %s\n\n" % event["fullDocument"]["door"]
|
await response.write("data: %s\n\n" % event["fullDocument"]["door"])
|
||||||
except pymongo.errors.PyMongoError:
|
except pymongo.errors.PyMongoError:
|
||||||
return "urror"
|
return
|
||||||
return stream(g, content_type="text/event-stream")
|
return stream(g, content_type="text/event-stream")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=False, host='::')
|
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) # swap default event loop to uvloop
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
app.run(host='::', debug=False, loop=loop)
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
Flask
|
|
||||||
Flask-WTF
|
|
||||||
pymongo
|
|
||||||
gunicorn
|
gunicorn
|
||||||
sanic
|
sanic
|
||||||
motor
|
motor
|
||||||
|
Loading…
Reference in New Issue
Block a user