Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple functions in the same package #308

Closed
cubuspl42 opened this issue Dec 23, 2021 · 3 comments
Closed

Multiple functions in the same package #308

cubuspl42 opened this issue Dec 23, 2021 · 3 comments

Comments

@cubuspl42
Copy link

cubuspl42 commented Dec 23, 2021

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
Copy link
Author

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
Copy link
Collaborator

kevmoo commented Dec 29, 2021

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

@kevmoo kevmoo closed this as completed Dec 29, 2021
@cubuspl42
Copy link
Author

@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()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants