GoogleCloudPlatform / functions-framework-dart

FaaS (Function as a service) framework for writing portable Dart functions
https://pub.dev/packages/functions_framework
Apache License 2.0
537 stars 54 forks source link

Multiple functions in the same package #308

Closed cubuspl42 closed 2 years ago

cubuspl42 commented 2 years ago

If you would like to rename the handler function (function) to something else more descriptive (ex: handleGet), you need to [...]

Referring to "the handler function" and suggesting a descriptive name like handleGet implies that it's correct / typical to have only one function defined in functions.dart file. But it's not the case, right? The file is named functions. And there's a _nameToFunctionTarget generated helper method.

Am I correct to assume that the intended usage is similar to the official Firebase Functions JavaScript SDK, where you can define dozens of functions in the single file?

cubuspl42 commented 2 years ago

I've tried setting up a simple experiment with two functions, foo and bar. Nothing fancy.

@CloudFunction()
Response foo(Request request) => Response.ok('Hello, World! (foo)');

@CloudFunction()
Response bar(Request request) => Response.ok('Hello, World! (bar)');

The server.dart got generated correctly, indicating that this is supported.

FunctionTarget? _nameToFunctionTarget(String name) {
  switch (name) {
    case 'foo':
      return FunctionTarget.http(
        function_library.foo,
      );
    case 'bar':
      return FunctionTarget.http(
        function_library.bar,
      );
    default:
      return null;
  }
}

But after building a container using the provided Dockerfile, it fails to start:

$ docker container run --name my-container-1 -p 8081:8080 my-image-1
There is no handler configured for FUNCTION_TARGET `function`.

How can I build (& deploy to the Cloud Run, preferably) such trivial two-functions package?

kevmoo commented 2 years ago

The default function is function. See https://github.com/GoogleCloudPlatform/functions-framework#specification-summary

You need to provide the --target flag to specify either foo or bar

cubuspl42 commented 2 years ago

@kevmoo Thank you very much for your answer, but please be aware that from my perspective this issue is not closed.

Thank you for a link to the functions-framework project. Would you point me to the place in the functions-framework-dart project that states that these two projects are interconnected? I can see that the names are similar and I can guess the relationship between them, but stating such relationship in the readme / docs is necessary, in my opinion. I wouldn't know how the functions-framework-dart user is expected to guess that they should look for information in the functions-framework#specification-summary document.

I might should have noted, that such direct docker container run invocation was just a "MCVE". I don't intend to invoke this code in such way. In a real life scenario, a similar (cryptic, for me) message was buried deeply in a Cloud Run logs, when my container exploded during start-up.

I'll also repeat the questions from my original posts.

Am I correct to assume that the intended usage (of functions-framework-dart) is similar to the official Firebase Functions JavaScript SDK, where you can define dozens of functions in the single file?

Should I conclude that the answer is "yes"?

How can I build (& deploy to the Cloud Run, preferably) such trivial two-functions package?

Does the provided Dockerfile, which is picked up by gcloud beta run deploy command as per Quickstart: Cloud Run docs support a project with multiple functions (or, in other words, multiple functions with non-default names)?

Am I right to assume that if I have multiple functions in my functions-framework-dart -based project, I'll need a separate Cloud Run service per single in-code @CloudFunction()?