Simple Flask Example¶
Folder contents¶
This is a simple example using flask showing how to update the TOML configuration
Code examples¶
extensions/config.py
config.py
import os
from binascii import hexlify
from simple_toml_configurator import Configuration
default_config = {
"app": {
"ip": "0.0.0.0",
"host": "",
"port": 5000,
"upload_folder": "uploads",
"flask_secret_key": "",
"proxy": "",
"site_url": "http://localhost:5000",
"debug": True,
},
"mysql": {
"host": "",
"port": "",
"user": "",
"password": "",
"databases": {"prod":"test", "dev":"test2"},
},
"scheduler": {
"disabled": True
},
"logging": {
"debug": True
},
"queue": {
"disabled": True
},
}
config_path = os.environ.get("CONFIG_PATH", os.path.join(os.getcwd(), "config"))
settings = Configuration()
settings.init_config(config_path, default_config)
# create random Flask secret_key if there's none in config.toml
if not settings.config.get("app", {}).get("flask_secret_key"):
key = os.environ.get("APP_FLASK_SECRET_KEY", hexlify(os.urandom(16)).decode())
settings.update_config({"app_flask_secret_key": key})
# Set default mysql host
if not settings.config.get("mysql",{}).get("host"):
mysql_host = os.environ.get("MYSQL_HOST", "localhost")
settings.update_config({"mysql_host": mysql_host})
# Set default mysql port
if not settings.config.get("mysql",{}).get("port"):
mysql_port = os.environ.get("MYSQL_PORT", "3306")
settings.update_config({"mysql_port": mysql_port})
# Set default mysql user
if not settings.config.get("mysql",{}).get("user"):
mysql_user = os.environ.get("MYSQL_USER", "root")
settings.update_config({"mysql_user": mysql_user})
# Set default mysql password
if not settings.config.get("mysql",{}).get("password"):
mysql_password = os.environ.get("MYSQL_PASSWORD", "root")
settings.update_config({"mysql_password": mysql_password})
if not settings.config.get("mysql",{}).get("database"):
mysql_database = os.environ.get("MYSQL_DATABASE", "some_database")
settings.update_config({"mysql_database": mysql_database})
app.py
app.py
from flask import Flask, jsonify, request, url_for,redirect
from extenstions.config import settings
def create_app():
app = Flask(__name__)
app.config["SECRET_KEY"] = settings.config.get("app").get("flask_secret_key")
app.config['APP_PORT'] = settings.config.get("app").get("port")
app.config['APP_IP'] = settings.config.get("app").get("ip")
app.config['APP_HOST'] = settings.config.get("app").get("host")
app.config["DEBUG"] = settings.config.get("app").get("debug")
return app
app = create_app()
# simple route that returns the config
@app.route("/")
def app_settings():
return jsonify(
{
"response": {
"data": {
"configuration": settings.get_settings(),
}
}
})
# Update settings route
@app.route("/update", methods=["POST"])
def update_settings():
data = request.get_json() # {"app_proxy": "http://localhost:8080", "app_debug": "True}
settings.update_config(data)
return redirect(url_for("app_settings"))
# Get settings value route
@app.route("/get", methods=["GET"])
def get_settings():
key = request.args.get("key","")
value = request.args.get("value","")
config_attr_value = settings.config.get(key, {}).get(value, "not found")
instance_attr_value = getattr(settings, key, "not found")
return jsonify(
{
"response": {
"data": {
"Configuration.config": {
"key": key,
"value": config_attr_value,
},
"Configuration": {
"key": key,
"value": instance_attr_value,
},
}
}
})
if __name__ == "__main__":
app.run(port=app.config.get("APP_PORT"), host=app.config.get("APP_IP"), debug=app.config.get("APP_DEBUG"))