Future _serve()

in functions_framework/lib/serve.dart [62:105]


Future<void> _serve(
  List<String> args,
  FunctionTarget? Function(String) nameToFunctionTarget,
) async {
  final configFromEnvironment = FunctionConfig.fromEnv();

  final config = FunctionConfig.fromArgs(
    args,
    defaults: configFromEnvironment,
  );

  final functionTarget = nameToFunctionTarget(config.target);
  if (functionTarget == null) {
    throw BadConfigurationException(
      'There is no handler configured for '
      '$environmentKeyFunctionTarget `${config.target}`.',
    );
  }

  if (functionTarget.type == FunctionType.cloudevent &&
      config.functionType == FunctionType.http) {
    // See https://github.com/GoogleCloudPlatform/functions-framework-conformance/issues/58
    throw BadConfigurationException(
      'The configured $environmentKeyFunctionTarget `${config.target}` has a '
      'function type of `cloudevent` which is not compatible with the '
      'configured $environmentKeyFunctionSignatureType of `http`.',
    );
  }

  String? projectId;
  try {
    projectId = await projectIdFromMetadataServer();
  } on BadConfigurationException {
    // NOOP! - we aren't on Google Cloud, so use normal logging
  }
  final loggingMiddleware = createLoggingMiddleware(projectId: projectId);

  await run(
    config.port,
    functionTarget.handler,
    waitForTerminate().then((value) => true),
    loggingMiddleware,
  );
}