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

feat: init declarative functions go #92

Merged
merged 18 commits into from Nov 2, 2021
Merged

Conversation

grant
Copy link
Contributor

@grant grant commented Oct 28, 2021

Declarative functions interface for the Go Functions Framework.

Example usage

main.go

Sample main package used for local development only.

package main

import (
	"os"

	"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
)

func main() {
	port := "8080"
	if envPort := os.Getenv("PORT"); envPort != "" {
		port = envPort
	}
	if err := funcframework.Start(port); err != nil {
		log.Fatalf("Failed to start functions framework: %v", err)
	}
}

function.go

Sample declarative function, registered in init.

package function

import (
	"net/http"
	"os"

	"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
)

func HelloWorld(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
}

func init() {
	// Register your function named "hello"
	funcframework.HTTP("hello", HelloWorld)
}

Features

  • Exposes new functions HTTP and CloudEvent that allows you to declaratively register a function with a given name.
  • Creates a function registry (similar to python and Node). I expect the registry to be more useful as we add more features to declarative functions.
  • Adds sample declarative functions for conformance tests.
  • Adds sample runnable functions in testdata.
  • Adds basic docs in the README.md.

Example tests

  • go run testdata/declarative/http/main.go
    • 2021/10/28 15:38:50 Listening to function "http" at http://localhost:8080/ Serving function...
  • go run testdata/declarative/cloudevent/main.go
    • 2021/10/28 15:40:54 Listening to function "cloudevent" at http://localhost:8080/ Serving function...

Example conformance functions

FUNCTION_TARGET=declarativeHTTP go run testdata/conformance/cmd/declarative/main.go
FUNCTION_TARGET=declarativeCE go run testdata/conformance/cmd/declarative/main.go

I'm not an expert in Go, so feel free to suggest any changes!

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
@grant grant requested a review from anniefu October 28, 2021 19:35
@grant grant self-assigned this Oct 28, 2021
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
funcframework/registry.go Outdated Show resolved Hide resolved
funcframework/registry.go Outdated Show resolved Hide resolved
funcframework/registry.go Outdated Show resolved Hide resolved
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Copy link
Contributor Author

@grant grant left a comment

Choose a reason for hiding this comment

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

I've made some significant changes and added a handful of example functions.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
funcframework/registry.go Outdated Show resolved Hide resolved
funcframework/registry.go Outdated Show resolved Hide resolved
funcframework/registry.go Outdated Show resolved Hide resolved
)

func init() {
log.Print("Listening to function \"hello\" at http://localhost:8080/")
Copy link
Contributor

Choose a reason for hiding this comment

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

Registration should be done in the function package conformance/function/function.go, or a new one if you prefer. The GCP buildpacks ignore the main.go, so this would not work in production.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the conformance main.gos are just for testing – we're mirroring the existing testing main.gos here.

https://github.com/GoogleCloudPlatform/functions-framework-go/blob/master/testdata/conformance/cmd/http/main.go

I think we're going to need multiple conformance/function/function.go packages to be able to test multiple different declarative functions if going that route.

funcframework/framework.go Outdated Show resolved Hide resolved
internal/registry/registry.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@grant
Copy link
Contributor Author

grant commented Oct 29, 2021

TODO: Look at modifying test handlers in funcframework/framework_test.go

	for _, tc := range tests {
		h := http.NewServeMux()
		if err := registerEventFunction("/", tc.fn, h); err != nil {
			t.Fatalf("registerEventFunction(): %v", err)
		}

		srv := httptest.NewServer(h)
		defer srv.Close()

->

	for _, tc := range tests {
		handler = http.NewServeMux()
		if err := RegisterEventFunctionContext(context.Background(), "/", tc.fn); err != nil {
			t.Fatalf("registerEventFunction(): %v", err)
		}

		srv := httptest.NewServer(handler)
		defer srv.Close()

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
@grant grant requested a review from anniefu November 1, 2021 22:19
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
README.md Outdated Show resolved Hide resolved
internal/registry/registry.go Show resolved Hide resolved
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
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!! It's pretty much there. Mostly just nits and a couple of implementation details.

README.md Outdated Show resolved Hide resolved
testdata/conformance/function/function.go Outdated Show resolved Hide resolved
.github/workflows/conformance.yml Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
internal/registry/registry.go Show resolved Hide resolved
internal/registry/registry_test.go Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
funcframework/framework.go Show resolved Hide resolved
internal/registry/registry_test.go Outdated Show resolved Hide resolved
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Copy link
Contributor

@KaylaNguyen KaylaNguyen left a comment

Choose a reason for hiding this comment

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

Looks great!

internal/registry/registry.go Show resolved Hide resolved
.github/workflows/conformance.yml Outdated Show resolved Hide resolved
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
@grant grant requested a review from anniefu November 2, 2021 19:43
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.

Could you modify the PR description too so that the function registration happens in outside of main.go?

funcframework/framework_test.go Outdated Show resolved Hide resolved
funcframework/framework.go Outdated Show resolved Hide resolved
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
@grant
Copy link
Contributor Author

grant commented Nov 2, 2021

Could you modify the PR description too so that the function registration happens in outside of main.go?

Done!

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.

None yet

3 participants