create static method

APIServer create(
  1. APIRoot apiRoot, [
  2. List<String> args = const <String>[],
  3. int argsOffset = 0
])

Creates an APIServer with apiRoot.

Implementation

static APIServer create(APIRoot apiRoot,
    [List<String> args = const <String>[], int argsOffset = 0]) {
  if (argsOffset > args.length) {
    argsOffset = args.length;
  }

  if (argsOffset > 0) {
    args = args.sublist(argsOffset);
  }

  String address;
  int port;
  var hotReload = false;
  String? configFile;

  if (args.isEmpty) {
    address = 'localhost';
    port = 8080;
  } else if (args.length == 1) {
    var a = args[0];
    var p = int.tryParse(a);

    if (p != null) {
      if (p >= 80) {
        address = 'localhost';
        port = p;
      } else {
        address = '$p';
        port = 8080;
      }
    } else {
      address = a;
      port = 8080;
    }
  } else {
    address = _parseArg(args, 'address', 'a', 'localhost', 0);
    port = int.parse(_parseArg(args, 'port', 'p', '8080', 1));

    var hotReloadStr =
        _parseArg(args, 'hotreload', 'r', 'false', 2, flag: true)
            .toLowerCase();

    hotReload = hotReloadStr == 'true' || hotReloadStr == 'hotreload';

    configFile = _parseArg(args, 'config', 'i', 'api-local.yaml', 3);
  }

  if (configFile != null) {
    var apiConfig = APIConfig.fromSync(configFile);
    if (apiConfig != null) {
      apiRoot.apiConfig = apiConfig;
    }
  }

  var apiServer = APIServer(apiRoot, address, port, hotReload: hotReload);

  return apiServer;
}