diff --git a/pkg/authReg/handler.go b/pkg/authReg/handler.go new file mode 100644 index 0000000..735525b --- /dev/null +++ b/pkg/authReg/handler.go @@ -0,0 +1,40 @@ +package authReg + +import ( + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + er "merch-api/pkg/externalRegistration/v1" + "time" +) + +const pkgLogHeader string = "External service registration |" + +type Handler struct { + client er.ExternalRegistrationClient + *service +} + +type Deps struct { + Addr string + Timeout time.Duration +} + +func New(deps Deps) *Handler { + var opts []grpc.DialOption + insec := grpc.WithTransportCredentials(insecure.NewCredentials()) + opts = append(opts, insec) + + conn, err := grpc.NewClient(deps.Addr, opts...) + if err != nil { + log.WithError(err).Fatalf("%v grpc connection failed", pkgLogHeader) + } + + client := er.NewExternalRegistrationClient(conn) + log.WithField("address", deps.Addr).Debugf("%v client", pkgLogHeader) + + return &Handler{ + client: client, + service: newService(client, deps.Timeout), + } +} diff --git a/pkg/authReg/interface.go b/pkg/authReg/interface.go new file mode 100644 index 0000000..f75f126 --- /dev/null +++ b/pkg/authReg/interface.go @@ -0,0 +1,9 @@ +package authReg + +import ( + "context" +) + +type ServiceRegistrar interface { + AuthenticateOrRegister(ctx context.Context, req *RegRequest) (*RegResponse, error) +} diff --git a/pkg/authReg/service.go b/pkg/authReg/service.go new file mode 100644 index 0000000..5b1a6b7 --- /dev/null +++ b/pkg/authReg/service.go @@ -0,0 +1,43 @@ +package authReg + +import ( + "context" + er "merch-api/pkg/externalRegistration/v1" + "time" +) + +type service struct { + client er.ExternalRegistrationClient + timeout time.Duration +} + +func newService(c er.ExternalRegistrationClient, timeout time.Duration) *service { + return &service{ + client: c, + timeout: timeout, + } +} + +func (s *service) AuthenticateOrRegister(ctx context.Context, req *RegRequest) (*RegResponse, error) { + authCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + + request := er.RegistrationRequest{ + Name: req.Name, + Description: req.Description, + ServiceCode: int32(req.BaseCode), + ServiceEndCode: int32(req.EndCode), + Secret: req.SecretHash, + Status: req.Status, + } + + response, err := s.client.AuthenticateOrRegister(authCtx, &request) + if err != nil { + return nil, err + } + + return &RegResponse{ + ServiceId: response.ServiceId, + AlreadyRegistered: response.AlreadyRegistered, + }, nil +} diff --git a/pkg/authReg/struct.go b/pkg/authReg/struct.go new file mode 100644 index 0000000..f10b89a --- /dev/null +++ b/pkg/authReg/struct.go @@ -0,0 +1,15 @@ +package authReg + +type RegRequest struct { + Name string + Description string + BaseCode int + EndCode int + SecretHash string + Status string +} + +type RegResponse struct { + ServiceId int32 + AlreadyRegistered bool +} diff --git a/pkg/externalRegistration/v1/serviceRegistration.pb.go b/pkg/externalRegistration/v1/serviceRegistration.pb.go new file mode 100644 index 0000000..9f1da73 --- /dev/null +++ b/pkg/externalRegistration/v1/serviceRegistration.pb.go @@ -0,0 +1,228 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.8 +// protoc v6.33.1 +// source: proto/serviceRegistration.proto + +package externalRegV1 + +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 RegistrationRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` + ServiceCode int32 `protobuf:"varint,3,opt,name=service_code,json=serviceCode,proto3" json:"service_code,omitempty"` + ServiceEndCode int32 `protobuf:"varint,4,opt,name=service_end_code,json=serviceEndCode,proto3" json:"service_end_code,omitempty"` + Secret string `protobuf:"bytes,5,opt,name=secret,proto3" json:"secret,omitempty"` + Status string `protobuf:"bytes,6,opt,name=status,proto3" json:"status,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RegistrationRequest) Reset() { + *x = RegistrationRequest{} + mi := &file_proto_serviceRegistration_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RegistrationRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RegistrationRequest) ProtoMessage() {} + +func (x *RegistrationRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_serviceRegistration_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 RegistrationRequest.ProtoReflect.Descriptor instead. +func (*RegistrationRequest) Descriptor() ([]byte, []int) { + return file_proto_serviceRegistration_proto_rawDescGZIP(), []int{0} +} + +func (x *RegistrationRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *RegistrationRequest) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + +func (x *RegistrationRequest) GetServiceCode() int32 { + if x != nil { + return x.ServiceCode + } + return 0 +} + +func (x *RegistrationRequest) GetServiceEndCode() int32 { + if x != nil { + return x.ServiceEndCode + } + return 0 +} + +func (x *RegistrationRequest) GetSecret() string { + if x != nil { + return x.Secret + } + return "" +} + +func (x *RegistrationRequest) GetStatus() string { + if x != nil { + return x.Status + } + return "" +} + +type RegistrationResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + ServiceId int32 `protobuf:"varint,1,opt,name=service_id,json=serviceId,proto3" json:"service_id,omitempty"` + AlreadyRegistered bool `protobuf:"varint,2,opt,name=already_registered,json=alreadyRegistered,proto3" json:"already_registered,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RegistrationResponse) Reset() { + *x = RegistrationResponse{} + mi := &file_proto_serviceRegistration_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RegistrationResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RegistrationResponse) ProtoMessage() {} + +func (x *RegistrationResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_serviceRegistration_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 RegistrationResponse.ProtoReflect.Descriptor instead. +func (*RegistrationResponse) Descriptor() ([]byte, []int) { + return file_proto_serviceRegistration_proto_rawDescGZIP(), []int{1} +} + +func (x *RegistrationResponse) GetServiceId() int32 { + if x != nil { + return x.ServiceId + } + return 0 +} + +func (x *RegistrationResponse) GetAlreadyRegistered() bool { + if x != nil { + return x.AlreadyRegistered + } + return false +} + +var File_proto_serviceRegistration_proto protoreflect.FileDescriptor + +const file_proto_serviceRegistration_proto_rawDesc = "" + + "\n" + + "\x1fproto/serviceRegistration.proto\x12\x11auth.registration\"\xc8\x01\n" + + "\x13RegistrationRequest\x12\x12\n" + + "\x04name\x18\x01 \x01(\tR\x04name\x12 \n" + + "\vdescription\x18\x02 \x01(\tR\vdescription\x12!\n" + + "\fservice_code\x18\x03 \x01(\x05R\vserviceCode\x12(\n" + + "\x10service_end_code\x18\x04 \x01(\x05R\x0eserviceEndCode\x12\x16\n" + + "\x06secret\x18\x05 \x01(\tR\x06secret\x12\x16\n" + + "\x06status\x18\x06 \x01(\tR\x06status\"d\n" + + "\x14RegistrationResponse\x12\x1d\n" + + "\n" + + "service_id\x18\x01 \x01(\x05R\tserviceId\x12-\n" + + "\x12already_registered\x18\x02 \x01(\bR\x11alreadyRegistered2\x81\x01\n" + + "\x14ExternalRegistration\x12i\n" + + "\x16AuthenticateOrRegister\x12&.auth.registration.RegistrationRequest\x1a'.auth.registration.RegistrationResponseB,Z*/pkg/externalRegistration/v1;externalRegV1b\x06proto3" + +var ( + file_proto_serviceRegistration_proto_rawDescOnce sync.Once + file_proto_serviceRegistration_proto_rawDescData []byte +) + +func file_proto_serviceRegistration_proto_rawDescGZIP() []byte { + file_proto_serviceRegistration_proto_rawDescOnce.Do(func() { + file_proto_serviceRegistration_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_proto_serviceRegistration_proto_rawDesc), len(file_proto_serviceRegistration_proto_rawDesc))) + }) + return file_proto_serviceRegistration_proto_rawDescData +} + +var file_proto_serviceRegistration_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_serviceRegistration_proto_goTypes = []any{ + (*RegistrationRequest)(nil), // 0: auth.registration.RegistrationRequest + (*RegistrationResponse)(nil), // 1: auth.registration.RegistrationResponse +} +var file_proto_serviceRegistration_proto_depIdxs = []int32{ + 0, // 0: auth.registration.ExternalRegistration.AuthenticateOrRegister:input_type -> auth.registration.RegistrationRequest + 1, // 1: auth.registration.ExternalRegistration.AuthenticateOrRegister:output_type -> auth.registration.RegistrationResponse + 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_proto_serviceRegistration_proto_init() } +func file_proto_serviceRegistration_proto_init() { + if File_proto_serviceRegistration_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_proto_serviceRegistration_proto_rawDesc), len(file_proto_serviceRegistration_proto_rawDesc)), + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_serviceRegistration_proto_goTypes, + DependencyIndexes: file_proto_serviceRegistration_proto_depIdxs, + MessageInfos: file_proto_serviceRegistration_proto_msgTypes, + }.Build() + File_proto_serviceRegistration_proto = out.File + file_proto_serviceRegistration_proto_goTypes = nil + file_proto_serviceRegistration_proto_depIdxs = nil +} diff --git a/pkg/externalRegistration/v1/serviceRegistration_grpc.pb.go b/pkg/externalRegistration/v1/serviceRegistration_grpc.pb.go new file mode 100644 index 0000000..d516302 --- /dev/null +++ b/pkg/externalRegistration/v1/serviceRegistration_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 v6.33.1 +// source: proto/serviceRegistration.proto + +package externalRegV1 + +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 ( + ExternalRegistration_AuthenticateOrRegister_FullMethodName = "/auth.registration.ExternalRegistration/AuthenticateOrRegister" +) + +// ExternalRegistrationClient is the client API for ExternalRegistration 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 ExternalRegistrationClient interface { + AuthenticateOrRegister(ctx context.Context, in *RegistrationRequest, opts ...grpc.CallOption) (*RegistrationResponse, error) +} + +type externalRegistrationClient struct { + cc grpc.ClientConnInterface +} + +func NewExternalRegistrationClient(cc grpc.ClientConnInterface) ExternalRegistrationClient { + return &externalRegistrationClient{cc} +} + +func (c *externalRegistrationClient) AuthenticateOrRegister(ctx context.Context, in *RegistrationRequest, opts ...grpc.CallOption) (*RegistrationResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(RegistrationResponse) + err := c.cc.Invoke(ctx, ExternalRegistration_AuthenticateOrRegister_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ExternalRegistrationServer is the server API for ExternalRegistration service. +// All implementations must embed UnimplementedExternalRegistrationServer +// for forward compatibility. +type ExternalRegistrationServer interface { + AuthenticateOrRegister(context.Context, *RegistrationRequest) (*RegistrationResponse, error) + mustEmbedUnimplementedExternalRegistrationServer() +} + +// UnimplementedExternalRegistrationServer 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 UnimplementedExternalRegistrationServer struct{} + +func (UnimplementedExternalRegistrationServer) AuthenticateOrRegister(context.Context, *RegistrationRequest) (*RegistrationResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method AuthenticateOrRegister not implemented") +} +func (UnimplementedExternalRegistrationServer) mustEmbedUnimplementedExternalRegistrationServer() {} +func (UnimplementedExternalRegistrationServer) testEmbeddedByValue() {} + +// UnsafeExternalRegistrationServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ExternalRegistrationServer will +// result in compilation errors. +type UnsafeExternalRegistrationServer interface { + mustEmbedUnimplementedExternalRegistrationServer() +} + +func RegisterExternalRegistrationServer(s grpc.ServiceRegistrar, srv ExternalRegistrationServer) { + // If the following call pancis, it indicates UnimplementedExternalRegistrationServer 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(&ExternalRegistration_ServiceDesc, srv) +} + +func _ExternalRegistration_AuthenticateOrRegister_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RegistrationRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ExternalRegistrationServer).AuthenticateOrRegister(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: ExternalRegistration_AuthenticateOrRegister_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ExternalRegistrationServer).AuthenticateOrRegister(ctx, req.(*RegistrationRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ExternalRegistration_ServiceDesc is the grpc.ServiceDesc for ExternalRegistration service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ExternalRegistration_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "auth.registration.ExternalRegistration", + HandlerType: (*ExternalRegistrationServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "AuthenticateOrRegister", + Handler: _ExternalRegistration_AuthenticateOrRegister_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "proto/serviceRegistration.proto", +} diff --git a/proto/serviceRegistration.proto b/proto/serviceRegistration.proto new file mode 100644 index 0000000..1228ea8 --- /dev/null +++ b/proto/serviceRegistration.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; + +package auth.registration; +option go_package = "/pkg/externalRegistration/v1;externalRegV1"; + +message RegistrationRequest { + string name = 1; + string description = 2; + int32 service_code = 3; + int32 service_end_code = 4; + string secret = 5; + string status = 6; +} + +message RegistrationResponse { + int32 service_id = 1; + bool already_registered = 2; +} + +service ExternalRegistration { + rpc AuthenticateOrRegister (RegistrationRequest) returns (RegistrationResponse); +} \ No newline at end of file