From 919d358b533b719d35b336f61a6640e67466e3bf Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Mon, 24 Feb 2020 10:44:10 +0100 Subject: [PATCH] add production WSGI --- README.md | 17 +++++++++++++++++ app.py | 3 +++ webhook-listener.py => webhook_listener.py | 7 ++++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 app.py rename webhook-listener.py => webhook_listener.py (95%) diff --git a/README.md b/README.md index 01e00ff..c7cd7ee 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,23 @@ The config file uses *COMMA* as a separator, lines are comments if they start wi PROJECT,TOKEN,PATH_TO_SCRIPT +# Running standalone with flask-inbuild server + + usage: webhook_listener.py [-h] [-i INTERFACE] [-p PORT] [-c C] + Simple Webhook listener + + optional arguments: + -h, --help show this help message and exit + -i INTERFACE, --interface INTERFACE + Interface to listen on (default: 0.0.0.0) + -p PORT, --port PORT Port to listen on (default: 5000) + -c C Config for handling of webhooks (default: + webhook.config) + +# Running with waitress (WSGI) + + waitress-serve --host 127.0.0.1 --port 5000 --call 'app:createApp' + # Running behind NGINX for SSL You can (and should) run this tool behind a reverse proxy handling SSL. I recommend nginx with this configuration. Note the *proxy_next_upstream*-directive which tells nginx, that it should only report a timeout as bad gateway, since the backend will respond with certain error codes to ease debugging. diff --git a/app.py b/app.py new file mode 100644 index 0000000..14cbab7 --- /dev/null +++ b/app.py @@ -0,0 +1,3 @@ +import webhook_listener as server +def createApp(envivorment=None, start_response=None): + return server.app diff --git a/webhook-listener.py b/webhook_listener.py similarity index 95% rename from webhook-listener.py rename to webhook_listener.py index 93cad05..a467772 100755 --- a/webhook-listener.py +++ b/webhook_listener.py @@ -7,6 +7,7 @@ import os import subprocess app = flask.Flask("webhook-listener") +app.config["EXEC_CONFIG"] = "webhook.config" TOKEN_HEADER = "X-Gitlab-Token" PROJECT_IDENTIFIER = "web_url" SEPERATOR = "," @@ -85,6 +86,10 @@ def readExecutionConfig(configFile): projectIdent, token, scriptName = line.split(SEPERATOR) config.update({projectIdent:(token, scriptName)}) +@app.before_first_request +def init(): + readExecutionConfig(app.config["EXEC_CONFIG"]) + if __name__ == "__main__": parser = argparse.ArgumentParser(description="Simple Webhook listener", \ @@ -95,5 +100,5 @@ if __name__ == "__main__": parser.add_argument("-c", default="webhook.config", help="Config for handling of webhooks") args = parser.parse_args() - readExecutionConfig(args.c) + app.config["EXEC_CONFIG"] = args.c app.run(host=args.interface, port=args.port)