diff --git a/hw12_13_14_15_calendar/cmd/calendar/main.go b/hw12_13_14_15_calendar/cmd/calendar/main.go index 69aa273..27b1e5c 100644 --- a/hw12_13_14_15_calendar/cmd/calendar/main.go +++ b/hw12_13_14_15_calendar/cmd/calendar/main.go @@ -2,8 +2,6 @@ package main import ( "flag" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/grpc" - googrpc "google.golang.org/grpc" oslog "log" "net" "os" @@ -12,9 +10,11 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/app" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/config" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/grpc" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/logger" internalhttp "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/server/http" store "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage" + googrpc "google.golang.org/grpc" ) var configFile string @@ -52,8 +52,11 @@ func main() { log.Fatalf("failed to listen %v", err) } serverGRPC := googrpc.NewServer() - grpc.RegisterGrpcServer(serverGRPC, grpc.Service{*calendar}) - serverGRPC.Serve(listnGrpc) + grpc.RegisterGrpcServer(serverGRPC, grpc.Service{App: *calendar}) + if err := serverGRPC.Serve(listnGrpc); err != nil { + log.Errorf("failed to start grpc server: " + err.Error()) + os.Exit(1) + } go func() { signals := make(chan os.Signal, 1) diff --git a/hw12_13_14_15_calendar/grpc/grpc.proto b/hw12_13_14_15_calendar/grpc/grpc.proto index 06fed34..cafcb6b 100644 --- a/hw12_13_14_15_calendar/grpc/grpc.proto +++ b/hw12_13_14_15_calendar/grpc/grpc.proto @@ -7,12 +7,12 @@ import "google/protobuf/empty.proto"; import "google/api/annotations.proto"; message Event { - string ID = 1; + int64 ID = 1; string Title = 2; google.protobuf.Timestamp Date = 3; google.protobuf.Duration Latency = 4; string Note = 5; - string UserID = 6; + int64 UserID = 6; google.protobuf.Duration NotifyTime = 7; } @@ -21,11 +21,11 @@ message EventList { } message EventID { - string ID = 1; + int64 ID = 1; } message EventWthID { - string ID = 1; + int64 ID = 1; Event Event = 2; } diff --git a/hw12_13_14_15_calendar/internal/grpc/converters.go b/hw12_13_14_15_calendar/internal/grpc/converters.go new file mode 100644 index 0000000..0a12180 --- /dev/null +++ b/hw12_13_14_15_calendar/internal/grpc/converters.go @@ -0,0 +1,40 @@ +package grpc + +import ( + "github.com/golang/protobuf/ptypes" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage/event" +) + +func pbevent2event(pbe *Event) (res event.Event, err error) { + res = event.Event{Title: pbe.Title, Note: pbe.Note, UserID: pbe.UserID} + res.Date, err = ptypes.Timestamp(pbe.Date) + if err != nil { + return event.Event{}, err + } + res.Latency, err = ptypes.Duration(pbe.Latency) + if err != nil { + return event.Event{}, err + } + res.NotifyTime, err = ptypes.Duration(pbe.NotifyTime) + if err != nil { + return event.Event{}, err + } + return res, nil +} + +func pbeventWitID2eventAndID(pbe *EventWthID) (id event.ID, evt event.Event, err error) { + evt = event.Event{Title: pbe.Event.Title, Note: pbe.Event.Note, UserID: pbe.Event.UserID} + evt.Date, err = ptypes.Timestamp(pbe.Event.Date) + if err != nil { + return 0, event.Event{}, err + } + evt.Latency, err = ptypes.Duration(pbe.Event.Latency) + if err != nil { + return 0, event.Event{}, err + } + evt.NotifyTime, err = ptypes.Duration(pbe.Event.NotifyTime) + if err != nil { + return 0, event.Event{}, err + } + return event.ID(pbe.ID), evt, nil +} diff --git a/hw12_13_14_15_calendar/internal/grpc/grpc.pb.go b/hw12_13_14_15_calendar/internal/grpc/grpc.pb.go index 8a49203..967c203 100644 --- a/hw12_13_14_15_calendar/internal/grpc/grpc.pb.go +++ b/hw12_13_14_15_calendar/internal/grpc/grpc.pb.go @@ -57,12 +57,12 @@ func (QueryRange) EnumDescriptor() ([]byte, []int) { } type Event struct { - ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"` + ID int64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"` Title string `protobuf:"bytes,2,opt,name=Title,proto3" json:"Title,omitempty"` Date *timestamp.Timestamp `protobuf:"bytes,3,opt,name=Date,proto3" json:"Date,omitempty"` Latency *duration.Duration `protobuf:"bytes,4,opt,name=Latency,proto3" json:"Latency,omitempty"` Note string `protobuf:"bytes,5,opt,name=Note,proto3" json:"Note,omitempty"` - UserID string `protobuf:"bytes,6,opt,name=UserID,proto3" json:"UserID,omitempty"` + UserID int64 `protobuf:"varint,6,opt,name=UserID,proto3" json:"UserID,omitempty"` NotifyTime *duration.Duration `protobuf:"bytes,7,opt,name=NotifyTime,proto3" json:"NotifyTime,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -94,11 +94,11 @@ func (m *Event) XXX_DiscardUnknown() { var xxx_messageInfo_Event proto.InternalMessageInfo -func (m *Event) GetID() string { +func (m *Event) GetID() int64 { if m != nil { return m.ID } - return "" + return 0 } func (m *Event) GetTitle() string { @@ -129,11 +129,11 @@ func (m *Event) GetNote() string { return "" } -func (m *Event) GetUserID() string { +func (m *Event) GetUserID() int64 { if m != nil { return m.UserID } - return "" + return 0 } func (m *Event) GetNotifyTime() *duration.Duration { @@ -183,7 +183,7 @@ func (m *EventList) GetEvents() []*Event { } type EventID struct { - ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"` + ID int64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -214,15 +214,15 @@ func (m *EventID) XXX_DiscardUnknown() { var xxx_messageInfo_EventID proto.InternalMessageInfo -func (m *EventID) GetID() string { +func (m *EventID) GetID() int64 { if m != nil { return m.ID } - return "" + return 0 } type EventWthID struct { - ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"` + ID int64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"` Event *Event `protobuf:"bytes,2,opt,name=Event,proto3" json:"Event,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -254,11 +254,11 @@ func (m *EventWthID) XXX_DiscardUnknown() { var xxx_messageInfo_EventWthID proto.InternalMessageInfo -func (m *EventWthID) GetID() string { +func (m *EventWthID) GetID() int64 { if m != nil { return m.ID } - return "" + return 0 } func (m *EventWthID) GetEvent() *Event { @@ -327,7 +327,7 @@ func init() { func init() { proto.RegisterFile("grpc.proto", fileDescriptor_bedfbfc9b54e5600) } var fileDescriptor_bedfbfc9b54e5600 = []byte{ - // 516 bytes of a gzipped FileDescriptorProto + // 519 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0xcd, 0x6e, 0xd3, 0x4c, 0x14, 0xad, 0x1d, 0xff, 0x24, 0x37, 0xfa, 0xf2, 0x85, 0x2b, 0x54, 0x5c, 0x53, 0x95, 0xe0, 0x0d, 0x51, 0x90, 0xc6, 0x52, 0xba, 0x40, 0xcd, 0x06, 0x01, 0x13, 0x81, 0x45, 0x09, 0xc2, 0x4a, 0x55, @@ -338,29 +338,29 @@ var fileDescriptor_bedfbfc9b54e5600 = []byte{ 0x92, 0x29, 0xc2, 0xc9, 0x2e, 0xe1, 0x76, 0x96, 0x47, 0x32, 0x4e, 0x13, 0x85, 0x3f, 0xde, 0xc5, 0xc5, 0x24, 0x93, 0x73, 0x05, 0x1e, 0x2b, 0x30, 0xca, 0x62, 0x3f, 0x4a, 0x92, 0x54, 0x92, 0x72, 0x5a, 0xa2, 0xde, 0x2f, 0x0d, 0xcc, 0xfe, 0x17, 0x91, 0x48, 0x6c, 0x80, 0x1e, 0x70, 0x47, 0x6b, - 0x69, 0xed, 0x5a, 0xa8, 0x07,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0xf9, 0xe1, 0xe3, 0x60, 0xf8, 0xae, 0xa9, 0x77, 0xbf, 0xeb, 0x60, + 0x14, 0x2f, 0x12, 0x9f, 0x81, 0xf5, 0x26, 0x17, 0x85, 0x83, 0x1a, 0xd4, 0xad, 0x32, 0xb5, 0xb7, + 0x57, 0xff, 0xfa, 0xe3, 0xe7, 0x37, 0xdd, 0xf4, 0x0e, 0x7a, 0x5a, 0x07, 0x5f, 0x82, 0x75, 0x91, + 0xdd, 0x16, 0xc4, 0x3a, 0xdb, 0x6c, 0xef, 0x1e, 0xee, 0x4d, 0xde, 0x2f, 0xde, 0xa2, 0xd7, 0x24, + 0x2d, 0xb8, 0xa6, 0xbf, 0x08, 0xf8, 0xb2, 0x38, 0xe0, 0x0c, 0x2c, 0x2e, 0xc6, 0x42, 0x0a, 0x5c, + 0x3b, 0xdc, 0xab, 0xfe, 0x8f, 0xd4, 0x76, 0xa7, 0x54, 0x63, 0x0f, 0x0c, 0xba, 0xb3, 0x7b, 0xe8, + 0x2e, 0xb0, 0xf5, 0xbd, 0x7a, 0x0d, 0x92, 0x56, 0xd1, 0xf2, 0x05, 0x3d, 0x73, 0x06, 0xf6, 0x5b, + 0x21, 0x5f, 0xcf, 0x03, 0xbe, 0xe5, 0xbb, 0x2d, 0x50, 0x5e, 0xa8, 0xbc, 0x5e, 0x40, 0x8d, 0xf8, + 0x94, 0xba, 0xc9, 0x8a, 0xcf, 0x1f, 0xf4, 0x47, 0x44, 0x7f, 0x80, 0xff, 0xfb, 0x8b, 0x02, 0x5b, + 0xfa, 0x0b, 0xca, 0x7b, 0x79, 0x6d, 0xd1, 0x50, 0xa7, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xa5, + 0xa3, 0x2a, 0x4c, 0xea, 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/hw12_13_14_15_calendar/internal/grpc/grpc.pb.gw.go b/hw12_13_14_15_calendar/internal/grpc/grpc.pb.gw.go index e7a7d31..c809891 100644 --- a/hw12_13_14_15_calendar/internal/grpc/grpc.pb.gw.go +++ b/hw12_13_14_15_calendar/internal/grpc/grpc.pb.gw.go @@ -53,7 +53,7 @@ func request_Grpc_Update_0(ctx context.Context, marshaler runtime.Marshaler, cli return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "ID") } - protoReq.ID, err = runtime.String(val) + protoReq.ID, err = runtime.Int64(val) if err != nil { return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "ID", err) @@ -80,7 +80,7 @@ func request_Grpc_Delete_0(ctx context.Context, marshaler runtime.Marshaler, cli return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "ID") } - protoReq.ID, err = runtime.String(val) + protoReq.ID, err = runtime.Int64(val) if err != nil { return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "ID", err) @@ -116,7 +116,7 @@ func request_Grpc_GetByID_0(ctx context.Context, marshaler runtime.Marshaler, cl return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "ID") } - protoReq.ID, err = runtime.String(val) + protoReq.ID, err = runtime.Int64(val) if err != nil { return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "ID", err) diff --git a/hw12_13_14_15_calendar/internal/grpc/handlers.go b/hw12_13_14_15_calendar/internal/grpc/handlers.go index 1a3f7bb..fac483a 100644 --- a/hw12_13_14_15_calendar/internal/grpc/handlers.go +++ b/hw12_13_14_15_calendar/internal/grpc/handlers.go @@ -13,15 +13,24 @@ type Service struct { func (s Service) Create(ctx context.Context, e *Event) (*EventID, error) { var res EventID - //var tmp = event.Event{e.Title, e.Date.(time.Time), e.Latency, e.Note, e.UserID, e.NotifyTime} - //t, err := s.App.Storage.Create(tmp) - //if err != nil { return nil, err } - //res.ID = string(t) + ce, err := pbevent2event(e) + if err != nil { + return nil, err + } + t, err := s.App.Storage.Create(ce) + if err != nil { + return nil, err + } + res.ID = int64(t) return &res, nil } func (s Service) Update(ctx context.Context, e *EventWthID) (*empty.Empty, error) { - return nil, nil + cid, ce, err := pbeventWitID2eventAndID(e) + if err != nil { + return nil, err + } + return nil, s.App.Storage.Update(cid, ce) } func (s Service) Delete(ctx context.Context, e *EventID) (*empty.Empty, error) {