27 lines
767 B
Python
27 lines
767 B
Python
import pymysql
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
def db_connection():
|
|
# Wir wählen die Konfiguration basierend auf der URL
|
|
is_prod = os.getenv("APP_URL") == "https://liga.au-fab.eu"
|
|
|
|
# Prefix für die Umgebungsvariablen wählen
|
|
prefix = "DB_" if is_prod else "DB_TEST_"
|
|
|
|
try:
|
|
connection = pymysql.connect(
|
|
host=os.getenv("DB_HOST", "localhost"),
|
|
port=int(os.getenv("DB_PORT", 3306)),
|
|
user=os.getenv(f"{prefix}USER"),
|
|
password=os.getenv(f"DB_PASSWORD"),
|
|
database=os.getenv(f"{prefix}NAME")
|
|
)
|
|
|
|
return connection
|
|
|
|
except pymysql.Error as e:
|
|
raise RuntimeError(f"Fehler beim Verbinden mit {'Prod' if is_prod else 'Test'}-DB: {e}")
|