From 21752c3f0f3e831c6e92b35ddcd666f54c2ae7ac Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Thu, 10 Nov 2022 23:27:55 +0900 Subject: [PATCH 1/4] refactor proxy module --- backend/json2grpc/ats.pb.go | 390 --------------------------- backend/json2grpc/go.mod | 1 + backend/json2grpc/go.sum | 2 + backend/json2grpc/main.go | 88 +++--- backend/json2grpc/pkg/proxy/proxy.go | 67 +++++ 5 files changed, 100 insertions(+), 448 deletions(-) delete mode 100644 backend/json2grpc/ats.pb.go mode change 100755 => 100644 backend/json2grpc/main.go create mode 100644 backend/json2grpc/pkg/proxy/proxy.go diff --git a/backend/json2grpc/ats.pb.go b/backend/json2grpc/ats.pb.go deleted file mode 100644 index 2d007470..00000000 --- a/backend/json2grpc/ats.pb.go +++ /dev/null @@ -1,390 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.28.1 -// protoc v3.20.3 -// source: proto/ats.proto - -package spec - -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 SendStatusRequest_SensorName int32 - -const ( - SendStatusRequest_unknown SendStatusRequest_SensorName = 0 - SendStatusRequest_shinjuku_d1 SendStatusRequest_SensorName = 1 - SendStatusRequest_shinjuku_d2 SendStatusRequest_SensorName = 2 - SendStatusRequest_sakurajosui_d1 SendStatusRequest_SensorName = 11 - SendStatusRequest_sakurajosui_d2 SendStatusRequest_SensorName = 12 - SendStatusRequest_sakurajosui_d3 SendStatusRequest_SensorName = 13 - SendStatusRequest_sakurajosui_d4 SendStatusRequest_SensorName = 14 - SendStatusRequest_chofu_d1 SendStatusRequest_SensorName = 21 - SendStatusRequest_chofu_d2 SendStatusRequest_SensorName = 22 - SendStatusRequest_chofu_d3 SendStatusRequest_SensorName = 23 - SendStatusRequest_chofu_d4 SendStatusRequest_SensorName = 24 - SendStatusRequest_chofu_d5 SendStatusRequest_SensorName = 25 - SendStatusRequest_chofu_d6 SendStatusRequest_SensorName = 26 - SendStatusRequest_hashimoto_d1 SendStatusRequest_SensorName = 31 - SendStatusRequest_hashimoto_d2 SendStatusRequest_SensorName = 32 - SendStatusRequest_hachioji_d1 SendStatusRequest_SensorName = 41 - SendStatusRequest_hachioji_d2 SendStatusRequest_SensorName = 42 - SendStatusRequest_wakabadai_d1 SendStatusRequest_SensorName = 51 - SendStatusRequest_wakabadai_d2 SendStatusRequest_SensorName = 52 -) - -// Enum value maps for SendStatusRequest_SensorName. -var ( - SendStatusRequest_SensorName_name = map[int32]string{ - 0: "unknown", - 1: "shinjuku_d1", - 2: "shinjuku_d2", - 11: "sakurajosui_d1", - 12: "sakurajosui_d2", - 13: "sakurajosui_d3", - 14: "sakurajosui_d4", - 21: "chofu_d1", - 22: "chofu_d2", - 23: "chofu_d3", - 24: "chofu_d4", - 25: "chofu_d5", - 26: "chofu_d6", - 31: "hashimoto_d1", - 32: "hashimoto_d2", - 41: "hachioji_d1", - 42: "hachioji_d2", - 51: "wakabadai_d1", - 52: "wakabadai_d2", - } - SendStatusRequest_SensorName_value = map[string]int32{ - "unknown": 0, - "shinjuku_d1": 1, - "shinjuku_d2": 2, - "sakurajosui_d1": 11, - "sakurajosui_d2": 12, - "sakurajosui_d3": 13, - "sakurajosui_d4": 14, - "chofu_d1": 21, - "chofu_d2": 22, - "chofu_d3": 23, - "chofu_d4": 24, - "chofu_d5": 25, - "chofu_d6": 26, - "hashimoto_d1": 31, - "hashimoto_d2": 32, - "hachioji_d1": 41, - "hachioji_d2": 42, - "wakabadai_d1": 51, - "wakabadai_d2": 52, - } -) - -func (x SendStatusRequest_SensorName) Enum() *SendStatusRequest_SensorName { - p := new(SendStatusRequest_SensorName) - *p = x - return p -} - -func (x SendStatusRequest_SensorName) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (SendStatusRequest_SensorName) Descriptor() protoreflect.EnumDescriptor { - return file_proto_ats_proto_enumTypes[0].Descriptor() -} - -func (SendStatusRequest_SensorName) Type() protoreflect.EnumType { - return &file_proto_ats_proto_enumTypes[0] -} - -func (x SendStatusRequest_SensorName) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use SendStatusRequest_SensorName.Descriptor instead. -func (SendStatusRequest_SensorName) EnumDescriptor() ([]byte, []int) { - return file_proto_ats_proto_rawDescGZIP(), []int{0, 0} -} - -type SendStatusResponse_Response int32 - -const ( - SendStatusResponse_UNKNOWN SendStatusResponse_Response = 0 - SendStatusResponse_SUCCESS SendStatusResponse_Response = 1 - SendStatusResponse_FAILED SendStatusResponse_Response = 2 -) - -// Enum value maps for SendStatusResponse_Response. -var ( - SendStatusResponse_Response_name = map[int32]string{ - 0: "UNKNOWN", - 1: "SUCCESS", - 2: "FAILED", - } - SendStatusResponse_Response_value = map[string]int32{ - "UNKNOWN": 0, - "SUCCESS": 1, - "FAILED": 2, - } -) - -func (x SendStatusResponse_Response) Enum() *SendStatusResponse_Response { - p := new(SendStatusResponse_Response) - *p = x - return p -} - -func (x SendStatusResponse_Response) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (SendStatusResponse_Response) Descriptor() protoreflect.EnumDescriptor { - return file_proto_ats_proto_enumTypes[1].Descriptor() -} - -func (SendStatusResponse_Response) Type() protoreflect.EnumType { - return &file_proto_ats_proto_enumTypes[1] -} - -func (x SendStatusResponse_Response) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use SendStatusResponse_Response.Descriptor instead. -func (SendStatusResponse_Response) EnumDescriptor() ([]byte, []int) { - return file_proto_ats_proto_rawDescGZIP(), []int{1, 0} -} - -type SendStatusRequest struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Sensor SendStatusRequest_SensorName `protobuf:"varint,1,opt,name=sensor,proto3,enum=SendStatusRequest_SensorName" json:"sensor,omitempty"` -} - -func (x *SendStatusRequest) Reset() { - *x = SendStatusRequest{} - if protoimpl.UnsafeEnabled { - mi := &file_proto_ats_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *SendStatusRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*SendStatusRequest) ProtoMessage() {} - -func (x *SendStatusRequest) ProtoReflect() protoreflect.Message { - mi := &file_proto_ats_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 SendStatusRequest.ProtoReflect.Descriptor instead. -func (*SendStatusRequest) Descriptor() ([]byte, []int) { - return file_proto_ats_proto_rawDescGZIP(), []int{0} -} - -func (x *SendStatusRequest) GetSensor() SendStatusRequest_SensorName { - if x != nil { - return x.Sensor - } - return SendStatusRequest_unknown -} - -type SendStatusResponse struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Response SendStatusResponse_Response `protobuf:"varint,1,opt,name=response,proto3,enum=SendStatusResponse_Response" json:"response,omitempty"` -} - -func (x *SendStatusResponse) Reset() { - *x = SendStatusResponse{} - if protoimpl.UnsafeEnabled { - mi := &file_proto_ats_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *SendStatusResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*SendStatusResponse) ProtoMessage() {} - -func (x *SendStatusResponse) ProtoReflect() protoreflect.Message { - mi := &file_proto_ats_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 SendStatusResponse.ProtoReflect.Descriptor instead. -func (*SendStatusResponse) Descriptor() ([]byte, []int) { - return file_proto_ats_proto_rawDescGZIP(), []int{1} -} - -func (x *SendStatusResponse) GetResponse() SendStatusResponse_Response { - if x != nil { - return x.Response - } - return SendStatusResponse_UNKNOWN -} - -var File_proto_ats_proto protoreflect.FileDescriptor - -var file_proto_ats_proto_rawDesc = []byte{ - 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x74, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x22, 0x96, 0x03, 0x0a, 0x11, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x35, 0x0a, 0x06, 0x73, 0x65, 0x6e, 0x73, 0x6f, - 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1d, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, - 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x53, 0x65, 0x6e, 0x73, - 0x6f, 0x72, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x06, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x22, 0xc9, - 0x02, 0x0a, 0x0a, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0b, 0x0a, - 0x07, 0x75, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x73, 0x68, - 0x69, 0x6e, 0x6a, 0x75, 0x6b, 0x75, 0x5f, 0x64, 0x31, 0x10, 0x01, 0x12, 0x0f, 0x0a, 0x0b, 0x73, - 0x68, 0x69, 0x6e, 0x6a, 0x75, 0x6b, 0x75, 0x5f, 0x64, 0x32, 0x10, 0x02, 0x12, 0x12, 0x0a, 0x0e, - 0x73, 0x61, 0x6b, 0x75, 0x72, 0x61, 0x6a, 0x6f, 0x73, 0x75, 0x69, 0x5f, 0x64, 0x31, 0x10, 0x0b, - 0x12, 0x12, 0x0a, 0x0e, 0x73, 0x61, 0x6b, 0x75, 0x72, 0x61, 0x6a, 0x6f, 0x73, 0x75, 0x69, 0x5f, - 0x64, 0x32, 0x10, 0x0c, 0x12, 0x12, 0x0a, 0x0e, 0x73, 0x61, 0x6b, 0x75, 0x72, 0x61, 0x6a, 0x6f, - 0x73, 0x75, 0x69, 0x5f, 0x64, 0x33, 0x10, 0x0d, 0x12, 0x12, 0x0a, 0x0e, 0x73, 0x61, 0x6b, 0x75, - 0x72, 0x61, 0x6a, 0x6f, 0x73, 0x75, 0x69, 0x5f, 0x64, 0x34, 0x10, 0x0e, 0x12, 0x0c, 0x0a, 0x08, - 0x63, 0x68, 0x6f, 0x66, 0x75, 0x5f, 0x64, 0x31, 0x10, 0x15, 0x12, 0x0c, 0x0a, 0x08, 0x63, 0x68, - 0x6f, 0x66, 0x75, 0x5f, 0x64, 0x32, 0x10, 0x16, 0x12, 0x0c, 0x0a, 0x08, 0x63, 0x68, 0x6f, 0x66, - 0x75, 0x5f, 0x64, 0x33, 0x10, 0x17, 0x12, 0x0c, 0x0a, 0x08, 0x63, 0x68, 0x6f, 0x66, 0x75, 0x5f, - 0x64, 0x34, 0x10, 0x18, 0x12, 0x0c, 0x0a, 0x08, 0x63, 0x68, 0x6f, 0x66, 0x75, 0x5f, 0x64, 0x35, - 0x10, 0x19, 0x12, 0x0c, 0x0a, 0x08, 0x63, 0x68, 0x6f, 0x66, 0x75, 0x5f, 0x64, 0x36, 0x10, 0x1a, - 0x12, 0x10, 0x0a, 0x0c, 0x68, 0x61, 0x73, 0x68, 0x69, 0x6d, 0x6f, 0x74, 0x6f, 0x5f, 0x64, 0x31, - 0x10, 0x1f, 0x12, 0x10, 0x0a, 0x0c, 0x68, 0x61, 0x73, 0x68, 0x69, 0x6d, 0x6f, 0x74, 0x6f, 0x5f, - 0x64, 0x32, 0x10, 0x20, 0x12, 0x0f, 0x0a, 0x0b, 0x68, 0x61, 0x63, 0x68, 0x69, 0x6f, 0x6a, 0x69, - 0x5f, 0x64, 0x31, 0x10, 0x29, 0x12, 0x0f, 0x0a, 0x0b, 0x68, 0x61, 0x63, 0x68, 0x69, 0x6f, 0x6a, - 0x69, 0x5f, 0x64, 0x32, 0x10, 0x2a, 0x12, 0x10, 0x0a, 0x0c, 0x77, 0x61, 0x6b, 0x61, 0x62, 0x61, - 0x64, 0x61, 0x69, 0x5f, 0x64, 0x31, 0x10, 0x33, 0x12, 0x10, 0x0a, 0x0c, 0x77, 0x61, 0x6b, 0x61, - 0x62, 0x61, 0x64, 0x61, 0x69, 0x5f, 0x64, 0x32, 0x10, 0x34, 0x22, 0x80, 0x01, 0x0a, 0x12, 0x53, - 0x65, 0x6e, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x38, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x0e, 0x32, 0x1c, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x30, 0x0a, 0x08, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, - 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0b, 0x0a, 0x07, 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x10, - 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x46, 0x41, 0x49, 0x4c, 0x45, 0x44, 0x10, 0x02, 0x32, 0x3e, 0x0a, - 0x03, 0x41, 0x74, 0x73, 0x12, 0x37, 0x0a, 0x0a, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, 0x61, 0x74, - 0x75, 0x73, 0x12, 0x12, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x53, 0x74, 0x61, - 0x74, 0x75, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x08, 0x5a, - 0x06, 0x2e, 0x2f, 0x73, 0x70, 0x65, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_proto_ats_proto_rawDescOnce sync.Once - file_proto_ats_proto_rawDescData = file_proto_ats_proto_rawDesc -) - -func file_proto_ats_proto_rawDescGZIP() []byte { - file_proto_ats_proto_rawDescOnce.Do(func() { - file_proto_ats_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_ats_proto_rawDescData) - }) - return file_proto_ats_proto_rawDescData -} - -var file_proto_ats_proto_enumTypes = make([]protoimpl.EnumInfo, 2) -var file_proto_ats_proto_msgTypes = make([]protoimpl.MessageInfo, 2) -var file_proto_ats_proto_goTypes = []interface{}{ - (SendStatusRequest_SensorName)(0), // 0: SendStatusRequest.SensorName - (SendStatusResponse_Response)(0), // 1: SendStatusResponse.Response - (*SendStatusRequest)(nil), // 2: SendStatusRequest - (*SendStatusResponse)(nil), // 3: SendStatusResponse -} -var file_proto_ats_proto_depIdxs = []int32{ - 0, // 0: SendStatusRequest.sensor:type_name -> SendStatusRequest.SensorName - 1, // 1: SendStatusResponse.response:type_name -> SendStatusResponse.Response - 2, // 2: Ats.SendStatus:input_type -> SendStatusRequest - 3, // 3: Ats.SendStatus:output_type -> SendStatusResponse - 3, // [3:4] is the sub-list for method output_type - 2, // [2:3] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name -} - -func init() { file_proto_ats_proto_init() } -func file_proto_ats_proto_init() { - if File_proto_ats_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_proto_ats_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*SendStatusRequest); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_proto_ats_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*SendStatusResponse); 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_proto_ats_proto_rawDesc, - NumEnums: 2, - NumMessages: 2, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_proto_ats_proto_goTypes, - DependencyIndexes: file_proto_ats_proto_depIdxs, - EnumInfos: file_proto_ats_proto_enumTypes, - MessageInfos: file_proto_ats_proto_msgTypes, - }.Build() - File_proto_ats_proto = out.File - file_proto_ats_proto_rawDesc = nil - file_proto_ats_proto_goTypes = nil - file_proto_ats_proto_depIdxs = nil -} diff --git a/backend/json2grpc/go.mod b/backend/json2grpc/go.mod index 93d5474f..a302959e 100644 --- a/backend/json2grpc/go.mod +++ b/backend/json2grpc/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( github.com/go-playground/validator/v10 v10.11.1 + github.com/kelseyhightower/envconfig v1.4.0 google.golang.org/grpc v1.50.1 google.golang.org/protobuf v1.28.1 ) diff --git a/backend/json2grpc/go.sum b/backend/json2grpc/go.sum index fb555fe1..1a16d11e 100644 --- a/backend/json2grpc/go.sum +++ b/backend/json2grpc/go.sum @@ -35,6 +35,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/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/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= diff --git a/backend/json2grpc/main.go b/backend/json2grpc/main.go old mode 100755 new mode 100644 index 42326734..6128a73b --- a/backend/json2grpc/main.go +++ b/backend/json2grpc/main.go @@ -1,31 +1,31 @@ package main import ( - "encoding/json" - "fmt" + "context" "log" "net/http" + "os" + "os/signal" "time" + "github.com/kelseyhightower/envconfig" + "github.com/ueckoken/backend/json2grpc/pkg/proxy" atspb "github.com/ueckoken/backend/json2grpc/spec" - "github.com/go-playground/validator/v10" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) -type Send struct { - Sensor int32 `validate:"required,min=1,max=52"` +type Config struct { + internalEndpoint string + listenAddr string } func main() { - var client atspb.AtsClient - - // grpcサーバーとのコネクションの確立 - address := "localhost:8888" // grpcサーバーのアドレス + var conf Config + envconfig.MustProcess("", &conf) conn, err := grpc.Dial( - address, - + conf.internalEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock(), ) @@ -34,56 +34,28 @@ func main() { return } defer conn.Close() - client = atspb.NewAtsClient(conn) - - http.HandleFunc("/sensor", func(w http.ResponseWriter, r *http.Request) { - var sensor Send - - w.Header().Set("Content-Type", "application/json") - - switch r.Method { - case http.MethodPost: - if err := json.NewDecoder(r.Body).Decode(&sensor); err != nil { - http.Error(w, fmt.Sprintf(`{"status":"%s"}`, err), http.StatusInternalServerError) - return - } - validate := validator.New() - if err := validate.Struct(sensor); err != nil { - http.Error(w, fmt.Sprintf(`{"status":"%s"}`, err), http.StatusBadRequest) - return - } - - log.Printf("%+v\n", sensor) - - req := &atspb.SendStatusRequest{ - Sensor: atspb.SendStatusRequest_SensorName(sensor.Sensor), - } - - res, err := client.SendStatus(r.Context(), req) - if err != nil { - log.Println(err) - http.Error(w, fmt.Sprintf(`{"status":"%s"}`, err), http.StatusInternalServerError) - return - } - log.Println(res.String()) - - w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte(`{"status": "ok"}`)); err != nil { - log.Println(err) - } - - default: - http.Error(w, `{"status":"permits only POST"}`, http.StatusMethodNotAllowed) - } - }) - + client := atspb.NewAtsClient(conn) + mux := http.NewServeMux() + mux.Handle("/sensor", proxy.NewHandler(client)) srv := &http.Server{ - Addr: ":8080", - Handler: nil, + Addr: conf.listenAddr, + Handler: mux, ReadHeaderTimeout: 3 * time.Second, ReadTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second, } - err = srv.ListenAndServe() - log.Println(err) + go func() { + if err := srv.ListenAndServe(); err != nil { + log.Print(err) + } + }() + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); err != nil { + log.Print(err) + } } diff --git a/backend/json2grpc/pkg/proxy/proxy.go b/backend/json2grpc/pkg/proxy/proxy.go new file mode 100644 index 00000000..11966102 --- /dev/null +++ b/backend/json2grpc/pkg/proxy/proxy.go @@ -0,0 +1,67 @@ +package proxy + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + + "github.com/go-playground/validator/v10" + atspb "github.com/ueckoken/backend/json2grpc/spec" +) + +type Handler struct { + grpcClient atspb.AtsClient +} + +type StatusResponse struct { + Status string `json:"status"` + InternalError error `json:"internalerror,omitempty"` +} + +type Send struct { + Sensor int32 `validate:"required,min=1,max=52"` +} + +func NewHandler(client atspb.AtsClient) *Handler { + return &Handler{grpcClient: client} +} + +func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.Method { + case http.MethodPost: + var sensor Send + if err := json.NewDecoder(r.Body).Decode(&sensor); err != nil { + if err := json.NewEncoder(w).Encode(StatusResponse{Status: "err", InternalError: err}); err != nil { + log.Println(err) + } + w.WriteHeader(http.StatusInternalServerError) + return + } + validate := validator.New() + if err := validate.Struct(sensor); err != nil { + if err := json.NewEncoder(w).Encode(StatusResponse{Status: "err", InternalError: err}); err != nil { + log.Println(err) + } + w.WriteHeader(http.StatusBadRequest) + return + } + log.Printf("%+v\n", sensor) + req := &atspb.SendStatusRequest{ + Sensor: atspb.SendStatusRequest_SensorName(sensor.Sensor), + } + res, err := h.grpcClient.SendStatus(r.Context(), req) + if err != nil { + log.Println(err) + http.Error(w, fmt.Sprintf(`{"status":"%s"}`, err), http.StatusInternalServerError) + return + } + if err := json.NewEncoder(w).Encode(StatusResponse{Status: res.GetResponse().String()}); err != nil { + log.Println(err) + } + w.WriteHeader(http.StatusOK) + default: + http.Error(w, `{"status":"permits only POST"}`, http.StatusMethodNotAllowed) + } +} From 4b86eefd55c28f7c46c0a62c780418a0d67eeb71 Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Thu, 10 Nov 2022 23:36:36 +0900 Subject: [PATCH 2/4] fix test --- .github/workflows/test-external.yml | 189 +++++++++++++++++++- .github/workflows/test-frontend.yml | 29 --- .github/workflows/test-internal.yml | 31 ---- .github/workflows/test-json2grpc.yml | 32 ---- .github/workflows/test-multicaster.yml | 22 --- .github/workflows/test-positioning.yml | 31 ---- .github/workflows/test-sender.yml | 22 --- .github/workflows/test-speed.yml | 32 ---- .github/workflows/test-testing-receiver.yml | 22 --- 9 files changed, 182 insertions(+), 228 deletions(-) delete mode 100644 .github/workflows/test-frontend.yml delete mode 100644 .github/workflows/test-internal.yml delete mode 100644 .github/workflows/test-json2grpc.yml delete mode 100644 .github/workflows/test-multicaster.yml delete mode 100644 .github/workflows/test-positioning.yml delete mode 100644 .github/workflows/test-sender.yml delete mode 100644 .github/workflows/test-speed.yml delete mode 100644 .github/workflows/test-testing-receiver.yml diff --git a/.github/workflows/test-external.yml b/.github/workflows/test-external.yml index 0d0387d2..b55b3a53 100644 --- a/.github/workflows/test-external.yml +++ b/.github/workflows/test-external.yml @@ -1,15 +1,13 @@ -name: External build and test - -defaults: - run: - working-directory: ./backend/external +name: code test on: push jobs: - test: + backend-external: + defaults: + run: + working-directory: ./backend/external runs-on: ubuntu-latest - steps: - name: checkout uses: actions/checkout@v3 @@ -30,3 +28,180 @@ jobs: - name: Run Go Test run: |- make test + backend-internal: + defaults: + run: + working-directory: ./backend/internal + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: ./backend/internal/go.mod + cache: true + cache-dependency-path: ./backend/internal/go.sum + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.50 + working-directory: ./backend/internal/ + - name: Build Go binary + run: |- + make build + - name: Run Go Test + run: |- + make test + + backend-json2grpc: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./backend/json2grpc + + steps: + - name: checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: ./backend/json2grpc/go.mod + cache: true + cache-dependency-path: ./backend/json2grpc/go.sum + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.50 + working-directory: ./backend/json2grpc/ + - name: Build Go binary + run: |- + make build + - name: Run Go Test + run: |- + make test + + backend-multicaster: + defaults: + run: + working-directory: ./backend/multicaster + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version-file: ./backend/multicaster/.python-version + cache: pip + cache-dependency-path: ./backend/multicaster/requirements-dev.txt + - run: pip install -r requirements-dev.txt + - name: Type check + run: mypy . + + backend-positioning: + defaults: + run: + working-directory: ./backend/positioning + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: ./backend/positioning/go.mod + cache: true + cache-dependency-path: ./backend/positioning/go.sum + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.50 + working-directory: ./backend/positioning/ + - name: Build Go binary + run: |- + make build + - name: Run Go Test + run: |- + make test + + frontend-skyway_receiver: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./frontend/skyway_receiver + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version-file: ./frontend/skyway_receiver/.nvmrc + cache: npm + cache-dependency-path: ./frontend/skyway_receiver/package-lock.json + - name: dependency-install + run: npm ci + - name: build + run: npm run build + backend-speed: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./backend/speed + steps: + - name: checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: ./backend/speed/go.mod + cache: true + cache-dependency-path: ./backend/speed/go.sum + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.50 + working-directory: ./backend/speed/ + - name: Build Go binary + run: |- + make buildGoBin + - name: Run Go Test + run: |- + make test + frontend-sender: + defaults: + run: + working-directory: ./frontend/momo_sender + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version-file: ./frontend/momo_sender/.nvmrc + cache: npm + cache-dependency-path: ./frontend/momo_sender/package-lock.json + - name: dependency-install + run: npm ci + - name: build + run: npm run build + + frontend-site: + defaults: + run: + working-directory: ./frontend/site + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ${{ github.workspace }}/frontend/site/.next/cache + key: ${{ runner.os }}-nextjs-${{ hashFiles('./frontend/site/**/yarn.lock') }}-${{ hashFiles('./frontend/site/**.[jt]s', './frontend/site/**.[jt]sx') }} + - uses: actions/setup-node@v3 + with: + node-version-file: ./frontend/site/.nvmrc + cache: yarn + cache-dependency-path: ./frontend/site/yarn.lock + - name: dependency-install + run: yarn install --frozen-lockfile --immutable + - name: lint + run: yarn lint + - name: build + run: yarn build diff --git a/.github/workflows/test-frontend.yml b/.github/workflows/test-frontend.yml deleted file mode 100644 index e1d26c34..00000000 --- a/.github/workflows/test-frontend.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Frontend build and test - -defaults: - run: - working-directory: ./frontend/site - -on: push - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 - with: - path: | - ${{ github.workspace }}/frontend/site/.next/cache - key: ${{ runner.os }}-nextjs-${{ hashFiles('./frontend/site/**/yarn.lock') }}-${{ hashFiles('./frontend/site/**.[jt]s', './frontend/site/**.[jt]sx') }} - - uses: actions/setup-node@v3 - with: - node-version-file: ./frontend/site/.nvmrc - cache: yarn - cache-dependency-path: ./frontend/site/yarn.lock - - name: dependency-install - run: yarn install --frozen-lockfile --immutable - - name: lint - run: yarn lint - - name: build - run: yarn build diff --git a/.github/workflows/test-internal.yml b/.github/workflows/test-internal.yml deleted file mode 100644 index c801056f..00000000 --- a/.github/workflows/test-internal.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Internal build and test - -defaults: - run: - working-directory: ./backend/internal - -on: push - -jobs: - test: - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version-file: ./backend/internal/go.mod - cache: true - cache-dependency-path: ./backend/internal/go.sum - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: v1.50 - working-directory: ./backend/internal/ - - name: Build Go binary - run: |- - make build - - name: Run Go Test - run: |- - make test diff --git a/.github/workflows/test-json2grpc.yml b/.github/workflows/test-json2grpc.yml deleted file mode 100644 index 30d8f94c..00000000 --- a/.github/workflows/test-json2grpc.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: json2grpc build and test - -defaults: - run: - working-directory: ./backend/json2grpc - -on: push - -jobs: - test: - runs-on: ubuntu-latest - - steps: - - name: checkout - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version-file: ./backend/json2grpc/go.mod - cache: true - cache-dependency-path: ./backend/json2grpc/go.sum - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: v1.50 - working-directory: ./backend/json2grpc/ - - name: Build Go binary - run: |- - make build - - name: Run Go Test - run: |- - make test diff --git a/.github/workflows/test-multicaster.yml b/.github/workflows/test-multicaster.yml deleted file mode 100644 index 70473986..00000000 --- a/.github/workflows/test-multicaster.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Multicaster test - -defaults: - run: - working-directory: ./backend/multicaster - -on: push - -jobs: - test: - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - with: - python-version-file: ./backend/multicaster/.python-version - cache: pip - cache-dependency-path: ./backend/multicaster/requirements-dev.txt - - run: pip install -r requirements-dev.txt - - name: Type check - run: mypy . diff --git a/.github/workflows/test-positioning.yml b/.github/workflows/test-positioning.yml deleted file mode 100644 index 558ff502..00000000 --- a/.github/workflows/test-positioning.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: positioning build and test - -defaults: - run: - working-directory: ./backend/positioning - -on: push - -jobs: - test: - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version-file: ./backend/positioning/go.mod - cache: true - cache-dependency-path: ./backend/positioning/go.sum - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: v1.50 - working-directory: ./backend/positioning/ - - name: Build Go binary - run: |- - make build - - name: Run Go Test - run: |- - make test diff --git a/.github/workflows/test-sender.yml b/.github/workflows/test-sender.yml deleted file mode 100644 index d905a044..00000000 --- a/.github/workflows/test-sender.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: WebRTC sender build test - -defaults: - run: - working-directory: ./frontend/momo_sender - -on: push - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version-file: ./frontend/momo_sender/.nvmrc - cache: npm - cache-dependency-path: ./frontend/momo_sender/package-lock.json - - name: dependency-install - run: npm ci - - name: build - run: npm run build diff --git a/.github/workflows/test-speed.yml b/.github/workflows/test-speed.yml deleted file mode 100644 index 616a90d2..00000000 --- a/.github/workflows/test-speed.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: speed build and test - -defaults: - run: - working-directory: ./backend/speed - -on: push - -jobs: - test: - runs-on: ubuntu-latest - - steps: - - name: checkout - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version-file: ./backend/speed/go.mod - cache: true - cache-dependency-path: ./backend/speed/go.sum - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: v1.50 - working-directory: ./backend/speed/ - - name: Build Go binary - run: |- - make buildGoBin - - name: Run Go Test - run: |- - make test diff --git a/.github/workflows/test-testing-receiver.yml b/.github/workflows/test-testing-receiver.yml deleted file mode 100644 index 9e643ba8..00000000 --- a/.github/workflows/test-testing-receiver.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: WebRTC receiver build test - -defaults: - run: - working-directory: ./frontend/skyway_receiver - -on: push - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version-file: ./frontend/skyway_receiver/.nvmrc - cache: npm - cache-dependency-path: ./frontend/skyway_receiver/package-lock.json - - name: dependency-install - run: npm ci - - name: build - run: npm run build From 2e4e5129c6c44e2635e0eb90c8ed1e8584076034 Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Thu, 10 Nov 2022 23:38:42 +0900 Subject: [PATCH 3/4] fix test --- .../{test-external.yml => code-test.yml} | 10 ++++++++++ .github/workflows/validate_github_workflow.yml | 15 --------------- 2 files changed, 10 insertions(+), 15 deletions(-) rename .github/workflows/{test-external.yml => code-test.yml} (94%) delete mode 100644 .github/workflows/validate_github_workflow.yml diff --git a/.github/workflows/test-external.yml b/.github/workflows/code-test.yml similarity index 94% rename from .github/workflows/test-external.yml rename to .github/workflows/code-test.yml index b55b3a53..214dbaeb 100644 --- a/.github/workflows/test-external.yml +++ b/.github/workflows/code-test.yml @@ -3,6 +3,16 @@ name: code test on: push jobs: + actionlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Run actionlint + shell: bash + run: | + bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) + ./actionlint -color -shellcheck= backend-external: defaults: run: diff --git a/.github/workflows/validate_github_workflow.yml b/.github/workflows/validate_github_workflow.yml deleted file mode 100644 index 32f62404..00000000 --- a/.github/workflows/validate_github_workflow.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: validate workflow def file -on: - push: - -jobs: - actionlint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Run actionlint - shell: bash - run: | - bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) - ./actionlint -color -shellcheck= From 2920ea331cec8503fcf92f52cbb6e8dc2f8fabb2 Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Fri, 11 Nov 2022 00:11:22 +0900 Subject: [PATCH 4/4] delete http error --- backend/json2grpc/pkg/proxy/proxy.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/json2grpc/pkg/proxy/proxy.go b/backend/json2grpc/pkg/proxy/proxy.go index 11966102..2d3d1d9a 100644 --- a/backend/json2grpc/pkg/proxy/proxy.go +++ b/backend/json2grpc/pkg/proxy/proxy.go @@ -2,7 +2,6 @@ package proxy import ( "encoding/json" - "fmt" "log" "net/http" @@ -54,7 +53,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { res, err := h.grpcClient.SendStatus(r.Context(), req) if err != nil { log.Println(err) - http.Error(w, fmt.Sprintf(`{"status":"%s"}`, err), http.StatusInternalServerError) + if err := json.NewEncoder(w).Encode(StatusResponse{Status: "err", InternalError: err}); err != nil { + log.Print(err) + } + w.WriteHeader(http.StatusInternalServerError) return } if err := json.NewEncoder(w).Encode(StatusResponse{Status: res.GetResponse().String()}); err != nil { @@ -62,6 +64,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } w.WriteHeader(http.StatusOK) default: - http.Error(w, `{"status":"permits only POST"}`, http.StatusMethodNotAllowed) + if err := json.NewEncoder(w).Encode(StatusResponse{Status: "permits only POST"}); err != nil { + log.Print(err) + } + w.WriteHeader(http.StatusMethodNotAllowed) } }