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.

97 lines
2.6 KiB

8 months ago
// Config/database_config.dart
import 'dart:io';
import 'dart:async';
10 months ago
class DatabaseConfig {
8 months ago
// 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';
10 months ago
8 months ago
// Production (public) MySQL settings
8 months ago
static const String prodHost = '102.17.52.31';
10 months ago
static const String prodUsername = 'guycom';
static const String prodPassword = '3iV59wjRdbuXAPR';
static const String prodDatabase = 'guycom';
8 months ago
static const int port = 3306;
10 months ago
static const Duration connectionTimeout = Duration(seconds: 30);
static const Duration queryTimeout = Duration(seconds: 15);
static const int maxConnections = 10;
static const int minConnections = 2;
9 months ago
static bool get isDevelopment => false;
10 months ago
8 months ago
/// 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,
);
10 months ago
} else {
8 months ago
return _buildConfig(
host: prodHost,
user: prodUsername,
password: prodPassword,
database: prodDatabase,
);
10 months ago
}
}
8 months ago
/// Validate any config
static bool validateConfig(Map<String, dynamic> config) {
10 months ago
try {
8 months ago
return config['host']?.toString().isNotEmpty == true &&
config['database']?.toString().isNotEmpty == true &&
config['user'] != null;
10 months ago
} catch (e) {
8 months ago
// print("Erreur de validation de la configuration: $e");
10 months ago
return false;
}
}
8 months ago
/// Add retry config
static Map<String, dynamic> addRetry(Map<String, dynamic> config) {
return {
...config,
'retryCount': 3,
'retryDelay': 5000, // ms
};
10 months ago
}
8 months ago
}