diff --git a/gen/pb/api.pb.go b/gen/pb/api.pb.go index 94e3ccb..c0c14a0 100644 --- a/gen/pb/api.pb.go +++ b/gen/pb/api.pb.go @@ -42,7 +42,7 @@ var file_api_proto_rawDesc = []byte{ 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, 0x02, 0x01, 0x02, 0x32, 0x10, 0x61, 0x70, 0x70, + 0x73, 0x74, 0x3a, 0x33, 0x32, 0x30, 0x31, 0x2a, 0x02, 0x01, 0x02, 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, diff --git a/gen/pb/common.pb.go b/gen/pb/common.pb.go index 1313769..caa221d 100644 --- a/gen/pb/common.pb.go +++ b/gen/pb/common.pb.go @@ -186,15 +186,15 @@ var file_common_proto_rawDesc = []byte{ 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, + 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x6b, 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, + 0x25, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x42, 0x0b, 0xfa, 0x42, 0x08, 0x72, 0x06, 0x18, 0xa0, 0x01, 0xd0, 0x01, 0x01, 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 ( diff --git a/gen/pb/common.pb.validate.go b/gen/pb/common.pb.validate.go index 130f36e..cbb2ef8 100644 --- a/gen/pb/common.pb.validate.go +++ b/gen/pb/common.pb.validate.go @@ -67,7 +67,20 @@ func (m *Message) validate(all bool) error { errors = append(errors, err) } - // no validation rules for Message + if m.GetMessage() != "" { + + if utf8.RuneCountInString(m.GetMessage()) > 160 { + err := MessageValidationError{ + field: "Message", + reason: "value length must be at most 160 runes", + } + if !all { + return err + } + errors = append(errors, err) + } + + } if len(errors) > 0 { return MessageMultiError(errors) diff --git a/gen/swagger/api.swagger.json b/gen/swagger/api.swagger.json index 46dfc32..65fd552 100644 --- a/gen/swagger/api.swagger.json +++ b/gen/swagger/api.swagger.json @@ -9,7 +9,7 @@ "email": "varlamow.col@yahoo.com" } }, - "host": "localhost:3010", + "host": "localhost:3201", "schemes": [ "http", "https" diff --git a/modules/controllers/messages/handlers.go b/modules/controllers/messages/handlers.go new file mode 100644 index 0000000..70feb58 --- /dev/null +++ b/modules/controllers/messages/handlers.go @@ -0,0 +1,38 @@ +package messages + +import ( + "context" + + "github.com/minish144/go-sms-api/gen/pb" + "github.com/minish144/go-sms-api/modules/modem" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" +) + +func Send(ctx context.Context, in *pb.Messages_SendRequest) (*pb.Messages_SendResponse, error) { + if err := in.Validate(); err != nil { + return nil, err + } + + comport := viper.GetString("modem.comport") + baudrate := viper.GetInt("modem.baudrate") + newModem := modem.New(comport, baudrate) + + if err := newModem.Send(in.Message.Phone, in.Message.Message); err != nil { + logrus.WithFields( + logrus.Fields{ + "error": err.Error(), + }, + ).Errorln("failed to send sms") + return nil, err + } + + logrus.WithFields( + logrus.Fields{ + "phone": in.Message.Phone, + "message": in.Message.Message, + }, + ).Infoln("sms was sent successfully") + + return &pb.Messages_SendResponse{}, nil +} diff --git a/modules/server/server.go b/modules/server/server.go index 642403e..0e3a30f 100644 --- a/modules/server/server.go +++ b/modules/server/server.go @@ -88,7 +88,7 @@ func Run() { mux.HandleFunc("/swagger.json", serveSwagger) - logrus.Infoln("serving gRPC-Gateway on " + httpConnectionString) + logrus.Infoln("serving HTTP gRPC-Gateway on " + httpConnectionString) if err := http.ListenAndServe(httpConnectionString, allowCORS(mux)); err != nil { logrus.WithFields(logrus.Fields{ "error": err.Error(), diff --git a/modules/server/structs.go b/modules/server/structs.go index 578ac33..5318390 100644 --- a/modules/server/structs.go +++ b/modules/server/structs.go @@ -4,6 +4,7 @@ import ( "context" "github.com/minish144/go-sms-api/gen/pb" + "github.com/minish144/go-sms-api/modules/controllers/messages" ) type ApiServiceServer struct { @@ -11,5 +12,5 @@ type ApiServiceServer struct { } func (s *ApiServiceServer) SendMessage(ctx context.Context, in *pb.Messages_SendRequest) (*pb.Messages_SendResponse, error) { - return nil, nil + return messages.Send(ctx, in) } diff --git a/proto/api.proto b/proto/api.proto index 564970a..c6c3cf0 100644 --- a/proto/api.proto +++ b/proto/api.proto @@ -18,7 +18,7 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = { email: "varlamow.col@yahoo.com" }; }; - host: "localhost:3010", + host: "localhost:3201", schemes: [HTTP, HTTPS] consumes: "application/json" produces: "application/json" diff --git a/proto/common.proto b/proto/common.proto index 795cd24..f299e29 100644 --- a/proto/common.proto +++ b/proto/common.proto @@ -10,5 +10,5 @@ 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; + string message = 2 [(validate.rules).string = { ignore_empty: true, max_len: 160 }]; } \ No newline at end of file