From 2b375d11300d00ed426f7143014a4993da48d708 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Fri, 7 May 2021 15:47:09 +0300 Subject: wqflask: Process MySQL URI and store the result in the global scope --- wqflask/wqflask/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/wqflask/wqflask/__init__.py b/wqflask/wqflask/__init__.py index bf5e094b..2e94dd01 100644 --- a/wqflask/wqflask/__init__.py +++ b/wqflask/wqflask/__init__.py @@ -5,6 +5,8 @@ import jinja2 from flask import g from flask import Flask +from typing import Tuple +from urllib.parse import urlparse from utility import formatting from wqflask.markdown_routes import glossary_blueprint from wqflask.markdown_routes import references_blueprint @@ -16,9 +18,28 @@ from wqflask.markdown_routes import blogs_blueprint app = Flask(__name__) + +# Helper function for getting the SQL objects +def parse_db_url(sql_uri: str) -> Tuple: + """Parse SQL_URI env variable from an sql URI + e.g. 'mysql://user:pass@host_name/db_name' + + """ + parsed_db = urlparse(sql_uri) + return (parsed_db.hostname, parsed_db.username, + parsed_db.password, parsed_db.path[1:]) + + # See http://flask.pocoo.org/docs/config/#configuring-from-files # Note no longer use the badly named WQFLASK_OVERRIDES (nyi) app.config.from_envvar('GN2_SETTINGS') + +DB_HOST, DB_USER, DB_PASS, DB_NAME = parse_db_url(app.config.get('SQL_URI')) +app.config["DB_HOST"] = DB_HOST +app.config["DB_USER"] = DB_USER +app.config["DB_PASS"] = DB_PASS +app.config["DB_NAME"] = DB_NAME + app.jinja_env.globals.update( undefined=jinja2.StrictUndefined, numify=formatting.numify) -- cgit v1.2.3