handle method

Future<DoxResponse> handle(
  1. DoxRequest req
)

The route controller.

Implementation

Future<DoxResponse> handle(DoxRequest req) async {
  if (checks.isEmpty) return response(<String, String>{'status': 'ok'});
  List<String> names = checks.keys.toList();
  List<Object?> errors = await Future.wait(<Future<Object?>>[
    for (String name in names) _run(checks[name]!),
  ]);
  Map<String, String> results = <String, String>{};
  Map<String, String> details = <String, String>{};
  for (int i = 0; i < names.length; i++) {
    Object? error = errors[i];
    results[names[i]] = error == null ? 'ok' : 'fail';
    if (error != null) details[names[i]] = '$error';
  }
  bool ok = details.isEmpty;
  return response(<String, dynamic>{
    'status': ok ? 'ok' : 'fail',
    'checks': results,
    if (!ok && _showErrors()) 'errors': details,
  }).statusCode(ok ? HttpStatus.ok : HttpStatus.serviceUnavailable);
}