aboutsummaryrefslogtreecommitdiff
path: root/wqflask
diff options
context:
space:
mode:
authorBonfaceKilz2021-05-07 15:47:09 +0300
committerBonfaceKilz2021-05-08 14:21:21 +0300
commit2b375d11300d00ed426f7143014a4993da48d708 (patch)
tree535edc22b484ae084261768c9fbe920ae793233a /wqflask
parent6f1812df49422387c3a72d04405d688fb3604b81 (diff)
downloadgenenetwork2-2b375d11300d00ed426f7143014a4993da48d708.tar.gz
wqflask: Process MySQL URI and store the result in the global scope
Diffstat (limited to 'wqflask')
-rw-r--r--wqflask/wqflask/__init__.py21
1 files changed, 21 insertions, 0 deletions
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)