From 891e56511704d82bf1c3dc86c0a60586020284a2 Mon Sep 17 00:00:00 2001 From: Gary Murphy Date: Wed, 29 Mar 2023 15:30:43 +0100 Subject: [PATCH] api gateway basic functionality --- src/backend/apiGateway.go | 48 +++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/backend/apiGateway.go b/src/backend/apiGateway.go index 5e34b5c..94089a6 100644 --- a/src/backend/apiGateway.go +++ b/src/backend/apiGateway.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "net/http" - "encoding/json" "io/ioutil" "github.com/gorilla/mux" ) @@ -15,40 +14,40 @@ type Schema struct { } type Container struct { + Id string `json:"Id` Name string `json:"Name"` port string `json:"Port"` } +var Containers []Container + 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"] + key := vars["container"] - requestUrl := fmt.Sprint("http://%s:%s/schema", container, port) + for _, container := range Containers { + if container.Id == key { + requestURL := fmt.Sprintf("http://%s:%s/schema", container.Name, container.port) + fmt.Fprintf(w, requestURL) + resp, err := http.Get(requestURL) + if err != nil { + log.Fatal("Request to container failed: %v", err) + } - 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("Failed to print Body: %v", err) + } + + fmt.Fprintf(w,string(body)) + } } - 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) } @@ -62,5 +61,10 @@ func handleRequests() { } func main() { + + Containers = []Container{ + Container{Id: "1", Name: "localhost", port: "3000"}, + } + handleRequests() } \ No newline at end of file