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

fix: Allow registering multiple functions with one server for local testing. #143

Merged
merged 5 commits into from
Aug 4, 2022

Conversation

jihuin
Copy link
Contributor

@jihuin jihuin commented Aug 1, 2022

There are two ways to test multiple functions within one server.

  1. Declaratively register multiple functions (e.g. calling functions.HTTP()) and run go run cmd/main.go without setting the env var FUNCTION_TARGET. In this case, all registered functions will be served at "/{func_name}".
  2. Manually register the handers (e.g. calling funcframework.RegisterHTTPFunctionContext()) and run go run cmd/main.go without setting the env var FUNCTION_TARGET. In this case, the registered handlers will be served at the user defined path.

Closes #109

@jihuin jihuin requested a review from anniefu August 1, 2022 18:32
@jihuin jihuin changed the title Allow registering multiple functions with one server for local testing. fix: Allow registering multiple functions with one server for local testing. Aug 1, 2022
internal/registry/registry.go Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
funcframework/framework.go Show resolved Hide resolved
@jihuin jihuin requested a review from anniefu August 3, 2022 21:55
Copy link
Contributor

@anniefu anniefu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for the major refactor! This makes all the code a lot cleaner now.

@@ -39,10 +39,6 @@ const (
fnErrorMessageStderrTmpl = "Function error: %v"
)

var (
handler http.Handler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

if err == nil {
handler = server
if err := registry.Default().RegisterHTTP(path, fn, registry.WithPath(path)); err != nil {
return fmt.Errorf("failed to register function at path %s: %s", path, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] maybe include function target in the error message too? Also, prefer %q for strings instead of %s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name is included so it should be fine. Changed %s to %q

server, err := wrapHTTPFunction(path, fn)
if err == nil {
handler = server
if err := registry.Default().RegisterHTTP(path, fn, registry.WithPath(path)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe just return registry.Default().RegisterHTTP's result directly? Make the error message returned by that more detailed if it isn't detailed enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

EventFn interface{} // Optional: The user's Event function
}

// Option is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] unfinished comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for catching this.

server, err := wrapHTTPFunction(path, fn)
if err == nil {
handler = server
if err := registry.Default().RegisterHTTP(path, fn, registry.WithPath(path)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I kind of forgot these didn't have names... Maybe we should give it a readable name like fmt.Sprintf("function at path %q", path) or something so that error messages that print the function name won't look as weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


if err := RegisterHTTPFunctionContext(context.Background(), "/", func(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still want to have some test that mixes both the older RegisterHTTPFunctionContext and functions.HTTP calls and makes sure things work as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jihuin jihuin merged commit 3cab285 into GoogleCloudPlatform:master Aug 4, 2022
anniefu added a commit to anniefu/functions-framework-go that referenced this pull request Aug 12, 2022
anniefu added a commit that referenced this pull request Aug 12, 2022
jihuin added a commit that referenced this pull request Aug 17, 2022
…testing (#154)

* feat: Allow registering multiple functions with one server for local testing (restore PR #143)

* feat: serve the last func that's not registered declaratively if no target is found

* Store declaratively defined functions in map and others in array

* update error message

* Add test case that multiple options are specified

* update registry reset func

* change the implementation of registry reset func
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

Successfully merging this pull request may close these issues.

Serving multiple functions locally from a single server instance
2 participants