From 5957029860a61e7ff7b402da2d2d7274ab510781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Mon, 17 Aug 2020 11:05:01 +0200 Subject: [PATCH] Configuration file --- .gitignore | 1 + config.sample.toml | 6 ++++++ requirements.txt | 1 + website/app.py | 27 ++++++--------------------- website/settings.py | 0 5 files changed, 14 insertions(+), 21 deletions(-) create mode 100644 config.sample.toml delete mode 100644 website/settings.py diff --git a/.gitignore b/.gitignore index 1efdcafb..6ee42de7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ venv/* .*@neomake* .ash_history .python_history +config.toml diff --git a/config.sample.toml b/config.sample.toml new file mode 100644 index 00000000..97e1a99f --- /dev/null +++ b/config.sample.toml @@ -0,0 +1,6 @@ +SECRET_KEY = "change me before you go in production" + +[LDAP] +URI = "ldaps://ldap.mydomain.tld" +BIND_USER = "cn=admin,dc=mydomain,dc=tld" +BIND_PW = "admin" diff --git a/requirements.txt b/requirements.txt index ff1021ef..e200bd4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Flask Flask-SQLAlchemy Authlib==0.14.3 python-ldap +toml diff --git a/website/app.py b/website/app.py index 578ec0cd..b198afff 100644 --- a/website/app.py +++ b/website/app.py @@ -1,8 +1,8 @@ import ldap import os +import toml from flask import Flask, g -# from .models import db from .oauth2 import config_oauth from .routes import bp @@ -10,19 +10,10 @@ from .routes import bp def create_app(config=None): app = Flask(__name__) - # load default configuration - app.config.from_object("website.settings") + config = toml.load(os.environ.get("CONFIG", "config.toml")) + app.config.from_mapping(config) - # load environment configuration - if "WEBSITE_CONF" in os.environ: - app.config.from_envvar("WEBSITE_CONF") - - # load app specified configuration - if config is not None: - if isinstance(config, dict): - app.config.update(config) - elif config.endswith(".py"): - app.config.from_pyfile(config) + app.url_map.strict_slashes = False setup_app(app) return app @@ -31,8 +22,8 @@ def create_app(config=None): def setup_app(app): @app.before_request def before_request(): - g.ldap = ldap.initialize("ldap://ldap") - g.ldap.simple_bind_s("cn=admin,dc=mydomain,dc=tld", "admin") + g.ldap = ldap.initialize(app.config["LDAP"]["URI"]) + g.ldap.simple_bind_s(app.config["LDAP"]["BIND_USER"], app.config["LDAP"]["BIND_PW"]) @app.after_request def after_request(response): @@ -40,11 +31,5 @@ def setup_app(app): g.ldap.unbind_s() return response - # # Create tables if they do not exist already - # @app.before_first_request - # def create_tables(): - # db.create_all() - # - # db.init_app(app) config_oauth(app) app.register_blueprint(bp, url_prefix="") diff --git a/website/settings.py b/website/settings.py deleted file mode 100644 index e69de29b..00000000