Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Updates the router handler to handle panic on incorrect service #39

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion TESTING_dockercompose.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net/http"
"strings"
"time"
)

type funcType func() map[string]string
Expand All @@ -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)
}
}

Expand Down
12 changes: 6 additions & 6 deletions opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down