Future main()

in lib/services_cloud_run.dart [24:66]


Future<void> main(List<String> args) async {
  final parser = ArgParser()
    ..addOption('channel', mandatory: true)
    ..addOption('port', abbr: 'p')
    ..addOption('redis-url');
  final results = parser.parse(args);

  final channel = results['channel'] as String;
  final sdk = Sdk.create(channel);

  // Cloud Run supplies the port to bind to in the environment.
  // Allow command line arg to override environment.
  final port = int.tryParse(results['port'] as String? ?? '') ??
      int.tryParse(Platform.environment['PORT'] ?? '');
  if (port == null) {
    stdout.writeln('Could not parse port value from either environment '
        '"PORT" or from command line argument "--port".');
    exit(1);
  }

  final redisServerUri = results['redis-url'] as String;

  Logger.root.level = Level.FINER;
  Logger.root.onRecord.listen((LogRecord record) {
    print(record);
    if (record.stackTrace != null) print(record.stackTrace);
  });

  final cloudRunEnvVars = Platform.environment.entries
      .where((entry) => entry.key.startsWith('K_'))
      .map((entry) => '${entry.key}: ${entry.value}')
      .join('\n');

  _logger.info('''Initializing dart-services:
    port: $port
    sdkPath: ${sdk.dartSdkPath}
    redisServerUri: $redisServerUri
    Cloud Run Environment variables:
    $cloudRunEnvVars''');

  await EndpointsServer.serve(port, redisServerUri, sdk);
  _logger.info('Listening on port $port');
}