You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

96 lines
2.6 KiB

// Config/database_config.dart
import 'dart:io';
import 'dart:async';
class DatabaseConfig {
// Local MySQL settings
static const String localHost = '192.168.88.73';
static const String localUsername = 'guycom';
static const String? localPassword = '3iV59wjRdbuXAPR';
static const String localDatabase = 'guycom';
// Production (public) MySQL settings
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 => false;
/// Build config map for connection
static Map<String, dynamic> _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<bool> 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<Map<String, dynamic>> 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<String, dynamic> 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<String, dynamic> addRetry(Map<String, dynamic> config) {
return {
...config,
'retryCount': 3,
'retryDelay': 5000, // ms
};
}
}