From 2cfb0a14b451686432a12da080e5f3784d8b9582 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Fri, 27 Oct 2023 22:13:11 +0800 Subject: [PATCH] update recorder proto --- recorder/example/{ => grpc}/main.go | 3 +- recorder/example/http/main.go | 53 +++++++++++++++++++++++++++++ recorder/proto/recorder.pb.go | 38 +++++++++++++-------- recorder/proto/recorder.proto | 1 + recorder/proto/recorder_grpc.pb.go | 4 +++ 5 files changed, 84 insertions(+), 15 deletions(-) rename recorder/example/{ => grpc}/main.go (90%) create mode 100644 recorder/example/http/main.go diff --git a/recorder/example/main.go b/recorder/example/grpc/main.go similarity index 90% rename from recorder/example/main.go rename to recorder/example/grpc/main.go index 5134acb..37bda52 100644 --- a/recorder/example/main.go +++ b/recorder/example/grpc/main.go @@ -23,7 +23,8 @@ func (s *server) Record(ctx context.Context, in *proto.RecordRequest) (*proto.Re reply := &proto.RecordReply{ Ok: true, } - log.Printf("record: %s", string(in.GetData())) + log.Printf("data: %s, metadata: %s", string(in.GetData()), string(in.Metadata)) + return reply, nil } diff --git a/recorder/example/http/main.go b/recorder/example/http/main.go new file mode 100644 index 0000000..c113d66 --- /dev/null +++ b/recorder/example/http/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net" + "net/http" +) + +var ( + port = flag.Int("port", 8000, "The server port") +) + +type recorderRequest struct { + Data []byte `json:"data"` + Metadata []byte `json:"metadata"` +} + +type recorderResponse struct { + OK bool `json:"ok"` +} + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + log.Printf("server listening at %v", lis.Addr()) + + http.HandleFunc("/recorder", func(w http.ResponseWriter, r *http.Request) { + rb := recorderRequest{} + if err := json.NewDecoder(r.Body).Decode(&rb); err != nil { + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + resp := recorderResponse{ + OK: true, + } + + log.Printf("recorder: data=%s, metadata: %s", string(rb.Data), string(rb.Metadata)) + + json.NewEncoder(w).Encode(resp) + }) + + if err := http.Serve(lis, nil); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/recorder/proto/recorder.pb.go b/recorder/proto/recorder.pb.go index e3a657d..f44b154 100644 --- a/recorder/proto/recorder.pb.go +++ b/recorder/proto/recorder.pb.go @@ -4,8 +4,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.25.0-devel -// protoc v3.14.0 +// protoc-gen-go v1.28.1 +// protoc v3.15.8 // source: recorder.proto package proto @@ -29,7 +29,8 @@ type RecordRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` + Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` + Metadata []byte `protobuf:"bytes,2,opt,name=metadata,proto3" json:"metadata,omitempty"` } func (x *RecordRequest) Reset() { @@ -71,6 +72,13 @@ func (x *RecordRequest) GetData() []byte { return nil } +func (x *RecordRequest) GetMetadata() []byte { + if x != nil { + return x.Metadata + } + return nil +} + type RecordReply struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -122,18 +130,20 @@ var File_recorder_proto protoreflect.FileDescriptor var file_recorder_proto_rawDesc = []byte{ 0x0a, 0x0e, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x23, 0x0a, 0x0d, 0x52, 0x65, 0x63, 0x6f, 0x72, + 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x3f, 0x0a, 0x0d, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x1d, 0x0a, 0x0b, - 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x6f, - 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x32, 0x3e, 0x0a, 0x08, 0x52, - 0x65, 0x63, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x32, 0x0a, 0x06, 0x52, 0x65, 0x63, 0x6f, 0x72, - 0x64, 0x12, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, - 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, - 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x2d, 0x67, 0x6f, 0x73, - 0x74, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2f, 0x69, 0x6e, 0x67, 0x72, 0x65, 0x73, 0x73, - 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x1a, 0x0a, 0x08, + 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, + 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x22, 0x1d, 0x0a, 0x0b, 0x52, 0x65, 0x63, 0x6f, + 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x32, 0x3e, 0x0a, 0x08, 0x52, 0x65, 0x63, 0x6f, 0x72, + 0x64, 0x65, 0x72, 0x12, 0x32, 0x0a, 0x06, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x12, 0x14, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x6f, + 0x72, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, + 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x2d, 0x67, 0x6f, 0x73, 0x74, 0x2f, 0x70, 0x6c, + 0x75, 0x67, 0x69, 0x6e, 0x2f, 0x69, 0x6e, 0x67, 0x72, 0x65, 0x73, 0x73, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/recorder/proto/recorder.proto b/recorder/proto/recorder.proto index 02606da..5862050 100644 --- a/recorder/proto/recorder.proto +++ b/recorder/proto/recorder.proto @@ -8,6 +8,7 @@ option go_package = "github.com/go-gost/plugin/ingress/proto"; message RecordRequest { bytes data = 1; + bytes metadata = 2; } message RecordReply { diff --git a/recorder/proto/recorder_grpc.pb.go b/recorder/proto/recorder_grpc.pb.go index d261b94..9dcb303 100644 --- a/recorder/proto/recorder_grpc.pb.go +++ b/recorder/proto/recorder_grpc.pb.go @@ -1,4 +1,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.15.8 +// source: recorder.proto package proto