serve method

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

Starts serving the requests.

Implementation

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

  _build();

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

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

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