change to sanic and motor
This commit is contained in:
parent
9adf269528
commit
661a704d8f
38
main.py
38
main.py
@ -1,18 +1,20 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from flask import Flask, request, redirect, render_template, Blueprint, make_response, jsonify
|
from sanic import Sanic, response
|
||||||
from pymongo import MongoClient
|
import uvloop, asyncio
|
||||||
import flask
|
from motor.motor_asyncio import AsyncIOMotorClient
|
||||||
|
import pymongo
|
||||||
import hashlib
|
import hashlib
|
||||||
import jinja2
|
import jinja2
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pymongo
|
|
||||||
import smtplib
|
import smtplib
|
||||||
import const
|
import const
|
||||||
|
|
||||||
app = Flask(__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 = MongoClient(const.MONGO_URI)
|
mongodb = AsyncIOMotorClient(const.MONGO_URI)
|
||||||
mongodb = mongodb.get_default_database()
|
mongodb = mongodb.get_default_database()
|
||||||
|
|
||||||
DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
||||||
@ -20,25 +22,25 @@ DOORBOY_SECRET = os.environ["DOORBOY_SECRET"]
|
|||||||
assert len(DOORBOY_SECRET) > 10
|
assert len(DOORBOY_SECRET) > 10
|
||||||
|
|
||||||
@app.route("/allowed")
|
@app.route("/allowed")
|
||||||
def view_doorboy_uids():
|
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 "how about no"
|
||||||
allowed_names = [o["_id"] for o in mongodb.member.find({"enabled": True})]
|
allowed_names = [o["_id"] for o in await mongodb.member.find({"enabled": True}).to_list()]
|
||||||
allowed_uids = []
|
allowed_uids = []
|
||||||
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 }):
|
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 }):
|
||||||
if obj["inventory"].pop("owner_id") in allowed_names:
|
if obj["inventory"].pop("owner_id") in allowed_names:
|
||||||
obj.pop("_id")
|
del obj["_id"]
|
||||||
obj.pop("inventory")
|
del obj["inventory"]
|
||||||
allowed_uids.append(obj)
|
allowed_uids.append(obj)
|
||||||
return jsonify(allowed_uids=allowed_uids)
|
return response.json(allowed_uids=allowed_uids)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/longpoll")
|
@app.route("/longpoll")
|
||||||
def view_longpoll():
|
async def view_longpoll(request):
|
||||||
if request.headers.get('KEY') != DOORBOY_SECRET:
|
if request.headers.get('KEY') != DOORBOY_SECRET:
|
||||||
return "how about no"
|
return "how about no"
|
||||||
|
|
||||||
def g():
|
async def g():
|
||||||
yield "data: response-generator-started\n\n"
|
yield "data: response-generator-started\n\n"
|
||||||
pipeline = [
|
pipeline = [
|
||||||
{
|
{
|
||||||
@ -50,16 +52,14 @@ def view_longpoll():
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
with mongodb.eventlog.watch(pipeline) as stream:
|
async with mongodb.eventlog.watch(pipeline) as stream:
|
||||||
yield "data: watch-stream-opened\n\n"
|
yield "data: watch-stream-opened\n\n"
|
||||||
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"]
|
yield "data: %s\n\n" % event["fullDocument"]["door"]
|
||||||
except pymongo.errors.PyMongoError:
|
except pymongo.errors.PyMongoError:
|
||||||
return "urror"
|
return "urror"
|
||||||
return flask.Response(g(),
|
return stream(g, content_type="text/event-stream")
|
||||||
mimetype="text/event-stream")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=False, host='::')
|
app.run(debug=False, host='::')
|
||||||
|
|
||||||
|
@ -2,3 +2,5 @@ Flask
|
|||||||
Flask-WTF
|
Flask-WTF
|
||||||
pymongo
|
pymongo
|
||||||
gunicorn
|
gunicorn
|
||||||
|
sanic
|
||||||
|
motor
|
||||||
|
Loading…
Reference in New Issue
Block a user