From ff70a04542910931973ef3797683e638e3ae95d8 Mon Sep 17 00:00:00 2001 From: Aviram Hassan Date: Wed, 5 Feb 2025 15:35:30 +0200 Subject: [PATCH 1/2] add grpc-info --- .github/workflows/build-ip-info-grpc.yaml | 36 +++++ ip-info-grpc/Dockerfile | 17 ++ ip-info-grpc/main.go | 73 +++++++++ proto/ipinfo.proto | 17 ++ protogen/ipinfo.pb.go | 189 ++++++++++++++++++++++ protogen/ipinfo_grpc.pb.go | 121 ++++++++++++++ 6 files changed, 453 insertions(+) create mode 100644 .github/workflows/build-ip-info-grpc.yaml create mode 100644 ip-info-grpc/Dockerfile create mode 100644 ip-info-grpc/main.go create mode 100644 proto/ipinfo.proto create mode 100644 protogen/ipinfo.pb.go create mode 100644 protogen/ipinfo_grpc.pb.go diff --git a/.github/workflows/build-ip-info-grpc.yaml b/.github/workflows/build-ip-info-grpc.yaml new file mode 100644 index 0000000..f5e534c --- /dev/null +++ b/.github/workflows/build-ip-info-grpc.yaml @@ -0,0 +1,36 @@ +name: Release ip info grpc +on: + push: + branches: [main] + workflow_dispatch: + +jobs: + image: + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up QEMU + id: qemu + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v3 + with: + file: ip-info-grpc/Dockerfile + platforms: linux/amd64,linux/arm64,linux/arm/v7 + context: ./ + push: true + tags: | + ghcr.io/metalbear-co/playground-ip-info-grpc:latest + ghcr.io/metalbear-co/playground-ip-info-grpc:${{ github.sha }} \ No newline at end of file diff --git a/ip-info-grpc/Dockerfile b/ip-info-grpc/Dockerfile new file mode 100644 index 0000000..1ec1f78 --- /dev/null +++ b/ip-info-grpc/Dockerfile @@ -0,0 +1,17 @@ +FROM --platform=$BUILDPLATFORM golang:1.23-alpine as build-env + +WORKDIR /app +COPY go.mod ./ +COPY go.sum ./ +RUN go mod download +COPY ip-info-grpc ./ip-info-grpc +COPY protogen ./protogen +COPY proto ./proto + +RUN GOARCH=$TARGETARCH go build -o /main ./ip-info-grpc/main.go + +FROM gcr.io/distroless/static-debian11 + +COPY --from=build-env /main /main + +CMD [ "/main" ] \ No newline at end of file diff --git a/ip-info-grpc/main.go b/ip-info-grpc/main.go new file mode 100644 index 0000000..57fbf8b --- /dev/null +++ b/ip-info-grpc/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "context" + "fmt" + "log" + "net" + + pb "github.com/metalbear-co/playground/protogen" + "github.com/spf13/viper" + "google.golang.org/grpc" +) + +var ctx = context.Background() + +// Config +// Struct that holds local service port, remote redis host and port +type Config struct { + Port int16 + KafkaAddress string + KafkaTopic string + KafkaConsumerGroup string +} + +type IpInfo struct { + Ip string `json:"ip"` + Info string `json:"name"` +} + +// albums slice to seed record album data. +var ipInfos = []IpInfo{ + {Ip: "84.229.14.82", Info: "Aviram, Loves coffee!"}, +} + +func loadConfig() Config { + viper.BindEnv("port") + + config := Config{} + config.Port = int16(viper.GetInt("port")) + return config +} + +type server struct { + pb.UnimplementedIpInfoServiceServer +} + +func (s *server) GetIpInfo(ctx context.Context, req *pb.IpRequest) (*pb.IpResponse, error) { + ip := req.GetIp() + + for _, info := range ipInfos { + if info.Ip == ip { + return &pb.IpResponse{Ip: info.Ip, Info: info.Info}, nil + } + } + return &pb.IpResponse{Ip: ip, Info: "Unknown"}, nil +} + +func main() { + config := loadConfig() + + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + + s := grpc.NewServer() + pb.RegisterIpInfoServiceServer(s, &server{}) + + fmt.Printf("gRPC server listening on port %d\n", config.Port) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/proto/ipinfo.proto b/proto/ipinfo.proto new file mode 100644 index 0000000..73a1b86 --- /dev/null +++ b/proto/ipinfo.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package ipinfo; +option go_package = "github.com/metalbear-co/playground/protogen"; + +service IpInfoService { + rpc GetIpInfo(IpRequest) returns (IpResponse); +} + +message IpRequest { + string ip = 1; +} + +message IpResponse { + string ip = 1; + string info = 2; +} \ No newline at end of file diff --git a/protogen/ipinfo.pb.go b/protogen/ipinfo.pb.go new file mode 100644 index 0000000..c0deada --- /dev/null +++ b/protogen/ipinfo.pb.go @@ -0,0 +1,189 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.4 +// protoc v5.28.1 +// source: ipinfo.proto + +package protogen + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +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 IpRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *IpRequest) Reset() { + *x = IpRequest{} + mi := &file_ipinfo_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *IpRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IpRequest) ProtoMessage() {} + +func (x *IpRequest) ProtoReflect() protoreflect.Message { + mi := &file_ipinfo_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IpRequest.ProtoReflect.Descriptor instead. +func (*IpRequest) Descriptor() ([]byte, []int) { + return file_ipinfo_proto_rawDescGZIP(), []int{0} +} + +func (x *IpRequest) GetIp() string { + if x != nil { + return x.Ip + } + return "" +} + +type IpResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"` + Info string `protobuf:"bytes,2,opt,name=info,proto3" json:"info,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *IpResponse) Reset() { + *x = IpResponse{} + mi := &file_ipinfo_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *IpResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IpResponse) ProtoMessage() {} + +func (x *IpResponse) ProtoReflect() protoreflect.Message { + mi := &file_ipinfo_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IpResponse.ProtoReflect.Descriptor instead. +func (*IpResponse) Descriptor() ([]byte, []int) { + return file_ipinfo_proto_rawDescGZIP(), []int{1} +} + +func (x *IpResponse) GetIp() string { + if x != nil { + return x.Ip + } + return "" +} + +func (x *IpResponse) GetInfo() string { + if x != nil { + return x.Info + } + return "" +} + +var File_ipinfo_proto protoreflect.FileDescriptor + +var file_ipinfo_proto_rawDesc = string([]byte{ + 0x0a, 0x0c, 0x69, 0x70, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, + 0x69, 0x70, 0x69, 0x6e, 0x66, 0x6f, 0x22, 0x1b, 0x0a, 0x09, 0x49, 0x70, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x02, 0x69, 0x70, 0x22, 0x30, 0x0a, 0x0a, 0x49, 0x70, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, + 0x70, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x32, 0x43, 0x0a, 0x0d, 0x49, 0x70, 0x49, 0x6e, 0x66, 0x6f, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x32, 0x0a, 0x09, 0x47, 0x65, 0x74, 0x49, 0x70, 0x49, + 0x6e, 0x66, 0x6f, 0x12, 0x11, 0x2e, 0x69, 0x70, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x49, 0x70, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x69, 0x70, 0x69, 0x6e, 0x66, 0x6f, 0x2e, + 0x49, 0x70, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x2d, 0x5a, 0x2b, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x65, 0x74, 0x61, 0x6c, 0x62, 0x65, + 0x61, 0x72, 0x2d, 0x63, 0x6f, 0x2f, 0x70, 0x6c, 0x61, 0x79, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, + 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, 0x65, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +}) + +var ( + file_ipinfo_proto_rawDescOnce sync.Once + file_ipinfo_proto_rawDescData []byte +) + +func file_ipinfo_proto_rawDescGZIP() []byte { + file_ipinfo_proto_rawDescOnce.Do(func() { + file_ipinfo_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_ipinfo_proto_rawDesc), len(file_ipinfo_proto_rawDesc))) + }) + return file_ipinfo_proto_rawDescData +} + +var file_ipinfo_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_ipinfo_proto_goTypes = []any{ + (*IpRequest)(nil), // 0: ipinfo.IpRequest + (*IpResponse)(nil), // 1: ipinfo.IpResponse +} +var file_ipinfo_proto_depIdxs = []int32{ + 0, // 0: ipinfo.IpInfoService.GetIpInfo:input_type -> ipinfo.IpRequest + 1, // 1: ipinfo.IpInfoService.GetIpInfo:output_type -> ipinfo.IpResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] 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_ipinfo_proto_init() } +func file_ipinfo_proto_init() { + if File_ipinfo_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_ipinfo_proto_rawDesc), len(file_ipinfo_proto_rawDesc)), + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_ipinfo_proto_goTypes, + DependencyIndexes: file_ipinfo_proto_depIdxs, + MessageInfos: file_ipinfo_proto_msgTypes, + }.Build() + File_ipinfo_proto = out.File + file_ipinfo_proto_goTypes = nil + file_ipinfo_proto_depIdxs = nil +} diff --git a/protogen/ipinfo_grpc.pb.go b/protogen/ipinfo_grpc.pb.go new file mode 100644 index 0000000..172d0b2 --- /dev/null +++ b/protogen/ipinfo_grpc.pb.go @@ -0,0 +1,121 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v5.28.1 +// source: ipinfo.proto + +package protogen + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + IpInfoService_GetIpInfo_FullMethodName = "/ipinfo.IpInfoService/GetIpInfo" +) + +// IpInfoServiceClient is the client API for IpInfoService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type IpInfoServiceClient interface { + GetIpInfo(ctx context.Context, in *IpRequest, opts ...grpc.CallOption) (*IpResponse, error) +} + +type ipInfoServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewIpInfoServiceClient(cc grpc.ClientConnInterface) IpInfoServiceClient { + return &ipInfoServiceClient{cc} +} + +func (c *ipInfoServiceClient) GetIpInfo(ctx context.Context, in *IpRequest, opts ...grpc.CallOption) (*IpResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(IpResponse) + err := c.cc.Invoke(ctx, IpInfoService_GetIpInfo_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// IpInfoServiceServer is the server API for IpInfoService service. +// All implementations must embed UnimplementedIpInfoServiceServer +// for forward compatibility. +type IpInfoServiceServer interface { + GetIpInfo(context.Context, *IpRequest) (*IpResponse, error) + mustEmbedUnimplementedIpInfoServiceServer() +} + +// UnimplementedIpInfoServiceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedIpInfoServiceServer struct{} + +func (UnimplementedIpInfoServiceServer) GetIpInfo(context.Context, *IpRequest) (*IpResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetIpInfo not implemented") +} +func (UnimplementedIpInfoServiceServer) mustEmbedUnimplementedIpInfoServiceServer() {} +func (UnimplementedIpInfoServiceServer) testEmbeddedByValue() {} + +// UnsafeIpInfoServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to IpInfoServiceServer will +// result in compilation errors. +type UnsafeIpInfoServiceServer interface { + mustEmbedUnimplementedIpInfoServiceServer() +} + +func RegisterIpInfoServiceServer(s grpc.ServiceRegistrar, srv IpInfoServiceServer) { + // If the following call pancis, it indicates UnimplementedIpInfoServiceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&IpInfoService_ServiceDesc, srv) +} + +func _IpInfoService_GetIpInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(IpRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(IpInfoServiceServer).GetIpInfo(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: IpInfoService_GetIpInfo_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(IpInfoServiceServer).GetIpInfo(ctx, req.(*IpRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// IpInfoService_ServiceDesc is the grpc.ServiceDesc for IpInfoService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var IpInfoService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "ipinfo.IpInfoService", + HandlerType: (*IpInfoServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetIpInfo", + Handler: _IpInfoService_GetIpInfo_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "ipinfo.proto", +} From dce385324a3c4d0965ad79345221eff895cfa840 Mon Sep 17 00:00:00 2001 From: Aviram Hassan Date: Wed, 5 Feb 2025 18:30:50 +0200 Subject: [PATCH 2/2] .. --- .github/workflows/build-ip-info-grpc.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/build-ip-info-grpc.yaml b/.github/workflows/build-ip-info-grpc.yaml index f5e534c..d2ec0df 100644 --- a/.github/workflows/build-ip-info-grpc.yaml +++ b/.github/workflows/build-ip-info-grpc.yaml @@ -13,9 +13,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - - name: Set up QEMU - id: qemu - uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Login to GitHub Container Registry