// Config/database_config.dart import 'dart:io'; import 'dart:async'; class DatabaseConfig { // Local MySQL settings static const String localHost = '192.168.88.3'; static const String localUsername = 'guycom'; static const String? localPassword = '3iV59wjRdbuXAPR'; static const String localDatabase = 'guycom'; // static const String localHost = 'localhost'; // static const String localUsername = 'root'; // static const String? localPassword = null; // static const String localDatabase = 'guycom'; // Production (public) MySQL settings static const String prodHost = '102.16.56.177'; // static const String prodHost = '185.70.105.157'; static const String prodUsername = 'guycom'; static const String prodPassword = '3iV59wjRdbuXAPR'; static const String prodDatabase = 'guycom'; static const int port = 3306; static const Duration connectionTimeout = Duration(seconds: 30); static const Duration queryTimeout = Duration(seconds: 15); static const int maxConnections = 10; static const int minConnections = 2; static bool get isDevelopment => true; /// Build config map for connection static Map _buildConfig({ required String host, required String user, required String? password, required String database, }) { return { 'host': host, 'port': port, 'user': user, 'password': password, 'database': database, 'timeout': connectionTimeout.inSeconds, }; } /// TCP check if MySQL server is reachable static Future isServerReachable(String host, {int port = 3306}) async { try { final socket = await Socket.connect(host, port, timeout: Duration(seconds: 2)); socket.destroy(); return true; } catch (_) { return false; } } /// Get smart config (local if reachable, otherwise public) static Future> getSmartConfig() async { if (await isServerReachable(localHost, port: port)) { return _buildConfig( host: localHost, user: localUsername, password: localPassword, database: localDatabase, ); } else { return _buildConfig( host: prodHost, user: prodUsername, password: prodPassword, database: prodDatabase, ); } } /// Validate any config static bool validateConfig(Map config) { try { return config['host']?.toString().isNotEmpty == true && config['database']?.toString().isNotEmpty == true && config['user'] != null; } catch (e) { print("Erreur de validation de la configuration: $e"); return false; } } /// Add retry config static Map addRetry(Map config) { return { ...config, 'retryCount': 3, 'retryDelay': 5000, // ms }; } }