-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sender example for protobuf data (#712)
* Add sender example for protobuf data This example mirrors the existing HTTP sender example except that it adds a protobuf object as the payload instead of a JSON map. Signed-off-by: Kevin Conway <kevinconway@invisionapp.com> * Fix envelope format and dataschema in sample Signed-off-by: Kevin Conway <kevinconway@invisionapp.com> * Add protobuf HTTP receiver sample Signed-off-by: Kevin Conway <kevinconway@invisionapp.com>
- Loading branch information
1 parent
70febdc
commit 7e47d96
Showing
8 changed files
with
396 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2021 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
_ "github.com/cloudevents/sdk-go/binding/format/protobuf/v2" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
p, err := cloudevents.NewHTTP() | ||
if err != nil { | ||
log.Fatalf("failed to create protocol: %s", err.Error()) | ||
} | ||
|
||
c, err := cloudevents.NewClient(p) | ||
if err != nil { | ||
log.Fatalf("failed to create client, %v", err) | ||
} | ||
|
||
log.Printf("will listen on :8080\n") | ||
log.Fatalf("failed to start receiver: %s", c.StartReceiver(ctx, receive)) | ||
} | ||
|
||
func receive(ctx context.Context, event cloudevents.Event) { | ||
log.Printf("%s", event) | ||
payload := &Sample{} | ||
if err := event.DataAs(payload); err != nil { | ||
log.Printf("failed to decode protobuf data: %s", err) | ||
return | ||
} | ||
log.Printf("decoded protobuf: %s", payload.Value) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto3"; | ||
|
||
package pb; | ||
|
||
option go_package = "/;main"; | ||
|
||
message Sample { string value = 1; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Copyright 2021 The CloudEvents Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
pbcloudevents "github.com/cloudevents/sdk-go/binding/format/protobuf/v2" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" | ||
) | ||
|
||
func main() { | ||
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/") | ||
|
||
p, err := cloudevents.NewHTTP() | ||
if err != nil { | ||
log.Fatalf("failed to create protocol: %s", err.Error()) | ||
} | ||
|
||
c, err := cloudevents.NewClient(p, cloudevents.WithTimeNow(), cloudevents.WithUUIDs()) | ||
if err != nil { | ||
log.Fatalf("failed to create client, %v", err) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
data := &Sample{Value: "sample"} | ||
e := cloudevents.NewEvent() | ||
e.SetType("com.cloudevents.sample.sent") | ||
e.SetSource("https://github.com/cloudevents/sdk-go/v2/samples/http/sender-protobuf") | ||
e.SetDataSchema("my-schema-registry://" + string(data.ProtoReflect().Descriptor().FullName())) | ||
_ = e.SetData(pbcloudevents.ContentTypeProtobuf, data) | ||
|
||
res := c.Send(ctx, e) | ||
if cloudevents.IsUndelivered(res) { | ||
log.Printf("Failed to send: %v", res) | ||
} else { | ||
var httpResult *cehttp.Result | ||
if cloudevents.ResultAs(res, &httpResult) { | ||
log.Printf("Sent %d with status code %d", i, httpResult.StatusCode) | ||
} | ||
log.Printf("Send did not return an HTTP response: %s", res) | ||
} | ||
} | ||
} |
Oops, something went wrong.