From 6bffc661967c7ddc881bbf8fd853eb72b0f48245 Mon Sep 17 00:00:00 2001 From: Nikolai Varlamov Date: Thu, 30 Sep 2021 22:15:47 +0300 Subject: [PATCH] grpc initial commit --- Makefile | 20 ++ config.yaml | 8 +- gen/pb/api.pb.go | 89 ++++++++ gen/pb/api.pb.gw.go | 169 +++++++++++++++ gen/pb/api.pb.validate.go | 36 ++++ gen/pb/api_grpc.pb.go | 101 +++++++++ gen/pb/common.pb.go | 261 +++++++++++++++++++++++ gen/pb/common.pb.validate.go | 148 +++++++++++++ gen/pb/messages.pb.go | 250 ++++++++++++++++++++++ gen/pb/messages.pb.validate.go | 365 +++++++++++++++++++++++++++++++++ gen/swagger/api.swagger.json | 115 +++++++++++ go.mod | 20 +- go.sum | 31 +++ main.go | 8 +- modules/modem/handlers.go | 1 + modules/modem/modem.go | 5 - modules/server/handlers.go | 9 +- modules/server/server.go | 77 ++++++- modules/server/structs.go | 15 ++ proto/api.proto | 40 ++++ proto/common.proto | 14 ++ proto/messages.proto | 16 ++ 22 files changed, 1773 insertions(+), 25 deletions(-) create mode 100644 Makefile create mode 100644 gen/pb/api.pb.go create mode 100644 gen/pb/api.pb.gw.go create mode 100644 gen/pb/api.pb.validate.go create mode 100644 gen/pb/api_grpc.pb.go create mode 100644 gen/pb/common.pb.go create mode 100644 gen/pb/common.pb.validate.go create mode 100644 gen/pb/messages.pb.go create mode 100644 gen/pb/messages.pb.validate.go create mode 100644 gen/swagger/api.swagger.json create mode 100644 modules/modem/handlers.go create mode 100644 modules/server/structs.go create mode 100644 proto/api.proto create mode 100644 proto/common.proto create mode 100644 proto/messages.proto diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c548d08 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +generate: + rm -rf gen/{pb,clean_proto,swagger}/{*.go,*.proto,*.json} + mkdir -p gen/{pb,swagger} + + protoc \ + -I=proto/ \ + -I=${GOPATH}/src/ \ + -I=${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/ \ + -I=${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ + --go_out=./ \ + --validate_out=lang=go:./ \ + --grpc-gateway_out=logtostderr=true:. \ + --swagger_out=allow_merge=true,merge_file_name=api:./gen/swagger/ proto/*.proto + + protoc \ + -I=proto/ \ + -I=${GOPATH}/src/ \ + -I=${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/ \ + -I=${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ + --go-grpc_out=./ proto/api.proto diff --git a/config.yaml b/config.yaml index d24e4f3..356875f 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,10 @@ server: - interface: 0.0.0.0 - port: 3200 + grpc: + interface: 0.0.0.0 + port: 3200 + http: + interface: 0.0.0.0 + port: 3201 modem: comport: /dev/ttyUSB0 diff --git a/gen/pb/api.pb.go b/gen/pb/api.pb.go new file mode 100644 index 0000000..99cd2d6 --- /dev/null +++ b/gen/pb/api.pb.go @@ -0,0 +1,89 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.15.8 +// source: api.proto + +package pb + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" +) + +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) +) + +var File_api_proto protoreflect.FileDescriptor + +var file_api_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x61, 0x70, 0x69, + 0x1a, 0x0e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x1a, 0x0c, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0x98, + 0x01, 0x0a, 0x0a, 0x41, 0x70, 0x69, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x89, 0x01, + 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x19, 0x2e, + 0x61, 0x70, 0x69, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x53, 0x65, 0x6e, + 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x43, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x14, 0x22, 0x0f, 0x2f, 0x61, + 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x6d, 0x73, 0x3a, 0x01, 0x2a, + 0x92, 0x41, 0x26, 0x0a, 0x08, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x12, 0x0c, 0x53, + 0x65, 0x6e, 0x64, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x53, 0x4d, 0x53, 0x1a, 0x0c, 0x53, 0x65, 0x6e, + 0x64, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x53, 0x4d, 0x53, 0x42, 0xa2, 0x01, 0x5a, 0x06, 0x67, 0x65, + 0x6e, 0x2f, 0x70, 0x62, 0x92, 0x41, 0x96, 0x01, 0x12, 0x5d, 0x0a, 0x0c, 0x54, 0x65, 0x73, 0x74, + 0x20, 0x53, 0x77, 0x61, 0x67, 0x67, 0x65, 0x72, 0x22, 0x48, 0x0a, 0x10, 0x4e, 0x69, 0x6b, 0x6f, + 0x6c, 0x61, 0x79, 0x20, 0x56, 0x61, 0x72, 0x6c, 0x61, 0x6d, 0x6f, 0x76, 0x12, 0x1c, 0x68, 0x74, + 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x6d, 0x69, 0x6e, 0x69, 0x73, 0x68, 0x31, 0x34, 0x34, 0x1a, 0x16, 0x76, 0x61, 0x72, 0x6c, + 0x61, 0x6d, 0x6f, 0x77, 0x2e, 0x63, 0x6f, 0x6c, 0x40, 0x79, 0x61, 0x68, 0x6f, 0x6f, 0x2e, 0x63, + 0x6f, 0x6d, 0x32, 0x03, 0x31, 0x2e, 0x30, 0x1a, 0x0e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, + 0x73, 0x74, 0x3a, 0x33, 0x30, 0x31, 0x30, 0x2a, 0x01, 0x01, 0x32, 0x10, 0x61, 0x70, 0x70, 0x6c, + 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x3a, 0x10, 0x61, 0x70, + 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var file_api_proto_goTypes = []interface{}{ + (*Messages_SendRequest)(nil), // 0: api.Messages.SendRequest + (*Messages_SendResponse)(nil), // 1: api.Messages.SendResponse +} +var file_api_proto_depIdxs = []int32{ + 0, // 0: api.ApiService.SendMessage:input_type -> api.Messages.SendRequest + 1, // 1: api.ApiService.SendMessage:output_type -> api.Messages.SendResponse + 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_api_proto_init() } +func file_api_proto_init() { + if File_api_proto != nil { + return + } + file_messages_proto_init() + file_common_proto_init() + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_api_proto_rawDesc, + NumEnums: 0, + NumMessages: 0, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_api_proto_goTypes, + DependencyIndexes: file_api_proto_depIdxs, + }.Build() + File_api_proto = out.File + file_api_proto_rawDesc = nil + file_api_proto_goTypes = nil + file_api_proto_depIdxs = nil +} diff --git a/gen/pb/api.pb.gw.go b/gen/pb/api.pb.gw.go new file mode 100644 index 0000000..b6263d3 --- /dev/null +++ b/gen/pb/api.pb.gw.go @@ -0,0 +1,169 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: api.proto + +/* +Package pb is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package pb + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/descriptor" + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" +) + +// Suppress "imported and not used" errors +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray +var _ = descriptor.ForMessage +var _ = metadata.Join + +func request_ApiService_SendMessage_0(ctx context.Context, marshaler runtime.Marshaler, client ApiServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq Messages_SendRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SendMessage(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_ApiService_SendMessage_0(ctx context.Context, marshaler runtime.Marshaler, server ApiServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq Messages_SendRequest + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.SendMessage(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterApiServiceHandlerServer registers the http handlers for service ApiService to "mux". +// UnaryRPC :call ApiServiceServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +// Note that using this registration option will cause many gRPC library features to stop working. Consider using RegisterApiServiceHandlerFromEndpoint instead. +func RegisterApiServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux, server ApiServiceServer) error { + + mux.Handle("POST", pattern_ApiService_SendMessage_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + var stream runtime.ServerTransportStream + ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_ApiService_SendMessage_0(rctx, inboundMarshaler, server, req, pathParams) + md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_ApiService_SendMessage_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterApiServiceHandlerFromEndpoint is same as RegisterApiServiceHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterApiServiceHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterApiServiceHandler(ctx, mux, conn) +} + +// RegisterApiServiceHandler registers the http handlers for service ApiService to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterApiServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterApiServiceHandlerClient(ctx, mux, NewApiServiceClient(conn)) +} + +// RegisterApiServiceHandlerClient registers the http handlers for service ApiService +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "ApiServiceClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "ApiServiceClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "ApiServiceClient" to call the correct interceptors. +func RegisterApiServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, client ApiServiceClient) error { + + mux.Handle("POST", pattern_ApiService_SendMessage_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_ApiService_SendMessage_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_ApiService_SendMessage_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_ApiService_SendMessage_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "SendSms"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_ApiService_SendMessage_0 = runtime.ForwardResponseMessage +) diff --git a/gen/pb/api.pb.validate.go b/gen/pb/api.pb.validate.go new file mode 100644 index 0000000..a99f422 --- /dev/null +++ b/gen/pb/api.pb.validate.go @@ -0,0 +1,36 @@ +// Code generated by protoc-gen-validate. DO NOT EDIT. +// source: api.proto + +package pb + +import ( + "bytes" + "errors" + "fmt" + "net" + "net/mail" + "net/url" + "regexp" + "sort" + "strings" + "time" + "unicode/utf8" + + "google.golang.org/protobuf/types/known/anypb" +) + +// ensure the imports are used +var ( + _ = bytes.MinRead + _ = errors.New("") + _ = fmt.Print + _ = utf8.UTFMax + _ = (*regexp.Regexp)(nil) + _ = (*strings.Reader)(nil) + _ = net.IPv4len + _ = time.Duration(0) + _ = (*url.URL)(nil) + _ = (*mail.Address)(nil) + _ = anypb.Any{} + _ = sort.Sort +) diff --git a/gen/pb/api_grpc.pb.go b/gen/pb/api_grpc.pb.go new file mode 100644 index 0000000..3cdb4f8 --- /dev/null +++ b/gen/pb/api_grpc.pb.go @@ -0,0 +1,101 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. + +package pb + +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.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// ApiServiceClient is the client API for ApiService 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 ApiServiceClient interface { + SendMessage(ctx context.Context, in *Messages_SendRequest, opts ...grpc.CallOption) (*Messages_SendResponse, error) +} + +type apiServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewApiServiceClient(cc grpc.ClientConnInterface) ApiServiceClient { + return &apiServiceClient{cc} +} + +func (c *apiServiceClient) SendMessage(ctx context.Context, in *Messages_SendRequest, opts ...grpc.CallOption) (*Messages_SendResponse, error) { + out := new(Messages_SendResponse) + err := c.cc.Invoke(ctx, "/api.ApiService/SendMessage", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ApiServiceServer is the server API for ApiService service. +// All implementations must embed UnimplementedApiServiceServer +// for forward compatibility +type ApiServiceServer interface { + SendMessage(context.Context, *Messages_SendRequest) (*Messages_SendResponse, error) + mustEmbedUnimplementedApiServiceServer() +} + +// UnimplementedApiServiceServer must be embedded to have forward compatible implementations. +type UnimplementedApiServiceServer struct { +} + +func (UnimplementedApiServiceServer) SendMessage(context.Context, *Messages_SendRequest) (*Messages_SendResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method SendMessage not implemented") +} +func (UnimplementedApiServiceServer) mustEmbedUnimplementedApiServiceServer() {} + +// UnsafeApiServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ApiServiceServer will +// result in compilation errors. +type UnsafeApiServiceServer interface { + mustEmbedUnimplementedApiServiceServer() +} + +func RegisterApiServiceServer(s grpc.ServiceRegistrar, srv ApiServiceServer) { + s.RegisterService(&ApiService_ServiceDesc, srv) +} + +func _ApiService_SendMessage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Messages_SendRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ApiServiceServer).SendMessage(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/api.ApiService/SendMessage", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ApiServiceServer).SendMessage(ctx, req.(*Messages_SendRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ApiService_ServiceDesc is the grpc.ServiceDesc for ApiService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ApiService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "api.ApiService", + HandlerType: (*ApiServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "SendMessage", + Handler: _ApiService_SendMessage_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "api.proto", +} diff --git a/gen/pb/common.pb.go b/gen/pb/common.pb.go new file mode 100644 index 0000000..1313769 --- /dev/null +++ b/gen/pb/common.pb.go @@ -0,0 +1,261 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.15.8 +// source: common.proto + +package pb + +import ( + validate "github.com/envoyproxy/protoc-gen-validate/validate" + options "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options" + annotations "google.golang.org/genproto/googleapis/api/annotations" + 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) +) + +// Symbols defined in public import of github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api/annotations.proto. + +var E_Http = annotations.E_Http + +// Symbols defined in public import of github.com/grpc-ecosystem/grpc-gateway/protoc-gen-openapiv2/options/annotations.proto. + +var E_Openapiv2Swagger = options.E_Openapiv2Swagger +var E_Openapiv2Operation = options.E_Openapiv2Operation +var E_Openapiv2Schema = options.E_Openapiv2Schema +var E_Openapiv2Tag = options.E_Openapiv2Tag +var E_Openapiv2Field = options.E_Openapiv2Field + +// Symbols defined in public import of github.com/envoyproxy/protoc-gen-validate/validate/validate.proto. + +type KnownRegex = validate.KnownRegex + +const KnownRegex_UNKNOWN = validate.KnownRegex_UNKNOWN +const KnownRegex_HTTP_HEADER_NAME = validate.KnownRegex_HTTP_HEADER_NAME +const KnownRegex_HTTP_HEADER_VALUE = validate.KnownRegex_HTTP_HEADER_VALUE + +var KnownRegex_name = validate.KnownRegex_name +var KnownRegex_value = validate.KnownRegex_value + +type FieldRules = validate.FieldRules +type FieldRules_Float = validate.FieldRules_Float +type FieldRules_Double = validate.FieldRules_Double +type FieldRules_Int32 = validate.FieldRules_Int32 +type FieldRules_Int64 = validate.FieldRules_Int64 +type FieldRules_Uint32 = validate.FieldRules_Uint32 +type FieldRules_Uint64 = validate.FieldRules_Uint64 +type FieldRules_Sint32 = validate.FieldRules_Sint32 +type FieldRules_Sint64 = validate.FieldRules_Sint64 +type FieldRules_Fixed32 = validate.FieldRules_Fixed32 +type FieldRules_Fixed64 = validate.FieldRules_Fixed64 +type FieldRules_Sfixed32 = validate.FieldRules_Sfixed32 +type FieldRules_Sfixed64 = validate.FieldRules_Sfixed64 +type FieldRules_Bool = validate.FieldRules_Bool +type FieldRules_String_ = validate.FieldRules_String_ +type FieldRules_Bytes = validate.FieldRules_Bytes +type FieldRules_Enum = validate.FieldRules_Enum +type FieldRules_Repeated = validate.FieldRules_Repeated +type FieldRules_Map = validate.FieldRules_Map +type FieldRules_Any = validate.FieldRules_Any +type FieldRules_Duration = validate.FieldRules_Duration +type FieldRules_Timestamp = validate.FieldRules_Timestamp +type FloatRules = validate.FloatRules +type DoubleRules = validate.DoubleRules +type Int32Rules = validate.Int32Rules +type Int64Rules = validate.Int64Rules +type UInt32Rules = validate.UInt32Rules +type UInt64Rules = validate.UInt64Rules +type SInt32Rules = validate.SInt32Rules +type SInt64Rules = validate.SInt64Rules +type Fixed32Rules = validate.Fixed32Rules +type Fixed64Rules = validate.Fixed64Rules +type SFixed32Rules = validate.SFixed32Rules +type SFixed64Rules = validate.SFixed64Rules +type BoolRules = validate.BoolRules +type StringRules = validate.StringRules + +const Default_StringRules_Strict = validate.Default_StringRules_Strict + +type StringRules_Email = validate.StringRules_Email +type StringRules_Hostname = validate.StringRules_Hostname +type StringRules_Ip = validate.StringRules_Ip +type StringRules_Ipv4 = validate.StringRules_Ipv4 +type StringRules_Ipv6 = validate.StringRules_Ipv6 +type StringRules_Uri = validate.StringRules_Uri +type StringRules_UriRef = validate.StringRules_UriRef +type StringRules_Address = validate.StringRules_Address +type StringRules_Uuid = validate.StringRules_Uuid +type StringRules_WellKnownRegex = validate.StringRules_WellKnownRegex +type BytesRules = validate.BytesRules +type BytesRules_Ip = validate.BytesRules_Ip +type BytesRules_Ipv4 = validate.BytesRules_Ipv4 +type BytesRules_Ipv6 = validate.BytesRules_Ipv6 +type EnumRules = validate.EnumRules +type MessageRules = validate.MessageRules +type RepeatedRules = validate.RepeatedRules +type MapRules = validate.MapRules +type AnyRules = validate.AnyRules +type DurationRules = validate.DurationRules +type TimestampRules = validate.TimestampRules + +var E_Disabled = validate.E_Disabled +var E_Ignored = validate.E_Ignored +var E_Required = validate.E_Required +var E_Rules = validate.E_Rules + +type Message struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Phone string `protobuf:"bytes,1,opt,name=phone,proto3" json:"phone,omitempty"` + Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *Message) Reset() { + *x = Message{} + if protoimpl.UnsafeEnabled { + mi := &file_common_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Message) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Message) ProtoMessage() {} + +func (x *Message) ProtoReflect() protoreflect.Message { + mi := &file_common_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 Message.ProtoReflect.Descriptor instead. +func (*Message) Descriptor() ([]byte, []int) { + return file_common_proto_rawDescGZIP(), []int{0} +} + +func (x *Message) GetPhone() string { + if x != nil { + return x.Phone + } + return "" +} + +func (x *Message) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_common_proto protoreflect.FileDescriptor + +var file_common_proto_rawDesc = []byte{ + 0x0a, 0x0c, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, + 0x61, 0x70, 0x69, 0x1a, 0x5a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x67, 0x72, 0x70, 0x63, 0x2d, 0x65, 0x63, 0x6f, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2f, 0x67, + 0x72, 0x70, 0x63, 0x2d, 0x67, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x2f, 0x74, 0x68, 0x69, 0x72, + 0x64, 0x5f, 0x70, 0x61, 0x72, 0x74, 0x79, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x61, 0x70, + 0x69, 0x73, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x61, 0x6e, + 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, + 0x55, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x72, 0x70, 0x63, + 0x2d, 0x65, 0x63, 0x6f, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2d, + 0x67, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, + 0x65, 0x6e, 0x2d, 0x6f, 0x70, 0x65, 0x6e, 0x61, 0x70, 0x69, 0x76, 0x32, 0x2f, 0x6f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x41, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x65, 0x6e, 0x76, 0x6f, 0x79, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, + 0x65, 0x2f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2f, 0x76, 0x61, 0x6c, 0x69, 0x64, + 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x5e, 0x0a, 0x07, 0x4d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x12, 0x39, 0x0a, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x42, 0x23, 0xfa, 0x42, 0x20, 0x72, 0x1e, 0x32, 0x1c, 0x5e, 0x5c, 0x2b, 0x28, + 0x3f, 0x3a, 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0xe2, 0x97, 0x8f, 0x3f, 0x29, 0x7b, 0x36, 0x2c, 0x31, + 0x34, 0x7d, 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0x24, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x12, + 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x67, 0x65, 0x6e, + 0x2f, 0x70, 0x62, 0x50, 0x00, 0x50, 0x01, 0x50, 0x02, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +} + +var ( + file_common_proto_rawDescOnce sync.Once + file_common_proto_rawDescData = file_common_proto_rawDesc +) + +func file_common_proto_rawDescGZIP() []byte { + file_common_proto_rawDescOnce.Do(func() { + file_common_proto_rawDescData = protoimpl.X.CompressGZIP(file_common_proto_rawDescData) + }) + return file_common_proto_rawDescData +} + +var file_common_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_common_proto_goTypes = []interface{}{ + (*Message)(nil), // 0: api.Message +} +var file_common_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_common_proto_init() } +func file_common_proto_init() { + if File_common_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_common_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Message); 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_common_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_common_proto_goTypes, + DependencyIndexes: file_common_proto_depIdxs, + MessageInfos: file_common_proto_msgTypes, + }.Build() + File_common_proto = out.File + file_common_proto_rawDesc = nil + file_common_proto_goTypes = nil + file_common_proto_depIdxs = nil +} diff --git a/gen/pb/common.pb.validate.go b/gen/pb/common.pb.validate.go new file mode 100644 index 0000000..130f36e --- /dev/null +++ b/gen/pb/common.pb.validate.go @@ -0,0 +1,148 @@ +// Code generated by protoc-gen-validate. DO NOT EDIT. +// source: common.proto + +package pb + +import ( + "bytes" + "errors" + "fmt" + "net" + "net/mail" + "net/url" + "regexp" + "sort" + "strings" + "time" + "unicode/utf8" + + "google.golang.org/protobuf/types/known/anypb" +) + +// ensure the imports are used +var ( + _ = bytes.MinRead + _ = errors.New("") + _ = fmt.Print + _ = utf8.UTFMax + _ = (*regexp.Regexp)(nil) + _ = (*strings.Reader)(nil) + _ = net.IPv4len + _ = time.Duration(0) + _ = (*url.URL)(nil) + _ = (*mail.Address)(nil) + _ = anypb.Any{} + _ = sort.Sort +) + +// Validate checks the field values on Message with the rules defined in the +// proto definition for this message. If any rules are violated, the first +// error encountered is returned, or nil if there are no violations. +func (m *Message) Validate() error { + return m.validate(false) +} + +// ValidateAll checks the field values on Message with the rules defined in the +// proto definition for this message. If any rules are violated, the result is +// a list of violation errors wrapped in MessageMultiError, or nil if none found. +func (m *Message) ValidateAll() error { + return m.validate(true) +} + +func (m *Message) validate(all bool) error { + if m == nil { + return nil + } + + var errors []error + + if !_Message_Phone_Pattern.MatchString(m.GetPhone()) { + err := MessageValidationError{ + field: "Phone", + reason: "value does not match regex pattern \"^\\\\+(?:[0-9]●?){6,14}[0-9]$\"", + } + if !all { + return err + } + errors = append(errors, err) + } + + // no validation rules for Message + + if len(errors) > 0 { + return MessageMultiError(errors) + } + return nil +} + +// MessageMultiError is an error wrapping multiple validation errors returned +// by Message.ValidateAll() if the designated constraints aren't met. +type MessageMultiError []error + +// Error returns a concatenation of all the error messages it wraps. +func (m MessageMultiError) Error() string { + var msgs []string + for _, err := range m { + msgs = append(msgs, err.Error()) + } + return strings.Join(msgs, "; ") +} + +// AllErrors returns a list of validation violation errors. +func (m MessageMultiError) AllErrors() []error { return m } + +// MessageValidationError is the validation error returned by Message.Validate +// if the designated constraints aren't met. +type MessageValidationError struct { + field string + reason string + cause error + key bool +} + +// Field function returns field value. +func (e MessageValidationError) Field() string { return e.field } + +// Reason function returns reason value. +func (e MessageValidationError) Reason() string { return e.reason } + +// Cause function returns cause value. +func (e MessageValidationError) Cause() error { return e.cause } + +// Key function returns key value. +func (e MessageValidationError) Key() bool { return e.key } + +// ErrorName returns error name. +func (e MessageValidationError) ErrorName() string { return "MessageValidationError" } + +// Error satisfies the builtin error interface +func (e MessageValidationError) Error() string { + cause := "" + if e.cause != nil { + cause = fmt.Sprintf(" | caused by: %v", e.cause) + } + + key := "" + if e.key { + key = "key for " + } + + return fmt.Sprintf( + "invalid %sMessage.%s: %s%s", + key, + e.field, + e.reason, + cause) +} + +var _ error = MessageValidationError{} + +var _ interface { + Field() string + Reason() string + Key() bool + Cause() error + ErrorName() string +} = MessageValidationError{} + +var _Message_Phone_Pattern = regexp.MustCompile("^\\+(?:[0-9]●?){6,14}[0-9]$") diff --git a/gen/pb/messages.pb.go b/gen/pb/messages.pb.go new file mode 100644 index 0000000..162a059 --- /dev/null +++ b/gen/pb/messages.pb.go @@ -0,0 +1,250 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.15.8 +// source: messages.proto + +package pb + +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 Messages struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *Messages) Reset() { + *x = Messages{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Messages) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Messages) ProtoMessage() {} + +func (x *Messages) ProtoReflect() protoreflect.Message { + mi := &file_messages_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 Messages.ProtoReflect.Descriptor instead. +func (*Messages) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{0} +} + +type Messages_SendRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Message *Message `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *Messages_SendRequest) Reset() { + *x = Messages_SendRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Messages_SendRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Messages_SendRequest) ProtoMessage() {} + +func (x *Messages_SendRequest) ProtoReflect() protoreflect.Message { + mi := &file_messages_proto_msgTypes[1] + 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 Messages_SendRequest.ProtoReflect.Descriptor instead. +func (*Messages_SendRequest) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{0, 0} +} + +func (x *Messages_SendRequest) GetMessage() *Message { + if x != nil { + return x.Message + } + return nil +} + +type Messages_SendResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *Messages_SendResponse) Reset() { + *x = Messages_SendResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Messages_SendResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Messages_SendResponse) ProtoMessage() {} + +func (x *Messages_SendResponse) ProtoReflect() protoreflect.Message { + mi := &file_messages_proto_msgTypes[2] + 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 Messages_SendResponse.ProtoReflect.Descriptor instead. +func (*Messages_SendResponse) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{0, 1} +} + +var File_messages_proto protoreflect.FileDescriptor + +var file_messages_proto_rawDesc = []byte{ + 0x0a, 0x0e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x03, 0x61, 0x70, 0x69, 0x1a, 0x0c, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x51, 0x0a, 0x08, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x1a, + 0x35, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x26, + 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x0c, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x07, 0x6d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0e, 0x0a, 0x0c, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x62, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_messages_proto_rawDescOnce sync.Once + file_messages_proto_rawDescData = file_messages_proto_rawDesc +) + +func file_messages_proto_rawDescGZIP() []byte { + file_messages_proto_rawDescOnce.Do(func() { + file_messages_proto_rawDescData = protoimpl.X.CompressGZIP(file_messages_proto_rawDescData) + }) + return file_messages_proto_rawDescData +} + +var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_messages_proto_goTypes = []interface{}{ + (*Messages)(nil), // 0: api.Messages + (*Messages_SendRequest)(nil), // 1: api.Messages.SendRequest + (*Messages_SendResponse)(nil), // 2: api.Messages.SendResponse + (*Message)(nil), // 3: api.Message +} +var file_messages_proto_depIdxs = []int32{ + 3, // 0: api.Messages.SendRequest.message:type_name -> api.Message + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_messages_proto_init() } +func file_messages_proto_init() { + if File_messages_proto != nil { + return + } + file_common_proto_init() + if !protoimpl.UnsafeEnabled { + file_messages_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Messages); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_messages_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Messages_SendRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_messages_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Messages_SendResponse); 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_messages_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_messages_proto_goTypes, + DependencyIndexes: file_messages_proto_depIdxs, + MessageInfos: file_messages_proto_msgTypes, + }.Build() + File_messages_proto = out.File + file_messages_proto_rawDesc = nil + file_messages_proto_goTypes = nil + file_messages_proto_depIdxs = nil +} diff --git a/gen/pb/messages.pb.validate.go b/gen/pb/messages.pb.validate.go new file mode 100644 index 0000000..8f7c4cb --- /dev/null +++ b/gen/pb/messages.pb.validate.go @@ -0,0 +1,365 @@ +// Code generated by protoc-gen-validate. DO NOT EDIT. +// source: messages.proto + +package pb + +import ( + "bytes" + "errors" + "fmt" + "net" + "net/mail" + "net/url" + "regexp" + "sort" + "strings" + "time" + "unicode/utf8" + + "google.golang.org/protobuf/types/known/anypb" +) + +// ensure the imports are used +var ( + _ = bytes.MinRead + _ = errors.New("") + _ = fmt.Print + _ = utf8.UTFMax + _ = (*regexp.Regexp)(nil) + _ = (*strings.Reader)(nil) + _ = net.IPv4len + _ = time.Duration(0) + _ = (*url.URL)(nil) + _ = (*mail.Address)(nil) + _ = anypb.Any{} + _ = sort.Sort +) + +// Validate checks the field values on Messages with the rules defined in the +// proto definition for this message. If any rules are violated, the first +// error encountered is returned, or nil if there are no violations. +func (m *Messages) Validate() error { + return m.validate(false) +} + +// ValidateAll checks the field values on Messages with the rules defined in +// the proto definition for this message. If any rules are violated, the +// result is a list of violation errors wrapped in MessagesMultiError, or nil +// if none found. +func (m *Messages) ValidateAll() error { + return m.validate(true) +} + +func (m *Messages) validate(all bool) error { + if m == nil { + return nil + } + + var errors []error + + if len(errors) > 0 { + return MessagesMultiError(errors) + } + return nil +} + +// MessagesMultiError is an error wrapping multiple validation errors returned +// by Messages.ValidateAll() if the designated constraints aren't met. +type MessagesMultiError []error + +// Error returns a concatenation of all the error messages it wraps. +func (m MessagesMultiError) Error() string { + var msgs []string + for _, err := range m { + msgs = append(msgs, err.Error()) + } + return strings.Join(msgs, "; ") +} + +// AllErrors returns a list of validation violation errors. +func (m MessagesMultiError) AllErrors() []error { return m } + +// MessagesValidationError is the validation error returned by +// Messages.Validate if the designated constraints aren't met. +type MessagesValidationError struct { + field string + reason string + cause error + key bool +} + +// Field function returns field value. +func (e MessagesValidationError) Field() string { return e.field } + +// Reason function returns reason value. +func (e MessagesValidationError) Reason() string { return e.reason } + +// Cause function returns cause value. +func (e MessagesValidationError) Cause() error { return e.cause } + +// Key function returns key value. +func (e MessagesValidationError) Key() bool { return e.key } + +// ErrorName returns error name. +func (e MessagesValidationError) ErrorName() string { return "MessagesValidationError" } + +// Error satisfies the builtin error interface +func (e MessagesValidationError) Error() string { + cause := "" + if e.cause != nil { + cause = fmt.Sprintf(" | caused by: %v", e.cause) + } + + key := "" + if e.key { + key = "key for " + } + + return fmt.Sprintf( + "invalid %sMessages.%s: %s%s", + key, + e.field, + e.reason, + cause) +} + +var _ error = MessagesValidationError{} + +var _ interface { + Field() string + Reason() string + Key() bool + Cause() error + ErrorName() string +} = MessagesValidationError{} + +// Validate checks the field values on Messages_SendRequest with the rules +// defined in the proto definition for this message. If any rules are +// violated, the first error encountered is returned, or nil if there are no violations. +func (m *Messages_SendRequest) Validate() error { + return m.validate(false) +} + +// ValidateAll checks the field values on Messages_SendRequest with the rules +// defined in the proto definition for this message. If any rules are +// violated, the result is a list of violation errors wrapped in +// Messages_SendRequestMultiError, or nil if none found. +func (m *Messages_SendRequest) ValidateAll() error { + return m.validate(true) +} + +func (m *Messages_SendRequest) validate(all bool) error { + if m == nil { + return nil + } + + var errors []error + + if all { + switch v := interface{}(m.GetMessage()).(type) { + case interface{ ValidateAll() error }: + if err := v.ValidateAll(); err != nil { + errors = append(errors, Messages_SendRequestValidationError{ + field: "Message", + reason: "embedded message failed validation", + cause: err, + }) + } + case interface{ Validate() error }: + if err := v.Validate(); err != nil { + errors = append(errors, Messages_SendRequestValidationError{ + field: "Message", + reason: "embedded message failed validation", + cause: err, + }) + } + } + } else if v, ok := interface{}(m.GetMessage()).(interface{ Validate() error }); ok { + if err := v.Validate(); err != nil { + return Messages_SendRequestValidationError{ + field: "Message", + reason: "embedded message failed validation", + cause: err, + } + } + } + + if len(errors) > 0 { + return Messages_SendRequestMultiError(errors) + } + return nil +} + +// Messages_SendRequestMultiError is an error wrapping multiple validation +// errors returned by Messages_SendRequest.ValidateAll() if the designated +// constraints aren't met. +type Messages_SendRequestMultiError []error + +// Error returns a concatenation of all the error messages it wraps. +func (m Messages_SendRequestMultiError) Error() string { + var msgs []string + for _, err := range m { + msgs = append(msgs, err.Error()) + } + return strings.Join(msgs, "; ") +} + +// AllErrors returns a list of validation violation errors. +func (m Messages_SendRequestMultiError) AllErrors() []error { return m } + +// Messages_SendRequestValidationError is the validation error returned by +// Messages_SendRequest.Validate if the designated constraints aren't met. +type Messages_SendRequestValidationError struct { + field string + reason string + cause error + key bool +} + +// Field function returns field value. +func (e Messages_SendRequestValidationError) Field() string { return e.field } + +// Reason function returns reason value. +func (e Messages_SendRequestValidationError) Reason() string { return e.reason } + +// Cause function returns cause value. +func (e Messages_SendRequestValidationError) Cause() error { return e.cause } + +// Key function returns key value. +func (e Messages_SendRequestValidationError) Key() bool { return e.key } + +// ErrorName returns error name. +func (e Messages_SendRequestValidationError) ErrorName() string { + return "Messages_SendRequestValidationError" +} + +// Error satisfies the builtin error interface +func (e Messages_SendRequestValidationError) Error() string { + cause := "" + if e.cause != nil { + cause = fmt.Sprintf(" | caused by: %v", e.cause) + } + + key := "" + if e.key { + key = "key for " + } + + return fmt.Sprintf( + "invalid %sMessages_SendRequest.%s: %s%s", + key, + e.field, + e.reason, + cause) +} + +var _ error = Messages_SendRequestValidationError{} + +var _ interface { + Field() string + Reason() string + Key() bool + Cause() error + ErrorName() string +} = Messages_SendRequestValidationError{} + +// Validate checks the field values on Messages_SendResponse with the rules +// defined in the proto definition for this message. If any rules are +// violated, the first error encountered is returned, or nil if there are no violations. +func (m *Messages_SendResponse) Validate() error { + return m.validate(false) +} + +// ValidateAll checks the field values on Messages_SendResponse with the rules +// defined in the proto definition for this message. If any rules are +// violated, the result is a list of violation errors wrapped in +// Messages_SendResponseMultiError, or nil if none found. +func (m *Messages_SendResponse) ValidateAll() error { + return m.validate(true) +} + +func (m *Messages_SendResponse) validate(all bool) error { + if m == nil { + return nil + } + + var errors []error + + if len(errors) > 0 { + return Messages_SendResponseMultiError(errors) + } + return nil +} + +// Messages_SendResponseMultiError is an error wrapping multiple validation +// errors returned by Messages_SendResponse.ValidateAll() if the designated +// constraints aren't met. +type Messages_SendResponseMultiError []error + +// Error returns a concatenation of all the error messages it wraps. +func (m Messages_SendResponseMultiError) Error() string { + var msgs []string + for _, err := range m { + msgs = append(msgs, err.Error()) + } + return strings.Join(msgs, "; ") +} + +// AllErrors returns a list of validation violation errors. +func (m Messages_SendResponseMultiError) AllErrors() []error { return m } + +// Messages_SendResponseValidationError is the validation error returned by +// Messages_SendResponse.Validate if the designated constraints aren't met. +type Messages_SendResponseValidationError struct { + field string + reason string + cause error + key bool +} + +// Field function returns field value. +func (e Messages_SendResponseValidationError) Field() string { return e.field } + +// Reason function returns reason value. +func (e Messages_SendResponseValidationError) Reason() string { return e.reason } + +// Cause function returns cause value. +func (e Messages_SendResponseValidationError) Cause() error { return e.cause } + +// Key function returns key value. +func (e Messages_SendResponseValidationError) Key() bool { return e.key } + +// ErrorName returns error name. +func (e Messages_SendResponseValidationError) ErrorName() string { + return "Messages_SendResponseValidationError" +} + +// Error satisfies the builtin error interface +func (e Messages_SendResponseValidationError) Error() string { + cause := "" + if e.cause != nil { + cause = fmt.Sprintf(" | caused by: %v", e.cause) + } + + key := "" + if e.key { + key = "key for " + } + + return fmt.Sprintf( + "invalid %sMessages_SendResponse.%s: %s%s", + key, + e.field, + e.reason, + cause) +} + +var _ error = Messages_SendResponseValidationError{} + +var _ interface { + Field() string + Reason() string + Key() bool + Cause() error + ErrorName() string +} = Messages_SendResponseValidationError{} diff --git a/gen/swagger/api.swagger.json b/gen/swagger/api.swagger.json new file mode 100644 index 0000000..9f12aa5 --- /dev/null +++ b/gen/swagger/api.swagger.json @@ -0,0 +1,115 @@ +{ + "swagger": "2.0", + "info": { + "title": "Test Swagger", + "version": "1.0", + "contact": { + "name": "Nikolay Varlamov", + "url": "https://github.com/minish144", + "email": "varlamow.col@yahoo.com" + } + }, + "host": "localhost:3010", + "schemes": [ + "http" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": { + "/api/v1/SendSms": { + "post": { + "summary": "Sends an SMS", + "description": "Sends an SMS", + "operationId": "ApiService_SendMessage", + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/MessagesSendResponse" + } + }, + "default": { + "description": "An unexpected error response.", + "schema": { + "$ref": "#/definitions/runtimeError" + } + } + }, + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/MessagesSendRequest" + } + } + ], + "tags": [ + "Messages" + ] + } + } + }, + "definitions": { + "MessagesSendRequest": { + "type": "object", + "properties": { + "message": { + "$ref": "#/definitions/apiMessage" + } + } + }, + "MessagesSendResponse": { + "type": "object" + }, + "apiMessage": { + "type": "object", + "properties": { + "phone": { + "type": "string" + }, + "message": { + "type": "string" + } + } + }, + "protobufAny": { + "type": "object", + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "type": "string", + "format": "byte" + } + } + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } + } + } +} diff --git a/go.mod b/go.mod index 2675d3f..e1efed5 100644 --- a/go.mod +++ b/go.mod @@ -2,22 +2,34 @@ module github.com/minish144/go-sms-api go 1.17 -require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 +require ( + github.com/envoyproxy/protoc-gen-validate v0.6.1 + github.com/golang/protobuf v1.5.2 + github.com/gorilla/mux v1.8.0 + github.com/grpc-ecosystem/grpc-gateway v1.16.0 + github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 + github.com/sirupsen/logrus v1.8.1 + github.com/spf13/viper v1.9.0 + github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 + google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 + google.golang.org/grpc v1.40.0 + google.golang.org/protobuf v1.27.1 +) require ( + github.com/felixge/httpsnoop v1.0.1 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect - github.com/gorilla/mux v1.8.0 // indirect + github.com/gorilla/handlers v1.5.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/magiconair/properties v1.8.5 // indirect github.com/mitchellh/mapstructure v1.4.2 // indirect github.com/pelletier/go-toml v1.9.4 // indirect - github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/spf13/viper v1.9.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect + golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect golang.org/x/text v0.3.6 // indirect gopkg.in/ini.v1 v1.63.2 // indirect diff --git a/go.sum b/go.sum index 13a4d0f..5d06d93 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,7 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -72,9 +73,14 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= +github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/envoyproxy/protoc-gen-validate v0.6.1 h1:4CF52PCseTFt4bE+Yk3dIpdVi7XWuPVMhPtm4FaIJPM= +github.com/envoyproxy/protoc-gen-validate v0.6.1/go.mod h1:txg5va2Qkip90uYoSKH+nkAAmXrb2j3iq4FLwdrCbXQ= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= +github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -84,6 +90,7 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2 github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -112,6 +119,7 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= @@ -127,6 +135,7 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -152,9 +161,14 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0= +github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= +github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 h1:rgxjzoDmDXw5q8HONgyHhBas4to0/XWRo/gPpJhsUNQ= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0/go.mod h1:qrJPVzv9YlhsrxJc3P/Q85nr0w1lIRikTl4JlhdDH5w= github.com/hashicorp/consul/api v1.10.1/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M= github.com/hashicorp/consul/sdk v0.8.0/go.mod h1:GBvyrGALthsZObzUGsfgHZQDXjg4lOjagTIwIR1vPms= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -177,6 +191,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY= github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE= github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk= +github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -186,9 +201,12 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lyft/protoc-gen-star v0.5.1/go.mod h1:9toiA3cC7z5uVbODF7kEQ91Xn7XNFkVUl+SrEe+ZORU= github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -216,6 +234,7 @@ github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhEC github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= @@ -228,6 +247,8 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= +github.com/spf13/afero v1.3.4/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= @@ -244,6 +265,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= @@ -348,6 +370,7 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 h1:a8jGStKg0XqKDlKqjLrXn0ioF5MH36pT7Z0BRTqLhbk= golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -482,6 +505,7 @@ golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWc golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= @@ -502,6 +526,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= @@ -589,6 +614,8 @@ google.golang.org/genproto v0.0.0-20210805201207-89edb61ffb67/go.mod h1:ob2IJxKr google.golang.org/genproto v0.0.0-20210813162853-db860fec028c/go.mod h1:cFeNkxwySK631ADgubI+/XFU/xp8FD5KIVV4rj8UC5w= google.golang.org/genproto v0.0.0-20210821163610-241b8fcbd6c8/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= google.golang.org/genproto v0.0.0-20210828152312-66f60bf46e71/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= +google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 h1:3V2dxSZpz4zozWWUq36vUxXEKnSYitEH2LdsAx+RUmg= +google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -613,6 +640,7 @@ google.golang.org/grpc v1.37.1/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQ google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= google.golang.org/grpc v1.39.1/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= +google.golang.org/grpc v1.40.0 h1:AGJ0Ih4mHjSeibYkFGh1dD9KJ/eOtZ93I6hoHhukQ5Q= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= @@ -627,9 +655,11 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/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-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c= @@ -640,6 +670,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index bf09af0..986e9c6 100644 --- a/main.go +++ b/main.go @@ -20,11 +20,5 @@ func main() { logrus.Info("viper config initialized successfully") // server init - if err := server.Run(); err != nil { - logrus.WithFields( - logrus.Fields{ - "error": err.Error(), - }, - ).Fatalln("server failed") - } + server.Run() } diff --git a/modules/modem/handlers.go b/modules/modem/handlers.go new file mode 100644 index 0000000..df19b44 --- /dev/null +++ b/modules/modem/handlers.go @@ -0,0 +1 @@ +package modem diff --git a/modules/modem/modem.go b/modules/modem/modem.go index c08b841..123b4f3 100644 --- a/modules/modem/modem.go +++ b/modules/modem/modem.go @@ -15,11 +15,6 @@ type Modem struct { instance *serial.Port } -type Message struct { - Phone string - Message string -} - // TODO add checking if device is connected func New(comport string, bound int) *Modem { m := &Modem{comport: comport, bound: bound} diff --git a/modules/server/handlers.go b/modules/server/handlers.go index 6a2ad30..e14179c 100644 --- a/modules/server/handlers.go +++ b/modules/server/handlers.go @@ -4,21 +4,18 @@ import ( "encoding/json" "net/http" + "github.com/minish144/go-sms-api/gen/pb" + "github.com/minish144/go-sms-api/modules/modem" "github.com/sirupsen/logrus" "github.com/spf13/viper" ) -type sms struct { - Phone string `json:"phone"` - Message string `json:"message"` -} - // TODO add response message func HandleSendSMS(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) defer r.Body.Close() - newSms := sms{} + newSms := pb.Message{} if err := decoder.Decode(&newSms); err != nil { w.WriteHeader(http.StatusInternalServerError) return diff --git a/modules/server/server.go b/modules/server/server.go index 487358e..9a19cab 100644 --- a/modules/server/server.go +++ b/modules/server/server.go @@ -1,15 +1,21 @@ package server import ( + "context" "fmt" + "net" "net/http" + "github.com/gorilla/handlers" "github.com/gorilla/mux" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/minish144/go-sms-api/gen/pb" "github.com/sirupsen/logrus" "github.com/spf13/viper" + "google.golang.org/grpc" ) -func Run() error { +func Start() error { router := mux.NewRouter() router.HandleFunc("/api/sms", HandleSendSMS).Methods("POST") @@ -26,3 +32,72 @@ func getListeningAddress() string { port := viper.GetString("server.port") return fmt.Sprintf("%s:%s", host, port) } + +func Run() { + // server side + grpcInterface := viper.GetString("server.grpc.interface") + grpcPort := viper.GetInt("server.grpc.port") + grpcConnectionString := fmt.Sprintf("%s:%d", grpcInterface, grpcPort) + + logrus.Infoln("serving gRPC on " + grpcConnectionString) + lis, err := net.Listen("tcp", grpcConnectionString) + if err != nil { + logrus.WithFields(logrus.Fields{ + "error": err.Error(), + }).Fatalln("failed to listen") + } + grpcServer := grpc.NewServer() + pb.RegisterApiServiceServer(grpcServer, &ApiServiceServer{}) + + // proxy client side + go func() { + if grpcServer.Serve(lis) != nil { + logrus.WithFields(logrus.Fields{ + "error": err.Error(), + }).Fatalln("failed to start server") + } + }() + + conn, err := grpc.DialContext( + context.Background(), + grpcConnectionString, + grpc.WithBlock(), + grpc.WithInsecure(), + ) + + if err != nil { + logrus.WithFields(logrus.Fields{ + "error": err.Error(), + }).Fatalln("failed to dial server") + } + + httpInterface := viper.GetString("server.http.interface") + httpPort := viper.GetInt("server.http.port") + httpConnectionString := fmt.Sprintf("%s:%d", httpInterface, httpPort) + + gwMux := runtime.NewServeMux() + err = pb.RegisterApiServiceHandler(context.Background(), gwMux, conn) + if err != nil { + logrus.WithFields(logrus.Fields{ + "error": err.Error(), + }).Fatalln("failed to register gateway") + } + + mux := http.NewServeMux() + mux.Handle("/", gwMux) + + logrus.Infoln("serving gRPC-Gateway on " + httpConnectionString) + if err := http.ListenAndServe(httpConnectionString, allowCORS(mux)); err != nil { + logrus.WithFields(logrus.Fields{ + "error": err.Error(), + }).Fatalln("gateway listener failed") + } +} + +func allowCORS(h http.Handler) http.Handler { + return handlers.CORS( + handlers.AllowedOrigins([]string{"*"}), + handlers.AllowedMethods([]string{"*"}), + handlers.AllowedHeaders([]string{"*"}), + )(h) +} diff --git a/modules/server/structs.go b/modules/server/structs.go new file mode 100644 index 0000000..578ac33 --- /dev/null +++ b/modules/server/structs.go @@ -0,0 +1,15 @@ +package server + +import ( + "context" + + "github.com/minish144/go-sms-api/gen/pb" +) + +type ApiServiceServer struct { + pb.UnimplementedApiServiceServer +} + +func (s *ApiServiceServer) SendMessage(ctx context.Context, in *pb.Messages_SendRequest) (*pb.Messages_SendResponse, error) { + return nil, nil +} diff --git a/proto/api.proto b/proto/api.proto new file mode 100644 index 0000000..32204f3 --- /dev/null +++ b/proto/api.proto @@ -0,0 +1,40 @@ +syntax = "proto3"; + +package api; + +option go_package = "gen/pb"; + +import "messages.proto"; + +import "common.proto"; + +option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = { + info: { + title: "Test Swagger" + version: "1.0" + contact: { + name: "Nikolay Varlamov" + url: "https://github.com/minish144" + email: "varlamow.col@yahoo.com" + }; + }; + host: "localhost:3010", + schemes: [HTTP] + consumes: "application/json" + produces: "application/json" +}; + +service ApiService { + rpc SendMessage(Messages.SendRequest) returns (Messages.SendResponse) { + option (google.api.http) = { + post: "/api/v1/SendSms"; + body: "*"; + }; + + option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = { + description: "Sends an SMS"; + summary: "Sends an SMS"; + tags: "Messages"; + }; + } +} diff --git a/proto/common.proto b/proto/common.proto new file mode 100644 index 0000000..795cd24 --- /dev/null +++ b/proto/common.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package api; + +option go_package = "gen/pb"; + +import public "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api/annotations.proto"; +import public "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-openapiv2/options/annotations.proto"; +import public "github.com/envoyproxy/protoc-gen-validate/validate/validate.proto"; + +message Message { + string phone = 1 [(validate.rules).string = { pattern: "^\\+(?:[0-9]●?){6,14}[0-9]$" }]; + string message = 2; +} \ No newline at end of file diff --git a/proto/messages.proto b/proto/messages.proto new file mode 100644 index 0000000..914ef91 --- /dev/null +++ b/proto/messages.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package api; + +option go_package = "gen/pb"; + +import "common.proto"; + +message Messages { + message SendRequest { + Message message = 1; + } + + message SendResponse { + } +} \ No newline at end of file