updated aplications database

This commit is contained in:
ske087
2025-11-22 22:26:23 +02:00
parent 4d6bd537e3
commit c6e254c390
3 changed files with 105 additions and 24 deletions

View File

@@ -17,12 +17,57 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DailyMirrorDatabase:
def __init__(self, host='localhost', user='trasabilitate', password='Initial01!', database='trasabilitate'):
self.host = host
self.user = user
self.password = password
self.database = database
def __init__(self, host=None, user=None, password=None, database=None):
"""Initialize database connection parameters.
If not provided, will read from external_server.conf"""
# If parameters not provided, read from config file
if host is None or user is None or password is None or database is None:
config = self._read_db_config()
self.host = config.get('host', 'db')
self.user = config.get('user', 'trasabilitate')
self.password = config.get('password', 'Initial01!')
self.database = config.get('database', 'trasabilitate')
else:
self.host = host
self.user = user
self.password = password
self.database = database
self.connection = None
def _read_db_config(self):
"""Read database configuration from external_server.conf"""
try:
from flask import current_app
settings_file = os.path.join(current_app.instance_path, 'external_server.conf')
if os.path.exists(settings_file):
settings = {}
with open(settings_file, 'r') as f:
for line in f:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith('#'):
continue
if '=' in line:
key, value = line.split('=', 1)
settings[key] = value
return {
'host': settings.get('server_domain', 'db'),
'user': settings.get('username', 'trasabilitate'),
'password': settings.get('password', 'Initial01!'),
'database': settings.get('database_name', 'trasabilitate')
}
except Exception as e:
logger.warning(f"Could not read config file, using defaults: {e}")
# Fallback defaults for Docker environment
return {
'host': 'db',
'user': 'trasabilitate',
'password': 'Initial01!',
'database': 'trasabilitate'
}
def connect(self):
"""Establish database connection"""
@@ -33,7 +78,7 @@ class DailyMirrorDatabase:
password=self.password,
database=self.database
)
logger.info("Database connection established")
logger.info(f"Database connection established to {self.host}")
return True
except Exception as e:
logger.error(f"Database connection failed: {e}")