serve method

Future<void> serve({
  1. bool logRequests = false,
})

Starts serving the requests.

Implementation

Future<void> serve({bool logRequests = false}) async {
  if (_servers != null) {
    throw Exception('Already started!');
  }

  _build();

  if (logRequests) {
    log.info("Serving on " + _connectionInfos.join(', '));
  }

  _servers = <HttpServer>[];
  try {
    for (int i = 0; i < _connectionInfos.length; i++) {
      ConnectTo ct = _connectionInfos[i];
      HttpServer server;
      if (ct.securityContext != null) {
        server = await HttpServer.bindSecure(
            ct.address, ct.port, ct.securityContext!,
            shared: ct.multiThread);
      } else {
        server = await HttpServer.bind(ct.address, ct.port,
            shared: ct.multiThread);
      }
      server.autoCompress = autoCompress;
      _servers!.add(server);
    }
  } catch (e) {
    try {
      await close();
    } catch (e) {
      // ignore error
    }
    rethrow;
  }

  for (HttpServer server in _servers!) {
    if (logRequests) {
      server.listen((HttpRequest r) {
        log.info("Req => Method: ${r.method} Url: ${r.uri}");
        _handler(r);
      });
    } else {
      server.listen(_handler);
    }
  }
}