11package main
22
33import (
4+ "context"
45 "encoding/json"
56 "net/http"
6- "context"
77
88 ofctx "github.com/OpenFunction/functions-framework-go/context"
99 "github.com/OpenFunction/functions-framework-go/functions"
@@ -17,16 +17,20 @@ func init() {
1717 functions .WithFunctionMethods ("GET" , "POST" ),
1818 )
1919
20- functions .CloudEvent ("Foo" , foo ,
20+ functions .CloudEvent ("Foo" , foo ,
2121 functions .WithFunctionPath ("/foo/{name}" ),
2222 )
2323
24- functions .OpenFunction ("Bar" , bar ,
24+ functions .OpenFunction ("Bar" , bar ,
2525 functions .WithFunctionPath ("/bar/{name}" ),
2626 functions .WithFunctionMethods ("GET" , "POST" ),
2727 )
2828}
2929
30+ type Message struct {
31+ Data string `json:"data"`
32+ }
33+
3034func hello (w http.ResponseWriter , r * http.Request ) {
3135 vars := ofctx .VarsFromCtx (r .Context ())
3236 response := map [string ]string {
@@ -39,8 +43,15 @@ func hello(w http.ResponseWriter, r *http.Request) {
3943
4044func foo (ctx context.Context , ce cloudevents.Event ) error {
4145 vars := ofctx .VarsFromCtx (ctx )
46+
47+ msg := & Message {}
48+ err := json .Unmarshal (ce .Data (), msg )
49+ if err != nil {
50+ return err
51+ }
52+
4253 response := map [string ]string {
43- string ( ce .Data ()) : vars ["name" ],
54+ msg .Data : vars ["name" ],
4455 }
4556 responseBytes , _ := json .Marshal (response )
4657 klog .Infof ("cloudevent - Data: %s" , string (responseBytes ))
@@ -49,8 +60,14 @@ func foo(ctx context.Context, ce cloudevents.Event) error {
4960
5061func bar (ctx ofctx.Context , in []byte ) (ofctx.Out , error ) {
5162 vars := ofctx .VarsFromCtx (ctx .GetNativeContext ())
63+ msg := & Message {}
64+ err := json .Unmarshal (in , msg )
65+ if err != nil {
66+ return ctx .ReturnOnInternalError (), err
67+ }
68+
5269 response := map [string ]string {
53- string ( in ) : vars ["name" ],
70+ msg . Data : vars ["name" ],
5471 }
5572 responseBytes , _ := json .Marshal (response )
5673 return ctx .ReturnOnSuccess ().WithData (responseBytes ), nil
0 commit comments