From 2a525efb4d1cfad6f40893a88674d72a3ec3d0be Mon Sep 17 00:00:00 2001 From: Gary Murphy Date: Wed, 29 Mar 2023 14:27:41 +0100 Subject: [PATCH] Rest service initiated --- src/backend/apiGateway.go | 66 ++++ src/backend/components.pb.go | 480 ------------------------------ src/backend/moduleAPI.go | 86 ------ src/common/proto/components.proto | 9 - 4 files changed, 66 insertions(+), 575 deletions(-) create mode 100644 src/backend/apiGateway.go delete mode 100644 src/backend/components.pb.go delete mode 100644 src/backend/moduleAPI.go diff --git a/src/backend/apiGateway.go b/src/backend/apiGateway.go new file mode 100644 index 0000000..5e34b5c --- /dev/null +++ b/src/backend/apiGateway.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "encoding/json" + "io/ioutil" + "github.com/gorilla/mux" +) + +type Schema struct { + Name string `json:"Name"` + Type int `json:"Type"` +} + +type Container struct { + Name string `json:"Name"` + port string `json:"Port"` +} + +func homePage(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Welcome to the home page!") +} + +func getSchema(w http.ResponseWriter, r *http.Request){ + vars := mux.Vars(r) + container := vars["Name"] + port := vars["Port"] + + requestUrl := fmt.Sprint("http://%s:%s/schema", container, port) + + resp, err := http.Get(requestUrl) + if err != nil { + log.Fatal("Request to %s Failed: %v", container, err) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal("error: %v", err) + } + + var response Schema + err = json.Unmarshal(body, &response) + if err != nil { + log.Fatal("failed to unmarshal: %v", err) + } + + + fmt.Println(response) + +} + +func handleRequests() { + myRouter := mux.NewRouter().StrictSlash(true) + myRouter.HandleFunc("/", homePage) + myRouter.HandleFunc("/{container}/schema", getSchema) + + log.Fatal(http.ListenAndServe(":10000", myRouter)) + +} + +func main() { + handleRequests() +} \ No newline at end of file diff --git a/src/backend/components.pb.go b/src/backend/components.pb.go deleted file mode 100644 index c790f84..0000000 --- a/src/backend/components.pb.go +++ /dev/null @@ -1,480 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.30.0 -// protoc v3.21.12 -// source: components.proto - -package main - -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 ComponentType int32 - -const ( - ComponentType_Text ComponentType = 0 - ComponentType_Chart ComponentType = 1 -) - -// Enum value maps for ComponentType. -var ( - ComponentType_name = map[int32]string{ - 0: "Text", - 1: "Chart", - } - ComponentType_value = map[string]int32{ - "Text": 0, - "Chart": 1, - } -) - -func (x ComponentType) Enum() *ComponentType { - p := new(ComponentType) - *p = x - return p -} - -func (x ComponentType) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (ComponentType) Descriptor() protoreflect.EnumDescriptor { - return file_components_proto_enumTypes[0].Descriptor() -} - -func (ComponentType) Type() protoreflect.EnumType { - return &file_components_proto_enumTypes[0] -} - -func (x ComponentType) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use ComponentType.Descriptor instead. -func (ComponentType) EnumDescriptor() ([]byte, []int) { - return file_components_proto_rawDescGZIP(), []int{0} -} - -type Component struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - Type ComponentType `protobuf:"varint,3,opt,name=type,proto3,enum=panoptes.ComponentType" json:"type,omitempty"` -} - -func (x *Component) Reset() { - *x = Component{} - if protoimpl.UnsafeEnabled { - mi := &file_components_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Component) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Component) ProtoMessage() {} - -func (x *Component) ProtoReflect() protoreflect.Message { - mi := &file_components_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 Component.ProtoReflect.Descriptor instead. -func (*Component) Descriptor() ([]byte, []int) { - return file_components_proto_rawDescGZIP(), []int{0} -} - -func (x *Component) GetId() string { - if x != nil { - return x.Id - } - return "" -} - -func (x *Component) GetName() string { - if x != nil { - return x.Name - } - return "" -} - -func (x *Component) GetType() ComponentType { - if x != nil { - return x.Type - } - return ComponentType_Text -} - -type Schema struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Components []*Component `protobuf:"bytes,1,rep,name=components,proto3" json:"components,omitempty"` -} - -func (x *Schema) Reset() { - *x = Schema{} - if protoimpl.UnsafeEnabled { - mi := &file_components_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Schema) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Schema) ProtoMessage() {} - -func (x *Schema) ProtoReflect() protoreflect.Message { - mi := &file_components_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 Schema.ProtoReflect.Descriptor instead. -func (*Schema) Descriptor() ([]byte, []int) { - return file_components_proto_rawDescGZIP(), []int{1} -} - -func (x *Schema) GetComponents() []*Component { - if x != nil { - return x.Components - } - return nil -} - -type TextComponent struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - Text string `protobuf:"bytes,3,opt,name=text,proto3" json:"text,omitempty"` - Websocket bool `protobuf:"varint,4,opt,name=websocket,proto3" json:"websocket,omitempty"` -} - -func (x *TextComponent) Reset() { - *x = TextComponent{} - if protoimpl.UnsafeEnabled { - mi := &file_components_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *TextComponent) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*TextComponent) ProtoMessage() {} - -func (x *TextComponent) ProtoReflect() protoreflect.Message { - mi := &file_components_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 TextComponent.ProtoReflect.Descriptor instead. -func (*TextComponent) Descriptor() ([]byte, []int) { - return file_components_proto_rawDescGZIP(), []int{2} -} - -func (x *TextComponent) GetId() string { - if x != nil { - return x.Id - } - return "" -} - -func (x *TextComponent) GetName() string { - if x != nil { - return x.Name - } - return "" -} - -func (x *TextComponent) GetText() string { - if x != nil { - return x.Text - } - return "" -} - -func (x *TextComponent) GetWebsocket() bool { - if x != nil { - return x.Websocket - } - return false -} - -type ChartComponent struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"` - Labels []string `protobuf:"bytes,4,rep,name=labels,proto3" json:"labels,omitempty"` - Values []int32 `protobuf:"varint,5,rep,packed,name=values,proto3" json:"values,omitempty"` - Websocket bool `protobuf:"varint,6,opt,name=websocket,proto3" json:"websocket,omitempty"` -} - -func (x *ChartComponent) Reset() { - *x = ChartComponent{} - if protoimpl.UnsafeEnabled { - mi := &file_components_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *ChartComponent) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ChartComponent) ProtoMessage() {} - -func (x *ChartComponent) ProtoReflect() protoreflect.Message { - mi := &file_components_proto_msgTypes[3] - 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 ChartComponent.ProtoReflect.Descriptor instead. -func (*ChartComponent) Descriptor() ([]byte, []int) { - return file_components_proto_rawDescGZIP(), []int{3} -} - -func (x *ChartComponent) GetId() int32 { - if x != nil { - return x.Id - } - return 0 -} - -func (x *ChartComponent) GetName() string { - if x != nil { - return x.Name - } - return "" -} - -func (x *ChartComponent) GetType() string { - if x != nil { - return x.Type - } - return "" -} - -func (x *ChartComponent) GetLabels() []string { - if x != nil { - return x.Labels - } - return nil -} - -func (x *ChartComponent) GetValues() []int32 { - if x != nil { - return x.Values - } - return nil -} - -func (x *ChartComponent) GetWebsocket() bool { - if x != nil { - return x.Websocket - } - return false -} - -var File_components_proto protoreflect.FileDescriptor - -var file_components_proto_rawDesc = []byte{ - 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x73, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x08, 0x70, 0x61, 0x6e, 0x6f, 0x70, 0x74, 0x65, 0x73, 0x22, 0x5c, 0x0a, 0x09, - 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, - 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x2b, 0x0a, - 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x17, 0x2e, 0x70, 0x61, - 0x6e, 0x6f, 0x70, 0x74, 0x65, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, - 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x22, 0x3d, 0x0a, 0x06, 0x53, 0x63, - 0x68, 0x65, 0x6d, 0x61, 0x12, 0x33, 0x0a, 0x0a, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, - 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x70, 0x61, 0x6e, 0x6f, 0x70, - 0x74, 0x65, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x52, 0x0a, 0x63, - 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x65, 0x0a, 0x0d, 0x54, 0x65, 0x78, - 0x74, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, - 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, - 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, - 0x78, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x18, - 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, - 0x22, 0x96, 0x01, 0x0a, 0x0e, 0x43, 0x68, 0x61, 0x72, 0x74, 0x43, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, - 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, - 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6c, - 0x61, 0x62, 0x65, 0x6c, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x6c, 0x61, 0x62, - 0x65, 0x6c, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x05, 0x20, - 0x03, 0x28, 0x05, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x77, - 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, - 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2a, 0x24, 0x0a, 0x0d, 0x43, 0x6f, 0x6d, - 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x54, 0x65, - 0x78, 0x74, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x43, 0x68, 0x61, 0x72, 0x74, 0x10, 0x01, 0x42, - 0x20, 0x5a, 0x1e, 0x70, 0x61, 0x6e, 0x6f, 0x70, 0x74, 0x65, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, - 0x73, 0x72, 0x63, 0x2f, 0x70, 0x61, 0x6e, 0x6f, 0x70, 0x74, 0x65, 0x73, 0x2f, 0x6d, 0x61, 0x69, - 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_components_proto_rawDescOnce sync.Once - file_components_proto_rawDescData = file_components_proto_rawDesc -) - -func file_components_proto_rawDescGZIP() []byte { - file_components_proto_rawDescOnce.Do(func() { - file_components_proto_rawDescData = protoimpl.X.CompressGZIP(file_components_proto_rawDescData) - }) - return file_components_proto_rawDescData -} - -var file_components_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_components_proto_msgTypes = make([]protoimpl.MessageInfo, 4) -var file_components_proto_goTypes = []interface{}{ - (ComponentType)(0), // 0: panoptes.ComponentType - (*Component)(nil), // 1: panoptes.Component - (*Schema)(nil), // 2: panoptes.Schema - (*TextComponent)(nil), // 3: panoptes.TextComponent - (*ChartComponent)(nil), // 4: panoptes.ChartComponent -} -var file_components_proto_depIdxs = []int32{ - 0, // 0: panoptes.Component.type:type_name -> panoptes.ComponentType - 1, // 1: panoptes.Schema.components:type_name -> panoptes.Component - 2, // [2:2] is the sub-list for method output_type - 2, // [2:2] 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_components_proto_init() } -func file_components_proto_init() { - if File_components_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_components_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Component); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_components_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Schema); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_components_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*TextComponent); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_components_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ChartComponent); 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_components_proto_rawDesc, - NumEnums: 1, - NumMessages: 4, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_components_proto_goTypes, - DependencyIndexes: file_components_proto_depIdxs, - EnumInfos: file_components_proto_enumTypes, - MessageInfos: file_components_proto_msgTypes, - }.Build() - File_components_proto = out.File - file_components_proto_rawDesc = nil - file_components_proto_goTypes = nil - file_components_proto_depIdxs = nil -} diff --git a/src/backend/moduleAPI.go b/src/backend/moduleAPI.go deleted file mode 100644 index 2a73dcc..0000000 --- a/src/backend/moduleAPI.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "log" - "net/http" - - "github.com/golang/protobuf/proto" - "github.com/gorilla/mux" -) - - -func getSchema(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - port := vars["port"] - - //make get request to specified container - url := fmt.Sprintf("http://localhost:%s", port) - resp, err := http.Get(url) - if err != nil { - log.Fatal("Error: ", err) - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - schema := &Schema{} - err = proto.Unmarshal(body, schema) - if err != nil { - log.Fatal(err) - } - - fmt.Println(schema) -} - -func getStats(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - port := vars["port"] - - - //make get request to specified container - url := fmt.Sprintf("http://localhost:%s", port) - resp, err := http.Get(url) - if err != nil { - log.Fatal("Error: ", err) - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - stats := &ChartComponent{} - err = proto.Unmarshal(body, stats) - if err != nil { - log.Fatal(err) - } - - fmt.Println(stats) - - -} - - - -func handleRequests() { - myRouter := mux.NewRouter().StrictSlash(true) - - myRouter.HandleFunc("/schema/{container}", getSchema) - myRouter.HandleFunc("/stats/{port}", getStats) - - log.Fatal(http.ListenAndServe(":3000", myRouter)) -} - - -func main() { - fmt.Println("Module API...") - handleRequests() -} \ No newline at end of file diff --git a/src/common/proto/components.proto b/src/common/proto/components.proto index a2c4d43..be5dd13 100644 --- a/src/common/proto/components.proto +++ b/src/common/proto/components.proto @@ -3,15 +3,6 @@ package panoptes; option go_package = "panoptes.com/src/panoptes/main"; - -service SchemaService { - rpc getSchema (Container) returns (Schema) {} -} - -service StatsService { - rpc getStats (Container) returns (ChartComponent) {} -} - enum ComponentType { Text = 0; Chart = 1;