From 7c2b4b47a025229d256722c06b91000666e4906d Mon Sep 17 00:00:00 2001 From: girst Date: Tue, 21 Jul 2020 12:53:27 +0200 Subject: [PATCH] make webhook a blueprint/standalone-hybrid --- app/{webhooks.py => webhooks/__init__.py} | 22 +++++++++++++--------- config/startup.sh | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) rename app/{webhooks.py => webhooks/__init__.py} (78%) diff --git a/app/webhooks.py b/app/webhooks/__init__.py similarity index 78% rename from app/webhooks.py rename to app/webhooks/__init__.py index 405c79a..8853fd8 100755 --- a/app/webhooks.py +++ b/app/webhooks/__init__.py @@ -1,19 +1,24 @@ """ -This is the webhook server that interfaces with Google's WebSub (formerly pubsubhubbub) server. This is its own server, so it can live on a different host than the frontend. +This is the webhook server that interfaces with Google's WebSub (formerly +pubsubhubbub) server. This can be run either as a module on the main frontend, +or as its own server, so it can live on a different host than the frontend. """ -#TODO: fold update-websub.py into this? - import time import sqlite3 -from flask import Flask, request +from flask import Flask, Blueprint, request from urllib.parse import parse_qs, urlparse from common.common import * -app = Flask(__name__) +frontend = Blueprint('webhooks', __name__) + +def app(): + app = Flask(__name__) + app.register_blueprint(frontend) + return app -@app.route('/websub/v1////', methods=["GET"]) +@frontend.route('/websub/v1////', methods=["GET"]) def websub(timestamp, nonce, subject, sig): hmackey = cf['websub']['hmac_key'] mode = request.args.get('hub.mode', '') @@ -39,7 +44,7 @@ def websub(timestamp, nonce, subject, sig): """, (subject, time.time()+until)) return challenge, 200 -@app.route('/websub/v1////', methods=["POST"]) +@frontend.route('/websub/v1////', methods=["POST"]) def websub_post(timestamp, nonce, subject, sig): lease = cf['websub']['lease'] hmackey = cf['websub']['hmac_key'] @@ -57,10 +62,9 @@ def websub_post(timestamp, nonce, subject, sig): except Exception as e: app.logger.error(e) with open('/tmp/websub-subscriptions.err', 'ab') as f: - #data = request.data.decode("utf-8", errors="ignore") f.write(f"\n".encode('ascii')) f.write(request.data + b"\n") return '', 200 if __name__ == '__main__': - app.run(debug=True) + app().run(debug=True) diff --git a/config/startup.sh b/config/startup.sh index ae82560..4819460 100755 --- a/config/startup.sh +++ b/config/startup.sh @@ -10,4 +10,4 @@ export APP_DIR=/opt/yt/app/ . "$VENV_ROOT/bin/activate" export YT_CONFIG="$CONFIG_DIR/config.ini" gunicorn --config="$CONFIG_DIR/gunicorn-frontend-config.py" --chdir="$MOD_DIR" --daemon app:app -gunicorn --config="$CONFIG_DIR/gunicorn-webhooks-config.py" --chdir="$APP_DIR" --daemon webhooks:app +gunicorn --config="$CONFIG_DIR/gunicorn-webhooks-config.py" --chdir="$APP_DIR" --daemon 'webhooks:app()' -- 2.39.3