From 2c3418fd011165582c643cdfc0a10c6af0161004 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Fri, 4 Jul 2025 14:00:59 +0530 Subject: [PATCH 1/5] adds protobuf skeleton code Signed-off-by: Amit Singh --- pkg/api/router.go | 15 ++- pkg/domain/protocols/message.proto | 7 ++ pkg/handlers/message.pb.go | 143 +++++++++++++++++++++++++++++ pkg/handlers/ping.go | 5 +- 4 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 pkg/domain/protocols/message.proto create mode 100644 pkg/handlers/message.pb.go diff --git a/pkg/api/router.go b/pkg/api/router.go index c618ca0..724a6ac 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -6,6 +6,7 @@ import ( "source-score/pkg/handlers" "github.com/gin-gonic/gin" + "google.golang.org/protobuf/proto" ) type router struct { @@ -42,7 +43,19 @@ func (r *router) UpdateSource(ctx *gin.Context, uriDigest string) { } func (r *router) GetPing(ctx *gin.Context) { - message := r.pingHandler.GetPing(ctx) + incomingMsg := &handlers.IncomingMessage{ + Message: "sample mssage", + } + data, err := proto.Marshal(incomingMsg) + if err != nil { + panic("failed proto marshalling:: " + err.Error()) + } + err = proto.Unmarshal(data, incomingMsg) + if err != nil { + panic("failed proto marshalling:: " + err.Error()) + } + + message := r.pingHandler.GetPing(ctx, incomingMsg) ctx.JSON(http.StatusOK, gin.H{"data": message}) } diff --git a/pkg/domain/protocols/message.proto b/pkg/domain/protocols/message.proto new file mode 100644 index 0000000..c32218a --- /dev/null +++ b/pkg/domain/protocols/message.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +option go_package = "/handlers"; + +message IncomingMessage{ + string message = 1; +} diff --git a/pkg/handlers/message.pb.go b/pkg/handlers/message.pb.go new file mode 100644 index 0000000..9108da9 --- /dev/null +++ b/pkg/handlers/message.pb.go @@ -0,0 +1,143 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v3.19.6 +// source: pkg/domain/protocols/message.proto + +package handlers + +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 IncomingMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *IncomingMessage) Reset() { + *x = IncomingMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_pkg_domain_protocols_message_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IncomingMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IncomingMessage) ProtoMessage() {} + +func (x *IncomingMessage) ProtoReflect() protoreflect.Message { + mi := &file_pkg_domain_protocols_message_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 IncomingMessage.ProtoReflect.Descriptor instead. +func (*IncomingMessage) Descriptor() ([]byte, []int) { + return file_pkg_domain_protocols_message_proto_rawDescGZIP(), []int{0} +} + +func (x *IncomingMessage) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_pkg_domain_protocols_message_proto protoreflect.FileDescriptor + +var file_pkg_domain_protocols_message_proto_rawDesc = []byte{ + 0x0a, 0x22, 0x70, 0x6b, 0x67, 0x2f, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2b, 0x0a, 0x0f, 0x49, 0x6e, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x2f, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x73, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_pkg_domain_protocols_message_proto_rawDescOnce sync.Once + file_pkg_domain_protocols_message_proto_rawDescData = file_pkg_domain_protocols_message_proto_rawDesc +) + +func file_pkg_domain_protocols_message_proto_rawDescGZIP() []byte { + file_pkg_domain_protocols_message_proto_rawDescOnce.Do(func() { + file_pkg_domain_protocols_message_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_domain_protocols_message_proto_rawDescData) + }) + return file_pkg_domain_protocols_message_proto_rawDescData +} + +var file_pkg_domain_protocols_message_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_pkg_domain_protocols_message_proto_goTypes = []interface{}{ + (*IncomingMessage)(nil), // 0: IncomingMessage +} +var file_pkg_domain_protocols_message_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_pkg_domain_protocols_message_proto_init() } +func file_pkg_domain_protocols_message_proto_init() { + if File_pkg_domain_protocols_message_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_pkg_domain_protocols_message_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IncomingMessage); 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_pkg_domain_protocols_message_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_pkg_domain_protocols_message_proto_goTypes, + DependencyIndexes: file_pkg_domain_protocols_message_proto_depIdxs, + MessageInfos: file_pkg_domain_protocols_message_proto_msgTypes, + }.Build() + File_pkg_domain_protocols_message_proto = out.File + file_pkg_domain_protocols_message_proto_rawDesc = nil + file_pkg_domain_protocols_message_proto_goTypes = nil + file_pkg_domain_protocols_message_proto_depIdxs = nil +} diff --git a/pkg/handlers/ping.go b/pkg/handlers/ping.go index dd17a3e..9bd5bdb 100644 --- a/pkg/handlers/ping.go +++ b/pkg/handlers/ping.go @@ -14,6 +14,7 @@ func NewPingHandler() *PingHandler { } } -func (ph PingHandler) GetPing(ctx context.Context) string { - return ph.message +func (ph PingHandler) GetPing(ctx context.Context, inccomingMsg *IncomingMessage) string { + + return "incoming: " + inccomingMsg.Message + "\nresponse: " + ph.message } From 98d53ba7832324703bb2a03501457cb7a41d3110 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Fri, 4 Jul 2025 14:37:54 +0530 Subject: [PATCH 2/5] adds test for protobuf Signed-off-by: Amit Singh --- acceptance/ping_test.go | 13 ++++++++++--- pkg/api/router.go | 11 +++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/acceptance/ping_test.go b/acceptance/ping_test.go index be83689..3eec1da 100644 --- a/acceptance/ping_test.go +++ b/acceptance/ping_test.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "net/url" + "path" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -12,12 +13,18 @@ import ( var _ = Describe("API Tests", func() { Context("Testing /ping endpoint", func() { - endpoint, err := url.JoinPath(baseUrl, "ping") + endpoint, err := url.Parse(path.Join(baseUrl, "ping")) Expect(err).To(BeNil()) + query := endpoint.Query() + query.Add( + "incomingMessage", + "sample incoming message", + ) + endpoint.RawQuery = query.Encode() When("GET request is sent to /ping", func() { It("should get Pong message in reponse", func() { - resp, err := http.Get(endpoint) + resp, err := http.Get(endpoint.String()) Expect(err).To(BeNil()) Expect(resp.StatusCode).To(BeEquivalentTo(http.StatusOK)) @@ -28,7 +35,7 @@ var _ = Describe("API Tests", func() { var respBody responseBody err = json.Unmarshal(body, &respBody) Expect(err).To(BeNil()) - Expect(respBody.Data).To(BeEquivalentTo("Pong")) + Expect(respBody.Data).To(ContainSubstring("sample incoming message")) }) }) }) diff --git a/pkg/api/router.go b/pkg/api/router.go index 724a6ac..ea94e32 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -43,14 +43,9 @@ func (r *router) UpdateSource(ctx *gin.Context, uriDigest string) { } func (r *router) GetPing(ctx *gin.Context) { - incomingMsg := &handlers.IncomingMessage{ - Message: "sample mssage", - } - data, err := proto.Marshal(incomingMsg) - if err != nil { - panic("failed proto marshalling:: " + err.Error()) - } - err = proto.Unmarshal(data, incomingMsg) + incomingMsg := &handlers.IncomingMessage{} + + err := proto.Unmarshal([]byte(ctx.Param("incomingMessage")), incomingMsg) if err != nil { panic("failed proto marshalling:: " + err.Error()) } From e12b538beb7821f4bb89ada652a5c0f79099a892 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Fri, 4 Jul 2025 14:49:11 +0530 Subject: [PATCH 3/5] adds test for protobuf Signed-off-by: Amit Singh --- acceptance/ping_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/acceptance/ping_test.go b/acceptance/ping_test.go index 3eec1da..1174eb8 100644 --- a/acceptance/ping_test.go +++ b/acceptance/ping_test.go @@ -5,7 +5,6 @@ import ( "io" "net/http" "net/url" - "path" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -13,7 +12,7 @@ import ( var _ = Describe("API Tests", func() { Context("Testing /ping endpoint", func() { - endpoint, err := url.Parse(path.Join(baseUrl, "ping")) + endpoint, err := url.Parse(baseUrl + "/" + "ping") Expect(err).To(BeNil()) query := endpoint.Query() query.Add( @@ -34,7 +33,7 @@ var _ = Describe("API Tests", func() { var respBody responseBody err = json.Unmarshal(body, &respBody) - Expect(err).To(BeNil()) + Expect(err).To(BeNil()) Expect(respBody.Data).To(ContainSubstring("sample incoming message")) }) }) From f00dcff1be4195c99d4564b98a0bb0bcb43fa0a2 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Fri, 4 Jul 2025 15:18:11 +0530 Subject: [PATCH 4/5] adds test for protobuf Signed-off-by: Amit Singh --- acceptance/ping_test.go | 40 ++++++++++++++++++++++++++++++++-------- pkg/api/router.go | 16 +++++++++++++--- pkg/handlers/ping.go | 7 ++++++- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/acceptance/ping_test.go b/acceptance/ping_test.go index 1174eb8..980face 100644 --- a/acceptance/ping_test.go +++ b/acceptance/ping_test.go @@ -1,29 +1,53 @@ package acceptance_test import ( + "bytes" "encoding/json" "io" "net/http" "net/url" + "source-score/pkg/handlers" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "google.golang.org/protobuf/proto" ) var _ = Describe("API Tests", func() { Context("Testing /ping endpoint", func() { - endpoint, err := url.Parse(baseUrl + "/" + "ping") + endpoint, err := url.JoinPath(baseUrl, "ping") Expect(err).To(BeNil()) - query := endpoint.Query() - query.Add( - "incomingMessage", - "sample incoming message", - ) - endpoint.RawQuery = query.Encode() When("GET request is sent to /ping", func() { It("should get Pong message in reponse", func() { - resp, err := http.Get(endpoint.String()) + resp, err := http.Get(endpoint) + Expect(err).To(BeNil()) + Expect(resp.StatusCode).To(BeEquivalentTo(http.StatusOK)) + + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + Expect(err).To(BeNil()) + + var respBody responseBody + err = json.Unmarshal(body, &respBody) + Expect(err).To(BeNil()) + Expect(respBody.Data).To(BeEquivalentTo("Pong")) + }) + }) + + When("POST request is sent to /ping", func() { + It("should sent message included in the reponse", func() { + msg := handlers.IncomingMessage{ + Message: "sample incoming message", + } + data, err := proto.Marshal(&msg) + Expect(err).To(BeNil()) + + resp, err := http.Post( + endpoint, + "application/x-protobuf", + bytes.NewReader(data), + ) Expect(err).To(BeNil()) Expect(resp.StatusCode).To(BeEquivalentTo(http.StatusOK)) diff --git a/pkg/api/router.go b/pkg/api/router.go index ea94e32..697a9c3 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -1,6 +1,7 @@ package api import ( + "io" "log/slog" "net/http" "source-score/pkg/handlers" @@ -43,14 +44,23 @@ func (r *router) UpdateSource(ctx *gin.Context, uriDigest string) { } func (r *router) GetPing(ctx *gin.Context) { - incomingMsg := &handlers.IncomingMessage{} + message := r.pingHandler.GetPing(ctx) - err := proto.Unmarshal([]byte(ctx.Param("incomingMessage")), incomingMsg) + ctx.JSON(http.StatusOK, gin.H{"data": message}) +} + +func (r *router) PostPing(ctx *gin.Context) { + body, _ := io.ReadAll(ctx.Request.Body) + incomingMsg := &handlers.IncomingMessage{ + Message: "sample incoming message", + } + + err := proto.Unmarshal(body, incomingMsg) if err != nil { panic("failed proto marshalling:: " + err.Error()) } - message := r.pingHandler.GetPing(ctx, incomingMsg) + message := r.pingHandler.PostPing(ctx, incomingMsg) ctx.JSON(http.StatusOK, gin.H{"data": message}) } diff --git a/pkg/handlers/ping.go b/pkg/handlers/ping.go index 9bd5bdb..3e8474f 100644 --- a/pkg/handlers/ping.go +++ b/pkg/handlers/ping.go @@ -14,7 +14,12 @@ func NewPingHandler() *PingHandler { } } -func (ph PingHandler) GetPing(ctx context.Context, inccomingMsg *IncomingMessage) string { +func (ph PingHandler) GetPing(ctx context.Context) string { + + return ph.message +} + +func (ph PingHandler) PostPing(ctx context.Context, inccomingMsg *IncomingMessage) string { return "incoming: " + inccomingMsg.Message + "\nresponse: " + ph.message } From e3649ded675a46c899c1e18d4256d0532a858498 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Fri, 4 Jul 2025 15:39:30 +0530 Subject: [PATCH 5/5] updates api config Signed-off-by: Amit Singh --- api/source-score.yaml | 4 ++++ go.mod | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/api/source-score.yaml b/api/source-score.yaml index ef7993b..27c9bec 100644 --- a/api/source-score.yaml +++ b/api/source-score.yaml @@ -6,6 +6,10 @@ info: paths: /ping: + post: + responses: + 201: + description: ping posted get: responses: 200: diff --git a/go.mod b/go.mod index f493e39..5d5be6a 100644 --- a/go.mod +++ b/go.mod @@ -236,7 +236,7 @@ require ( golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.20.0 // indirect golang.org/x/tools v0.27.0 // indirect - google.golang.org/protobuf v1.34.2 // indirect + google.golang.org/protobuf v1.34.2 gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect