start method

Future<bool> start()

Starts the webserver. Returns true if the webserver was started successfully.

Implementation

Future<bool> start() async {
  var templatesDirectory = Directory(path.joinAll(['web', 'templates']));
  await templates.loadAll(templatesDirectory);
  if (templates.isEmpty) {
    logDebug(
        'No webserver relic templates found, template directory path: "${templatesDirectory.path}".');
  }

  try {
    _httpServer = await HttpServer.bind(InternetAddress.anyIPv6, _port);
  } catch (e) {
    stderr.writeln(
      '${DateTime.now().toUtc()} ERROR: Failed to bind socket, Webserver '
      'port $_port may already be in use.',
    );
    stderr.writeln('${DateTime.now().toUtc()} ERROR: $e');
    return false;
  }
  httpServer.autoCompress = true;

  runZonedGuarded(
    _start,
    (e, stackTrace) {
      // Last resort error handling
      stdout.writeln('${DateTime.now()} Relic zoned error: $e');
      stdout.writeln('$stackTrace');
    },
  );

  return true;
}