From 8ece59267e373edb810acd22aac19ef74349d93c Mon Sep 17 00:00:00 2001 From: songmeo Date: Wed, 17 Mar 2021 09:58:03 +0200 Subject: [PATCH] first commit --- .env | 3 +++ Dockerfile | 8 ++++++ docker-compose.yml | 18 +++++++++++++ main.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 +++ 5 files changed, 96 insertions(+) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.env b/.env new file mode 100644 index 0000000..3b6d269 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +# MONGO_URI=mongodb://root:salakala@localhost:27017/?replicaSet=kspace-mongo-set +MONGO_URI=mongodb://root:salakala@localhost:27017 +DOORBOY_SECRET=doorboy_secret diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f5320b1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3 +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install -r requirements.txt + +COPY *.py ./ +CMD ["gunicorn", "--workers", "10", "--bind", "[::]:5000", "main:app", "--timeout", "90"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..521587d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.7' + +networks: + infra: + external: true + +services: + doorboy_proxy: + hostname: doorboy.infra.k-space.ee + restart: unless-stopped + env_file: .env + networks: + infra: + ipv4_address: 172.21.99.97 + build: + context: . + + diff --git a/main.py b/main.py new file mode 100644 index 0000000..a42013f --- /dev/null +++ b/main.py @@ -0,0 +1,63 @@ +from datetime import datetime +from flask import Flask, request, redirect, render_template, Blueprint, make_response, jsonify +from pymongo import MongoClient +import flask +import hashlib +import jinja2 +import json +import os +import pymongo +import smtplib + +app = Flask(__name__) +mongodb = MongoClient('mongodb://172.21.27.1,172.21.27.2,172.21.27.3:27017/', replicaSet="kspace-mongo-set").kspace_accounting +mongodb.authenticate("kspace_accounting", os.environ["MONGO_PASSWORD"]) + +DOORBOY_SECRET = os.environ["DOORBOY_SECRET"] + +assert len(DOORBOY_SECRET) > 10 + +@app.route("/allowed") +def view_doorboy_uids(): + if request.headers.get('KEY') != DOORBOY_SECRET: + return "how about no" + allowed_names = [o["_id"] for o in mongodb.member.find({"enabled": True})] + 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 }): + if obj["inventory"].pop("owner_id") in allowed_names: + obj.pop("_id") + obj.pop("inventory") + allowed_uids.append(obj) + return jsonify(allowed_uids=allowed_uids) + + +@app.route("/longpoll") +def view_longpoll(): + if request.headers.get('KEY') != DOORBOY_SECRET: + return "how about no" + + def g(): + yield "data: response-generator-started\n\n" + pipeline = [ + { + '$match': { + 'operationType': "insert", +# 'component': 'doorboy', +# 'type': 'open-door' + } + } + ] + try: + with mongodb.eventlog.watch(pipeline) as stream: + yield "data: watch-stream-opened\n\n" + for event in stream: + if event["fullDocument"].get("type") == "open-door": + yield "data: %s\n\n" % event["fullDocument"]["door"] + except pymongo.errors.PyMongoError: + return "urror" + return flask.Response(g(), + mimetype="text/event-stream") + +if __name__ == '__main__': + app.run(debug=False, host='::') + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2df0789 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Flask +Flask-WTF +pymongo +gunicorn