usesNative static method

bool usesNative({
  1. bool log = false,
})

true when listen will use the native server (dox_http_native). log prints why it does not, once.

Implementation

static bool usesNative({bool log = false}) {
  String? env = Platform.environment['DOX_SERVER']?.toLowerCase();
  ServerBackend backend = switch (env) {
    'dartio' || 'dart_io' || 'dart:io' => ServerBackend.dartIo,
    'native' => ServerBackend.native,
    _ => Dox().config.server,
  };
  if (backend == ServerBackend.dartIo) return false;

  // TODO(native): WebSocket upgrade in dox_http_native. Until then these
  // apps stay on dart:io. `DOX_SERVER=native` forces native anyway (the
  // WebSocket upgrade then fails, Socket.IO long-polling still works).
  if (env != 'native' &&
      (Dox().websocket != null || Route().mounts.isNotEmpty)) {
    if (log) {
      Logger.info('WebSocket or Socket.IO routes: using the dart:io server');
    }
    return false;
  }
  if (!NativeHttpServer.isAvailable) {
    /// always shown, also when the logger is off
    if (log) {
      stderr.writeln(
        '[dox] warning: dox_http_native did not load, using the dart:io '
        'server (${NativeHttpServer.loadError})',
      );
    }
    return false;
  }
  return true;
}