Big improve on selection of GVA endpoint at startup
This commit is contained in:
parent
0aa5d8a5af
commit
27ae053af0
|
@ -1,4 +1,5 @@
|
|||
[
|
||||
"https://g1.librelois.fr/gva",
|
||||
"http://localhost:30901/gva"
|
||||
"https://g1.librelois.fr/gva",
|
||||
"https://duniter-gva.axiom-team.fr/gva",
|
||||
"https://duniter-g1.p2p.legal/gva"
|
||||
]
|
||||
|
|
|
@ -32,22 +32,8 @@ Future<void> main() async {
|
|||
final HiveStore _store =
|
||||
await HiveStore.open(path: '${appPath.path}/gqlCache');
|
||||
|
||||
String _randomEndpoint;
|
||||
int i = 0;
|
||||
do {
|
||||
if (i >= 5) {
|
||||
print('NO VALID ENDPOINT FOUND !');
|
||||
break;
|
||||
}
|
||||
if (i != 0) {
|
||||
print(i.toString() + ' ème essai de recherche de endpoint GVA.');
|
||||
await Future.delayed(Duration(milliseconds: 300));
|
||||
}
|
||||
_randomEndpoint = await _homeProvider.getRandomEndpoint();
|
||||
i++;
|
||||
} while (_randomEndpoint == 'HS');
|
||||
|
||||
endPointGVA = _randomEndpoint;
|
||||
// Get a valid GVA endpoint
|
||||
endPointGVA = await _homeProvider.getValidEndpoint();
|
||||
|
||||
if (kReleaseMode && enableSentry) {
|
||||
await SentryFlutter.init(
|
||||
|
@ -55,12 +41,12 @@ Future<void> main() async {
|
|||
options.dsn =
|
||||
'https://c09587b46eaa42e8b9fda28d838ed180@o496840.ingest.sentry.io/5572110';
|
||||
},
|
||||
appRunner: () => runApp(Gecko(_randomEndpoint, _store)),
|
||||
appRunner: () => runApp(Gecko(endPointGVA, _store)),
|
||||
);
|
||||
} else {
|
||||
print('Debug mode enabled: No sentry alerte');
|
||||
|
||||
runApp(Gecko(_randomEndpoint, _store));
|
||||
runApp(Gecko(endPointGVA, _store));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
@ -28,28 +29,49 @@ class HomeProvider with ChangeNotifier {
|
|||
return version + '+' + buildNumber;
|
||||
}
|
||||
|
||||
Future<String> getRandomEndpoint() async {
|
||||
// TODO: Improve implemention of getRandomEndpoint()
|
||||
// final _json = json.decode(await getJsonEndpoints());
|
||||
// print('JSON !! :');
|
||||
// print(_json);
|
||||
// final _list = _json[];
|
||||
Future<String> getValidEndpoint() async {
|
||||
List _listEndpoints = await rootBundle
|
||||
.loadString('config/gva_endpoints.json')
|
||||
.then((jsonStr) => jsonDecode(jsonStr));
|
||||
|
||||
int i = 0;
|
||||
http.Response response;
|
||||
_listEndpoints.shuffle();
|
||||
String _endpoint;
|
||||
int statusCode = 0;
|
||||
|
||||
final client = new HttpClient();
|
||||
client.connectionTimeout = const Duration(seconds: 1);
|
||||
|
||||
do {
|
||||
i++;
|
||||
print(i.toString() + ' ème essai de recherche de endpoint GVA.');
|
||||
try {
|
||||
if (i > 5) {
|
||||
break;
|
||||
}
|
||||
if (i != 0) {
|
||||
await Future.delayed(Duration(milliseconds: 300));
|
||||
}
|
||||
response = await http
|
||||
.post(_listEndpoints[i])
|
||||
.timeout(const Duration(seconds: 1));
|
||||
} on TimeoutException catch (_) {
|
||||
print(_listEndpoints[i] + ' is timeout, next');
|
||||
statusCode = 50;
|
||||
|
||||
continue;
|
||||
} on SocketException catch (_) {
|
||||
print(_listEndpoints[i] + ' is a bad endpoint, next');
|
||||
statusCode = 70;
|
||||
continue;
|
||||
}
|
||||
_endpoint = _listEndpoints[i];
|
||||
statusCode = response.statusCode;
|
||||
print('Endpoint statutcode: ' + statusCode.toString());
|
||||
} while (statusCode != 400);
|
||||
|
||||
final _listEndpoints = [
|
||||
'https://g1.librelois.fr/gva',
|
||||
'https://duniter-gva.axiom-team.fr/gva',
|
||||
'https://duniter-g1.p2p.legal/gva'
|
||||
];
|
||||
final _endpoint = getRandomElement(_listEndpoints);
|
||||
print('ENDPOINT: ' + _endpoint);
|
||||
|
||||
// http.post(_endpoint);
|
||||
final response = await http.post(_endpoint);
|
||||
if (response.statusCode != 400) {
|
||||
print('Endpoint statutcode: ' + response.statusCode.toString());
|
||||
return 'HS';
|
||||
}
|
||||
|
||||
return _endpoint;
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ flutter:
|
|||
|
||||
assets:
|
||||
- images/
|
||||
- config/
|
||||
- config/gva_endpoints.json
|
||||
- assets/icon/gecko_final.png
|
||||
- assets/
|
||||
- assets/OpenSans-Regular.ttf
|
||||
|
|
Loading…
Reference in New Issue