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
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions .github/workflows/conformance.yml
Expand Up @@ -16,18 +16,14 @@ jobs:
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
with:
go-version: '${{ matrix.go-version }}'

- name: Pre-fetch go dependencies and build
run: 'go build ./...'

- name: Pre-install conformance test client
run: 'go get github.com/GoogleCloudPlatform/functions-framework-conformance/client@v1.2.1 && go install github.com/GoogleCloudPlatform/functions-framework-conformance/client'

- name: Run HTTP conformance tests
uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1
with:
Expand All @@ -36,7 +32,6 @@ jobs:
useBuildpacks: false
cmd: "'go run testdata/conformance/cmd/http/main.go'"
startDelay: 5

- name: Run event conformance tests
uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1
with:
Expand All @@ -46,7 +41,6 @@ jobs:
useBuildpacks: false
cmd: "'go run testdata/conformance/cmd/legacyevent/main.go'"
startDelay: 5

- name: Run CloudEvent conformance tests
uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1
with:
Expand All @@ -56,3 +50,23 @@ jobs:
useBuildpacks: false
cmd: "'go run testdata/conformance/cmd/cloudevent/main.go'"
startDelay: 5
- name: Run HTTP conformance tests using declarative API
uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1
env:
FUNCTION_TARGET: 'declarativeHTTP'
with:
version: 'v1.2.1'
functionType: 'http'
useBuildpacks: false
cmd: "'go run testdata/conformance/cmd/declarative/main.go'"
startDelay: 5
- name: Run CloudEvent conformance tests using declarative API
uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1
env:
FUNCTION_TARGET: 'declarativeCE'
grant marked this conversation as resolved.
Show resolved Hide resolved
with:
version: 'v1.2.1'
functionType: 'cloudevent'
useBuildpacks: false
cmd: "'go run testdata/conformance/cmd/declarative/main.go'"
startDelay: 5
grant marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -216,6 +216,36 @@ These functions are registered with the handler via `funcframework.RegisterCloud

To learn more about CloudEvents, see the [Go SDK for CloudEvents](https://github.com/cloudevents/sdk-go).

### Declarative Functions

The Functions Framework also provides a way to declaratively define `HTTP` and `CloudEvent` functions:

```golang
package function

import (
"net/http"

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

func init() {
funcframework.HTTP("hello", HelloWorld)
funcframework.HTTP("ce", CloudEvent)
grant marked this conversation as resolved.
Show resolved Hide resolved
}

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

func CloudEvent(ctx context.Context, e cloudevents.Event) error {
// Do something with event.Context and event.Data (via event.DataAs(foo)).
return nil
}
```

Upon starting, the framework will listen to HTTP requests at `/` and invoke your registered function (specified `FUNCTION_TARGET=hello`).
grant marked this conversation as resolved.
Show resolved Hide resolved

[ff_go_unit_img]: https://github.com/GoogleCloudPlatform/functions-framework-go/workflows/Go%20Unit%20CI/badge.svg
[ff_go_unit_link]: https://github.com/GoogleCloudPlatform/functions-framework-go/actions?query=workflow%3A"Go+Unit+CI"
[ff_go_lint_img]: https://github.com/GoogleCloudPlatform/functions-framework-go/workflows/Go%20Lint%20CI/badge.svg
Expand Down
42 changes: 41 additions & 1 deletion funcframework/framework.go
Expand Up @@ -27,6 +27,7 @@ import (
"runtime/debug"
"strings"

"github.com/GoogleCloudPlatform/functions-framework-go/internal/registry"
cloudevents "github.com/cloudevents/sdk-go/v2"
)

Expand Down Expand Up @@ -96,11 +97,50 @@ func RegisterCloudEventFunctionContext(ctx context.Context, path string, fn func
return registerCloudEventFunction(ctx, path, fn, handler)
}

// Declaratively registers a HTTP function.
func HTTP(name string, fn func(http.ResponseWriter, *http.Request)) {
defer recoverPanic("Registration panic")
grant marked this conversation as resolved.
Show resolved Hide resolved

// Register the function.
registry.RegisterHTTP(name, fn)
grant marked this conversation as resolved.
Show resolved Hide resolved
}

// Declaratively registers a CloudEvent function.
func CloudEvent(name string, fn func(context.Context, cloudevents.Event) error) {
defer recoverPanic("Registration panic")

// Register the function.
registry.RegisterCloudEvent(name, fn)
}

// Start serves an HTTP server with registered function(s).
func Start(port string) error {
// If FUNCTION_TARGET, try to start with that registered function
grant marked this conversation as resolved.
Show resolved Hide resolved
// If not set, assume non-declarative functions.
target := os.Getenv("FUNCTION_TARGET")
if target == "" {
grant marked this conversation as resolved.
Show resolved Hide resolved
// Default function target
target = "function"
}

// Check if we have a function resource set, and if so, log progress.
if os.Getenv("K_SERVICE") == "" {
fmt.Println("Serving function...")
fmt.Printf("Serving function: %s\n", target)
}

// Check if there's a registered function, and use if possible
fn, hasRegisteredFn := registry.GetRegisteredFunction(target)
grant marked this conversation as resolved.
Show resolved Hide resolved
if hasRegisteredFn {
ctx := context.Background()
if fn.HTTPFn != nil {
if err := registerHTTPFunction("/", fn.HTTPFn, handler); err != nil {
panic(fmt.Sprintf("unexpected error in registerHTTPFunction: %v", err))
grant marked this conversation as resolved.
Show resolved Hide resolved
}
} else if fn.CloudEventFn != nil {
if err := registerCloudEventFunction(ctx, "/", fn.CloudEventFn, handler); err != nil {
panic(fmt.Sprintf("unexpected error in registerCloudEventFunction: %v", err))
}
}
}

return http.ListenAndServe(":"+port, handler)
Expand Down
52 changes: 52 additions & 0 deletions internal/registry/registry.go
@@ -0,0 +1,52 @@
package registry

import (
"context"
"fmt"
"net/http"

cloudevents "github.com/cloudevents/sdk-go/v2"
)

// A declaratively registered function
type RegisteredFunction struct {
grant marked this conversation as resolved.
Show resolved Hide resolved
Name string // The name of the function
CloudEventFn func(context.Context, cloudevents.Event) error // Optional: The user's CloudEvent function
HTTPFn func(http.ResponseWriter, *http.Request) // Optional: The user's HTTP function
}

var (
function_registry = map[string]RegisteredFunction{}
)

// Registers a HTTP function with a given name
func RegisterHTTP(name string, fn func(http.ResponseWriter, *http.Request)) error {
if _, ok := function_registry[name]; ok {
return fmt.Errorf("function name already registered: %s", name)
}
function_registry[name] = RegisteredFunction{
Name: name,
CloudEventFn: nil,
HTTPFn: fn,
}
return nil
}

// Registers a CloudEvent function with a given name
func RegisterCloudEvent(name string, fn func(context.Context, cloudevents.Event) error) error {
if _, ok := function_registry[name]; ok {
return fmt.Errorf("function name already registered: %s", name)
}
function_registry[name] = RegisteredFunction{
Name: name,
CloudEventFn: fn,
HTTPFn: nil,
}
return nil
}

// Gets a registered function by name
func GetRegisteredFunction(name string) (RegisteredFunction, bool) {
fn, ok := function_registry[name]
grant marked this conversation as resolved.
Show resolved Hide resolved
return fn, ok
grant marked this conversation as resolved.
Show resolved Hide resolved
}
83 changes: 83 additions & 0 deletions internal/registry/registry_test.go
@@ -0,0 +1,83 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package registry

import (
"context"
"fmt"
"net/http"
"testing"

cloudevents "github.com/cloudevents/sdk-go/v2"
)

func TestRegisterHTTP(t *testing.T) {
RegisterHTTP("httpfn", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
})

fn, ok := GetRegisteredFunction("httpfn")
if !ok {
t.Error("Expected function to be registered")
grant marked this conversation as resolved.
Show resolved Hide resolved
}
if fn.Name != "httpfn" {
t.Errorf("Expected function name to be 'httpfn', got %s", fn.Name)
}
}

func TestRegisterCE(t *testing.T) {
RegisterCloudEvent("cefn", func(context.Context, cloudevents.Event) error {
return nil
})

fn, ok := GetRegisteredFunction("cefn")
if !ok {
t.Error("Expected function to be registered")
}
if fn.Name != "cefn" {
t.Errorf("Expected function name to be 'cefn', got %s", fn.Name)
}
}

func TestRegisterMultipleFunctions(t *testing.T) {
if ok := RegisterHTTP("multifn1", func(w http.ResponseWriter, r *http.Request) {
grant marked this conversation as resolved.
Show resolved Hide resolved
fmt.Fprint(w, "Hello World!")
}); ok != nil {
t.Error("Expected \"multifn1\" function to be registered")
}
if ok := RegisterHTTP("multifn2", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World 2!")
}); ok != nil {
t.Error("Expected \"multifn2\" function to be registered")
}
if ok := RegisterCloudEvent("multifn3", func(context.Context, cloudevents.Event) error {
return nil
}); ok != nil {
t.Error("Expected \"multifn3\" function to be registered")
}
}

func TestRegisterMultipleFunctionsError(t *testing.T) {
if err := RegisterHTTP("samename", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
}); err != nil {
t.Error("Expected no error registering function")
}

if err := RegisterHTTP("samename", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World 2!")
}); err == nil {
t.Error("Expected error registering function with same name")
}
}
37 changes: 37 additions & 0 deletions testdata/conformance/cmd/declarative/main.go
@@ -0,0 +1,37 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Binary that serves the HTTP conformance test function.
package main

import (
"log"
"os"

"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
_ "github.com/GoogleCloudPlatform/functions-framework-go/testdata/conformance/function"
)

// Main function used for testing only:
// FUNCTION_TARGET=declarativeHTTP go run testdata/conformance/cmd/declarative/main.go
// FUNCTION_TARGET=declarativeCE go run testdata/conformance/cmd/declarative/main.go
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)
}
}
12 changes: 10 additions & 2 deletions testdata/conformance/function/function.go
Expand Up @@ -24,7 +24,7 @@ import (
"net/http"

"cloud.google.com/go/functions/metadata"
_ "github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
cloudevents "github.com/cloudevents/sdk-go/v2"
)

Expand Down Expand Up @@ -69,7 +69,6 @@ func Event(ctx context.Context, data interface{}) error {
}

// CloudEvent is a cloud event function that dumps the event to JSON and calls the validator script
// on the result.
func CloudEvent(ctx context.Context, ce cloudevents.Event) error {
e, err := json.Marshal(ce)
if err != nil {
Expand All @@ -82,3 +81,12 @@ func CloudEvent(ctx context.Context, ce cloudevents.Event) error {

return nil
}

// Register declarative functions
func init() {
fmt.Println("Registering function: declarativeHTTP")
grant marked this conversation as resolved.
Show resolved Hide resolved
funcframework.HTTP("declarativeHTTP", HTTP)

fmt.Println("Registering function: declarativeCE")
funcframework.CloudEvent("declarativeCE", CloudEvent)
}