startServer method

Future<void> startServer()

start dox server

await Dox().startServer();

Implementation

Future<void> startServer() async {
  checkAppKey();
  addProviders(config.providers);
  GroupIdentity? group = _group;
  if (group != null) {
    // an isolate of its own group: the primary started it, so it only
    // serves. It does not start isolates, watch signals or exit.
    isWebServer = true;
    await GroupWorker.run(group);
    return;
  }
  _totalIsolate ??= Dox().config.totalIsolate;
  int isolatesToSpawn = _totalIsolate!;

  /// stop here, before any isolate starts, when the native server can
  /// not run. The error says why and how to fix it.
  NativeHttpServer.ensureAvailable();

  /// a bad certificate file stops the start here, once. Else every
  /// isolate fails on it and is started again, in a loop.
  await DoxServer().checkCertificate();

  /// the same for a port that `AppConfig.ports` lists two times
  DoxServer.checkPorts(
    DoxServer.portFor(config.serverPort),
    config.additionalPorts,
    config.ports,
  );

  /// a bad gzip level stops the start here, before a port is open
  RangeError.checkValueInInterval(
    config.compressLevel,
    1,
    12,
    'compressLevel',
  );

  /// the same for the threads of the file pool
  RangeError.checkValueInInterval(config.fileThreads, 1, 64, 'fileThreads');

  /// Windows has no SO_REUSEPORT: one isolate owns the native listener.
  if (Platform.isWindows && isolatesToSpawn > 1) {
    Logger.info(
      'Windows: the native server runs on one isolate',
      null,
      'server',
    );
    isolatesToSpawn = 1;
  }

  isolateCount = isolatesToSpawn;
  isWebServer = true;
  // each isolate group has its own slots, so there the warning is only
  // for the fallback
  bool tryGroups = config.flags.isolateGroups && isolatesToSpawn > 1;
  if (!tryGroups) warnIsolateSlots(isolatesToSpawn, IsolateSlots.available);
  if (isolatesToSpawn > 1) {
    bool groups = await _spawnIsolates(isolatesToSpawn);
    if (tryGroups && !groups) {
      warnIsolateSlots(isolatesToSpawn, IsolateSlots.available);
    }
  }

  await startProviders();
  warnWebSocketOrigin();
  warnCorsCredentials();
  DoxServer().setResponseHandler(config.responseHandler);
  HttpServer server = await DoxServer().listen(
    DoxServer.portFor(config.serverPort),
    address: DoxServer.hostFor(config.host),
    additionalPorts: config.additionalPorts,
    ports: config.ports,
  );
  List<int> others = <int>[
    ...config.additionalPorts,
    for (PortConfig p in config.ports) p.port,
  ];
  String more = others.isEmpty ? '' : ' (and port ${others.join(', ')})';

  Logger.info(
    'Server started at ${serverUrl(server)}$more with $isolatesToSpawn '
        'isolate (native)',
    null,
    'server',
  );
  _watchSignals();
}