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 1 commit
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
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -216,6 +216,28 @@ 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 functions:

```golang
package function

import (
"net/http"

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

func init() {
funcframework.HTTPFunction("foo", func(w http.ResponseWriter, r *http.Request) {
grant marked this conversation as resolved.
Show resolved Hide resolved
w.Write([]byte("Hello, World!"))
})
}
```

Upon starting, the framework will register your function with the function name in the URL path.
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
47 changes: 47 additions & 0 deletions funcframework/registry.go
@@ -0,0 +1,47 @@
package funcframework

import (
"context"
"net/http"

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

// A declaratively registered function
type RegisteredFunction struct {
Name string
CloudEventFn func(context.Context, cloudevents.Event) error
HTTPFn func(http.ResponseWriter, *http.Request)
}

var (
function_registry = map[string]RegisteredFunction{}
grant marked this conversation as resolved.
Show resolved Hide resolved
)

// Registers a HTTP function with a given name
grant marked this conversation as resolved.
Show resolved Hide resolved
func HTTPFunction(name string, fn func(http.ResponseWriter, *http.Request)) error {
function_registry[name] = RegisteredFunction{
Name: name,
CloudEventFn: nil,
HTTPFn: fn,
}
RegisterHTTPFunctionContext(context.Background(), name, fn)
grant marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// Registers a CloudEvent function with a given name
func CloudEventFunction(name string, fn func(context.Context, cloudevents.Event) error) error {
function_registry[name] = RegisteredFunction{
Name: name,
CloudEventFn: fn,
HTTPFn: nil,
}
RegisterCloudEventFunctionContext(context.Background(), name, fn)
return nil
}

// Gets a registered function by name
func GetRegisteredFunction(name string) (RegisteredFunction, bool) {
fn, ok := function_registry[name]
return fn, ok
}
28 changes: 28 additions & 0 deletions testdata/declarative/declarative_function.go
@@ -0,0 +1,28 @@
// 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 function contains test functions to validate the framework.
package function

import (
"net/http"

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

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