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: Updated URL format to improve readability #16

Merged
merged 8 commits into from
Nov 6, 2023
40 changes: 20 additions & 20 deletions TESTING_dockercompose.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,44 @@ docker run --rm --net internal-services-test_default jwilder/dockerize dockerize
docker-compose exec -T commons sh -c "cat /etc/os-release" | grep "Alpine Linux"

# mariadb-10-4 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb-10-4" | grep "SERVICE_HOST=10.4"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb-10-4" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10.4" | grep "SERVICE_HOST=10.4"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10.4" | grep "LAGOON_TEST_VAR=internal-services-test"

# mariadb-10-6 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb-10-6" | grep "SERVICE_HOST=10.6"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb-10-6" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10.6" | grep "SERVICE_HOST=10.6"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10.6" | grep "LAGOON_TEST_VAR=internal-services-test"

# postgres-11 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres-11" | grep "SERVICE_HOST=PostgreSQL 11"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres-11" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-11" | grep "SERVICE_HOST=PostgreSQL 11"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-11" | grep "LAGOON_TEST_VAR=internal-services-test"

# postgres-15 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres-15" | grep "SERVICE_HOST=PostgreSQL 15"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres-15" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-15" | grep "SERVICE_HOST=PostgreSQL 15"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-15" | grep "LAGOON_TEST_VAR=internal-services-test"

# opensearch-2 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/opensearch-2" | grep "SERVICE_HOST=opensearch-2"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/opensearch-2" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/opensearch?service=opensearch-2" | grep "SERVICE_HOST=opensearch-2"
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-4" | grep "SERVICE_HOST="
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/mongo-4" | grep "LAGOON_TEST_VAR=internal-services-test"
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 "LAGOON_TEST_VAR=internal-services-test"

# redis-5 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis-5" | grep "SERVICE_HOST=redis-5"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis-5" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis?service=redis-5" | grep "SERVICE_HOST=redis-5"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis?service=redis-5" | grep "LAGOON_TEST_VAR=internal-services-test"

# redis-6 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis-6" | grep "SERVICE_HOST=redis-6"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis-6" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis?service=redis-6" | grep "SERVICE_HOST=redis-6"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/redis?service=redis-6" | grep "LAGOON_TEST_VAR=internal-services-test"

# solr-7 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr-7" | grep "SERVICE_HOST=solr-7"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr-7" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr?service=solr-7" | grep "SERVICE_HOST=solr-7"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr?service=solr-7" | grep "LAGOON_TEST_VAR=internal-services-test"

# solr-8 should be able to read/write data
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr-8" | grep "SERVICE_HOST=solr-8"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr-8" | grep "LAGOON_TEST_VAR=internal-services-test"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr?service=solr-8" | grep "SERVICE_HOST=solr-8"
docker-compose exec -T commons sh -c "curl -kL http://go-web:3000/solr?service=solr-8" | grep "LAGOON_TEST_VAR=internal-services-test"
```

Destroy tests
Expand Down
36 changes: 25 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"bytes"
"errors"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"

"github.com/gorilla/mux"
Expand All @@ -19,12 +21,12 @@ type funcType func() map[string]string

func main() {
r := mux.NewRouter()
r.HandleFunc("/{mariadb:mariadb-.*}", mariadbHandler)
r.HandleFunc("/{postgres:postgres-.*}", postgresHandler)
r.HandleFunc("/{redis:redis-.*}", redisHandler)
r.HandleFunc("/{solr:solr-.*}", solrHandler)
r.HandleFunc("/{mongo:mongo-.*}", mongoHandler)
r.HandleFunc("/{opensearch:opensearch-.*}", opensearchHandler)
r.HandleFunc("/mariadb", mariadbHandler)
r.HandleFunc("/postgres", postgresHandler)
r.HandleFunc("/redis", redisHandler)
r.HandleFunc("/solr", solrHandler)
r.HandleFunc("/mongo", mongoHandler)
r.HandleFunc("/opensearch", opensearchHandler)
r.HandleFunc("/", handleReq)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":3000", nil))
Expand Down Expand Up @@ -66,11 +68,23 @@ func cleanRoute(basePath string) (string, string) {
return localService, lagoonService
}

func verifyDriverService(r *http.Request) (string, error) {
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
driver := strings.ReplaceAll(r.URL.Path, "/", "")
serviceVersion := r.URL.Query().Get("service")
regexp := regexp.MustCompile(`(\w*)-`)
service := regexp.FindStringSubmatch(serviceVersion)
if driver != service[1] {
incompatibleError := fmt.Sprintf("%s is not a compatible driver with service: %s", driver, service[1])
return "", errors.New(incompatibleError)
}
return serviceVersion, nil
}

// getEnv get key environment variable if exist otherwise return defalutValue
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}
11 changes: 7 additions & 4 deletions mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
"os"
"strings"

_ "github.com/go-sql-driver/mysql"
)

var (
Expand All @@ -17,8 +16,12 @@ var (
)

func mariadbHandler(w http.ResponseWriter, r *http.Request) {
mariadbPath := r.URL.Path
localService, lagoonService := cleanRoute(mariadbPath)
service, error := verifyDriverService(r)
if error != nil {
fmt.Fprintf(w, error.Error())
return
}
localService, lagoonService := cleanRoute(service)
mariadbUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mariadbPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mariadbHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
Expand Down
8 changes: 6 additions & 2 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ var (
)

func mongoHandler(w http.ResponseWriter, r *http.Request) {
mongoPath := r.URL.Path
localService, lagoonService := cleanRoute(mongoPath)
service, error := verifyDriverService(r)
if error != nil {
fmt.Fprintf(w, error.Error())
return
}
localService, lagoonService := cleanRoute(service)
mongoUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mongoPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mongoHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
Expand Down
17 changes: 12 additions & 5 deletions opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ import (
)

var (
opensearchHost = os.Getenv("OPENSEARCH_HOST")
opensearchConnectionStr = fmt.Sprintf("http://%s:9200", opensearchHost)
opensearchHost string
)

func opensearchHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, opensearchConnector())
service, error := verifyDriverService(r)
if error != nil {
fmt.Fprintf(w, error.Error())
return
}
opensearchHost = service
opensearchConnectionStr := fmt.Sprintf("http://%s:9200", opensearchHost)

fmt.Fprintf(w, opensearchConnector(opensearchConnectionStr))
}

func cleanOpensearchOutput(sr *opensearchapi.Response) string {
Expand Down Expand Up @@ -90,12 +97,12 @@ func createOpensearchIndexDocument(client *opensearch.Client) {
time.Sleep(1 * time.Second)
}

func opensearchConnector() string {
func opensearchConnector(connectionString string) string {
client, _ := opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{opensearchConnectionStr},
Addresses: []string{connectionString},
})

createOpensearchIndexDocument(client)
Expand Down
8 changes: 6 additions & 2 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ var (
)

func postgresHandler(w http.ResponseWriter, r *http.Request) {
postgresPath := r.URL.Path
localService, lagoonService := cleanRoute(postgresPath)
service, error := verifyDriverService(r)
if error != nil {
fmt.Fprintf(w, error.Error())
return
}
localService, lagoonService := cleanRoute(service)
postgresUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
postgresPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
postgresHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
Expand Down
8 changes: 6 additions & 2 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ var (
)

func redisHandler(w http.ResponseWriter, r *http.Request) {
redisPath := r.URL.Path
redisRoute := strings.ReplaceAll(redisPath, "/", "")
service, error := verifyDriverService(r)
if error != nil {
fmt.Fprintf(w, error.Error())
return
}
redisRoute := strings.ReplaceAll(service, "/", "")
redisConnectionStr := fmt.Sprintf("%s:6379", redisRoute)
fmt.Fprintf(w, redisConnector(redisConnectionStr, redisRoute))
}
Expand Down
8 changes: 6 additions & 2 deletions solr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import (
)

func solrHandler(w http.ResponseWriter, r *http.Request) {
solrPath := r.URL.Path
solrRoute := strings.ReplaceAll(solrPath, "/", "")
service, error := verifyDriverService(r)
if error != nil {
fmt.Fprintf(w, error.Error())
return
}
solrRoute := strings.ReplaceAll(service, "/", "")
solrConnectionStr := fmt.Sprintf("http://%s:8983/solr", solrRoute)
fmt.Fprintf(w, convertSolrDoc(solrConnector(solrConnectionStr), solrRoute))
}
Expand Down