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
def activate_job():
helpers.load_dbconfig(app)
helpers.load_version(app)
helpers.create_data_directory()
database.create_entry_table()

View File

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

View File

@ -47,6 +47,19 @@ def load_version(app):
app.config['VERSION'] = ""
else:
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):
if check_config_exists():