Load DB Connection from ENV

This commit is contained in:
Phillip Kühne 2022-07-15 15:27:36 +02:00
parent fc78bdc4fe
commit 57ced69ddd
Signed by: phillip
GPG Key ID: E4C1C4D2F90902AA
3 changed files with 16 additions and 2 deletions

View File

@ -229,6 +229,7 @@ def admin():
@app.before_first_request @app.before_first_request
def activate_job(): def activate_job():
helpers.load_dbconfig(app)
helpers.load_version(app) helpers.load_version(app)
helpers.create_data_directory() helpers.create_data_directory()
database.create_entry_table() database.create_entry_table()

View File

@ -6,6 +6,7 @@ from sqlalchemy import create_engine, engine
import mariadb import mariadb
import pandas import pandas
from io import StringIO from io import StringIO
from flask import current_app
song_table = "songs" song_table = "songs"
entry_table = "entries" entry_table = "entries"
@ -18,8 +19,7 @@ connection = None
def open_db() -> engine.base.Connection: def open_db() -> engine.base.Connection:
global connection global connection
if (not connection): if (not connection):
engine = create_engine( engine = create_engine(current_app.config.get("DBCONNSTRING"))
"mysql://ek0ur6p6ky9gdmif:jk571ov6g38g5iqv@eporqep6b4b8ql12.chr7pe7iynqr.eu-west-1.rds.amazonaws.com:3306/xdfmpudc3remzgj0")
connection = engine.connect() connection = engine.connect()
# cur.execute('PRAGMA encoding = "UTF-8";') # cur.execute('PRAGMA encoding = "UTF-8";')
return connection return connection

View File

@ -48,6 +48,19 @@ def load_version(app):
else: else:
app.config['VERSION'] = "" app.config['VERSION'] = ""
def load_dbconfig(app):
if os.environ.get("JAWSDB_MARIA_URL"):
app.config['VERSION'] = os.environ.get("JAWSDB_MARIA_URL")
elif os.path.isfile(".dbconn"):
with open('.dbconn', 'r') as file:
data = file.read().replace('\n', '')
if data:
app.config['DBCONNSTRING'] = data
else:
app.config['DBCONNSTRING'] = ""
else:
app.config['DBCONNSTRING'] = ""
def setup_config(app): def setup_config(app):
if check_config_exists(): if check_config_exists():
config = json.load(open(config_file)) config = json.load(open(config_file))