From e4e413ede34a2ae306ddd130324435d0e735f064 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sat, 9 May 2015 00:07:35 +0200 Subject: influxdb-guard --- run.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 run.py (limited to 'run.py') diff --git a/run.py b/run.py new file mode 100755 index 0000000..bfd9975 --- /dev/null +++ b/run.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +import os +import sys +import logging +from configparser import ConfigParser + +from flask import Flask + +from guard import application, model, proxyfix + + +def make_app(config): + app = Flask("Guard") + app.secret_key = config["Guard"]["Adminsecret"] + os.uname()[1] + + model.db.init(os.path.join(os.path.dirname(__file__), "db.sqlite")) + model.db.connect() + model.db.create_tables(filter(lambda t: not t.table_exists(), [model.Access])) + + log_format = "%(asctime)-15s %(levelname)s [%(module) 15s] %(message)s" + if config["Guard"]["Debug"] == "true": + logging.basicConfig(level=logging.INFO, format=log_format) + logging.info("Running in DEBUG mode!") + else: + logging.basicConfig(level=logging.ERROR, format=log_format) + logging.info("Running in production mode!") + + application.make_app(app, config["Guard"]) + + app.wsgi_app = proxyfix.fix(app.wsgi_app) + return app + + +def get_config(path: str = None) -> ConfigParser: + config = ConfigParser() + config["Guard"] = { + 'url': "http://localhost:8086/write", + "port": "8001", + "cookiename": "token", + "adminsecret": "changeme", + "debug": "false" + } + if path is not None: + config.read(path) + return config + + +def start(): + if len(sys.argv) == 2: + config = get_config(sys.argv[1]) + app = make_app(config) + app.run(host="0.0.0.0", port=int(config["Guard"]["Port"]), + debug=config["Guard"]["Debug"] == "true", + use_reloader=config["Guard"]["Debug"] == "true") + else: + print("# {} >config.defaults.ini".format(sys.argv[0])) + get_config().write(sys.stdout) + sys.exit(1) + + +if __name__ == "__main__": + start() +else: # for running gunicorn3 + config = get_config('config') + application = make_app(config) -- cgit v1.2.1