diff --git a/samples/http/go.mod b/samples/http/go.mod index 88e95f75a..b4da920e5 100644 --- a/samples/http/go.mod +++ b/samples/http/go.mod @@ -4,14 +4,18 @@ go 1.14 require ( contrib.go.opencensus.io/exporter/prometheus v0.1.0 + github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.5.0 github.com/cloudevents/sdk-go/observability/opencensus/v2 v2.5.0 github.com/cloudevents/sdk-go/v2 v2.5.0 github.com/google/uuid v1.1.1 github.com/gorilla/mux v1.7.3 github.com/kelseyhightower/envconfig v1.4.0 go.opencensus.io v0.22.3 + google.golang.org/protobuf v1.26.0 ) replace github.com/cloudevents/sdk-go/v2 => ../../v2 +replace github.com/cloudevents/sdk-go/binding/format/protobuf/v2 => ../../binding/format/protobuf/v2 + replace github.com/cloudevents/sdk-go/observability/opencensus/v2 => ../../observability/opencensus/v2 diff --git a/samples/http/go.sum b/samples/http/go.sum index 2aea98989..c04b474e0 100644 --- a/samples/http/go.sum +++ b/samples/http/go.sum @@ -24,14 +24,16 @@ github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:x github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -156,8 +158,10 @@ google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/samples/http/sender-protobuf/main.go b/samples/http/sender-protobuf/main.go new file mode 100644 index 000000000..bb99aba12 --- /dev/null +++ b/samples/http/sender-protobuf/main.go @@ -0,0 +1,47 @@ +// +// 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(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 + cloudevents.ResultAs(res, &httpResult) + log.Printf("Sent %d with status code %d", i, httpResult.StatusCode) + } + } +} diff --git a/samples/http/sender-protobuf/sample.pb.go b/samples/http/sender-protobuf/sample.pb.go new file mode 100644 index 000000000..78fed5d97 --- /dev/null +++ b/samples/http/sender-protobuf/sample.pb.go @@ -0,0 +1,141 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.17.3 +// source: sample.proto + +package main + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Sample struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *Sample) Reset() { + *x = Sample{} + if protoimpl.UnsafeEnabled { + mi := &file_sample_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Sample) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Sample) ProtoMessage() {} + +func (x *Sample) ProtoReflect() protoreflect.Message { + mi := &file_sample_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Sample.ProtoReflect.Descriptor instead. +func (*Sample) Descriptor() ([]byte, []int) { + return file_sample_proto_rawDescGZIP(), []int{0} +} + +func (x *Sample) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + +var File_sample_proto protoreflect.FileDescriptor + +var file_sample_proto_rawDesc = []byte{ + 0x0a, 0x0c, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, + 0x70, 0x62, 0x22, 0x1e, 0x0a, 0x06, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x12, 0x14, 0x0a, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2f, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_sample_proto_rawDescOnce sync.Once + file_sample_proto_rawDescData = file_sample_proto_rawDesc +) + +func file_sample_proto_rawDescGZIP() []byte { + file_sample_proto_rawDescOnce.Do(func() { + file_sample_proto_rawDescData = protoimpl.X.CompressGZIP(file_sample_proto_rawDescData) + }) + return file_sample_proto_rawDescData +} + +var file_sample_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_sample_proto_goTypes = []interface{}{ + (*Sample)(nil), // 0: pb.Sample +} +var file_sample_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_sample_proto_init() } +func file_sample_proto_init() { + if File_sample_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_sample_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Sample); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_sample_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_sample_proto_goTypes, + DependencyIndexes: file_sample_proto_depIdxs, + MessageInfos: file_sample_proto_msgTypes, + }.Build() + File_sample_proto = out.File + file_sample_proto_rawDesc = nil + file_sample_proto_goTypes = nil + file_sample_proto_depIdxs = nil +} diff --git a/samples/http/sender-protobuf/sample.proto b/samples/http/sender-protobuf/sample.proto new file mode 100644 index 000000000..0efb776df --- /dev/null +++ b/samples/http/sender-protobuf/sample.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +package pb; + +option go_package = "/;main"; + +message Sample { string value = 1; }