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
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,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: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)
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
r.HandleFunc("/", handleReq)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":3000", nil))
Expand Down Expand Up @@ -68,9 +68,9 @@ func cleanRoute(basePath string) (string, string) {

// 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
}
7 changes: 3 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,8 @@ var (
)

func mariadbHandler(w http.ResponseWriter, r *http.Request) {
mariadbPath := r.URL.Path
localService, lagoonService := cleanRoute(mariadbPath)
service := r.URL.Query().Get("service")
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
4 changes: 2 additions & 2 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var (
)

func mongoHandler(w http.ResponseWriter, r *http.Request) {
mongoPath := r.URL.Path
localService, lagoonService := cleanRoute(mongoPath)
service := r.URL.Query().Get("service")
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
13 changes: 8 additions & 5 deletions opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ 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 := r.URL.Query().Get("service")
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 +93,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
4 changes: 2 additions & 2 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var (
)

func postgresHandler(w http.ResponseWriter, r *http.Request) {
postgresPath := r.URL.Path
localService, lagoonService := cleanRoute(postgresPath)
service := r.URL.Query().Get("service")
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
4 changes: 2 additions & 2 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (
)

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

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