From b00b047de939ec23d44259f99815f52fbd5c157d Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Wed, 3 Apr 2024 12:29:15 +1100 Subject: [PATCH] Updated handler & included test --- TESTING_dockercompose.md | 5 ++++- main.go | 20 ++++++++++++-------- opensearch.go | 12 ++++++------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/TESTING_dockercompose.md b/TESTING_dockercompose.md index 047e7a3..f39dbf6 100644 --- a/TESTING_dockercompose.md +++ b/TESTING_dockercompose.md @@ -57,7 +57,7 @@ docker compose exec -T commons sh -c "curl -kL http://go-web:3000/opensearch?ser docker compose exec -T commons sh -c "curl -kL http://go-web:3000/opensearch?service=opensearch-2" | grep "LAGOON_TEST_VAR=internal-services-test" # mongo-4 should be able to read/write data -docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mongo?service=mongo-4" | grep "SERVICE_HOST=" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mongo?service=mongo-4" | grep "SERVICE_HOST=mongo-4" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mongo?service=mongo-4" | grep "LAGOON_TEST_VAR=internal-services-test" # redis-6 should be able to read/write data @@ -75,6 +75,9 @@ docker compose exec -T commons sh -c "curl -kL http://go-web:3000/solr?service=s # persistent storage should be able to read/write data docker compose exec -T commons sh -c "curl -kL http://go-web:3000/storage?path=/app/files" | grep "STORAGE_PATH=/app/files/storage.txt" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/storage?path=/app/files" | grep "LAGOON_TEST_VAR=internal-services-test" + +# Incorrect service should be caught & error output +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=incorrect-service" | grep "mariadb is not a compatible driver with service: incorrect-service" ``` Destroy tests diff --git a/main.go b/main.go index 558fe84..5d3a8d6 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "strings" - "time" ) type funcType func() map[string]string @@ -24,16 +23,21 @@ func main() { r.HandleFunc("/", handleReq) http.Handle("/", r) - log.Fatal(http.ListenAndServe(":3000", timeoutHandler(r))) + log.Fatal(http.ListenAndServe(":3000", handler(r))) } -func timeoutHandler(m *mux.Router) http.HandlerFunc { +func handler(m *mux.Router) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - driver := strings.ReplaceAll(r.URL.Path, "/", "") - service := r.URL.Query().Get("service") - incompatibleError := fmt.Sprintf("%s is not a compatible driver with service: %s", driver, service) - timeoutHandler := http.TimeoutHandler(m, 3*time.Second, incompatibleError) - timeoutHandler.ServeHTTP(w, r) + defer func() { + driver := strings.ReplaceAll(r.URL.Path, "/", "") + service := r.URL.Query().Get("service") + incompatibleError := fmt.Sprintf("%s is not a compatible driver with service: %s", driver, service) + if err := recover(); err != nil { + http.Error(w, incompatibleError, http.StatusInternalServerError) + } + }() + handler := http.Handler(m) + handler.ServeHTTP(w, r) } } diff --git a/opensearch.go b/opensearch.go index 33c7376..2ff3661 100644 --- a/opensearch.go +++ b/opensearch.go @@ -57,13 +57,13 @@ func cleanOpensearchOutput(sr *opensearchapi.SearchResp) string { func createOpensearchIndexDocument(client *opensearchapi.Client) { settings := strings.NewReader(`{ - 'settings': { - 'index': { - 'number_of_shards': 1, - 'number_of_replicas': 0 - } + "settings": { + "index": { + "number_of_shards": 1, + "number_of_replicas": 0 } - }`) + } + }`) _, err := client.Indices.Create( ctx,