Adding sample function in go template (#840)

* adding sample function in go template

* adding simple echo, using the correct signature

* fixing readme
This commit is contained in:
salaboy 2022-03-01 15:38:42 +00:00 committed by GitHub
parent bea34d21a0
commit b97fe9c4ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 13 deletions

2
.gitignore vendored
View File

@ -7,7 +7,7 @@
/coverage.out
/bin
/target
.DS_Store
node_modules
__pycache__
/coverage.out

View File

@ -1,6 +1,6 @@
# Go Cloud Events Function
Welcome to your new Go Function! The boilerplate function code can be found in [`handle.go`](handle.go). This Function is meant to respond exclusively to [Cloud Events](https://cloudevents.io/), but you can remove the check for this in the function and it will respond just fine to plain vanilla incoming HTTP requests.
Welcome to your new Go Function! The boilerplate function code can be found in [`handle.go`](handle.go). This Function is meant to respond exclusively to [Cloud Events](https://cloudevents.io/), but you can remove the check for this in the function and it will respond just fine to plain vanilla incoming HTTP requests.
## Development
@ -9,13 +9,14 @@ Develop new features by adding a test to [`handle_test.go`](handle_test.go) for
Update the running analog of the function using the `func` CLI or client library, and it can be invoked using a manually-created CloudEvent:
```console
curl -X POST -d '{"hello": "world"}' \
curl -v -X POST -d 'hello' \
-H'Content-type: application/json' \
-H'Ce-id: 1' \
-H'Ce-source: cloud-event-example' \
-H'Ce-type: dev.knative.example' \
-H'Ce-subject: Echo content' \
-H'Ce-type: MyEvent' \
-H'Ce-specversion: 1.0' \
http://myFunction.example.com/
http://localhost:8080/
```
For more, see [the complete documentation]('https://github.com/knative-sandbox/kn-plugin-func/tree/main/docs')

View File

@ -2,13 +2,16 @@ package function
import (
"context"
"encoding/json"
"fmt"
event "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
// Handle an event.
func Handle(ctx context.Context, event event.Event) error {
func Handle(ctx context.Context, event cloudevents.Event) (*event.Event, error) {
/*
* YOUR CODE HERE
@ -16,10 +19,23 @@ func Handle(ctx context.Context, event event.Event) error {
* Try running `go test`. Add more test as you code in `handle_test.go`.
*/
// Example implementation:
fmt.Printf("%v\n", event) // print the received event to standard output
fmt.Printf("Incoming Event: %v\n", event) // print the received event to standard output
payload := ""
err := json.Unmarshal(event.Data(), &payload)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
}
return nil
payload = "echo " + payload
outputEvent := cloudevents.NewEvent()
outputEvent.SetSource("http://example.com/echo")
outputEvent.SetType("Echo")
outputEvent.SetData(cloudevents.ApplicationJSON, &payload)
fmt.Printf("Outgoing Event: %v\n", outputEvent)
return &outputEvent, nil
}
/*

View File

@ -2,8 +2,11 @@ package function
import (
"context"
"encoding/json"
"testing"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
)
@ -12,11 +15,30 @@ func TestHandle(t *testing.T) {
// A minimal, but valid, event.
event := event.New()
event.SetID("TEST-EVENT-01")
event.SetType("com.example.event.test")
event.SetType("MyEvent")
event.SetSource("http://localhost:8080/")
event.SetSubject("Echo")
input := "hello"
event.SetData(cloudevents.ApplicationJSON, &input)
// Invoke the defined handler.
if err := Handle(context.Background(), event); err != nil {
ce, err := Handle(context.Background(), event)
if err != nil {
t.Fatal(err)
}
if ce == nil {
t.Errorf("The output CloudEvent cannot be nil")
}
if ce.Type() != "Echo" {
t.Errorf("Wrong CloudEvent Type received: %v , expected Echo", ce.Type())
}
output := ""
err = json.Unmarshal(ce.Data(), &output)
if err != nil {
t.Fatal(err)
}
if output != "echo "+input {
t.Errorf("The expected output should be: 'echo hello and it was: %v", output)
}
}