Skip to content

Commit

Permalink
Importing machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Jun 13, 2023
1 parent 46208bd commit c7c72dd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 35 deletions.
17 changes: 1 addition & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ package main
import (
"bytes"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"strings"

"github.com/gorilla/mux"
)

var (
localCheck = os.Getenv("LAGOON_ENVIRONMENT")
)

type funcType func() map[string]string
Expand Down Expand Up @@ -66,12 +60,3 @@ func cleanRoute(basePath string) (string, string) {
lagoonService := strings.ToUpper(replaceHyphen)
return localService, lagoonService
}

// 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
}
11 changes: 6 additions & 5 deletions mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"fmt"
machineryEnvVars "github.com/uselagoon/machinery/utils/variables"
"log"
"net/http"
"os"
Expand All @@ -19,11 +20,11 @@ var (
func mariadbHandler(w http.ResponseWriter, r *http.Request) {
mariadbPath := r.URL.Path
localService, lagoonService := cleanRoute(mariadbPath)
mariadbUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mariadbPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mariadbHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
mariadbPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306")
mariadbDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")
mariadbUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mariadbPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mariadbHost := machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
mariadbPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306")
mariadbDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")

mariadbConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mariadbUser, mariadbPassword, mariadbHost, mariadbPort, mariadbDatabase)
log.Print(fmt.Sprintf("Using %s as the connstring", mariadbConnectionStr))
Expand Down
11 changes: 6 additions & 5 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
machineryEnvVars "github.com/uselagoon/machinery/utils/variables"
"log"
"net/http"
"os"
Expand All @@ -25,11 +26,11 @@ var (
func mongoHandler(w http.ResponseWriter, r *http.Request) {
mongoPath := r.URL.Path
localService, lagoonService := cleanRoute(mongoPath)
mongoUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mongoPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mongoHost = getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
mongoPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "27017")
mongoDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")
mongoUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mongoPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mongoHost = machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
mongoPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "27017")
mongoDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")

if mongoHost != localService {
mongoConnectionStr = fmt.Sprintf("mongodb://%s:%s@%s:%s/%s", mongoUser, mongoPassword, mongoHost, mongoPort, mongoDatabase)
Expand Down
3 changes: 2 additions & 1 deletion opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
machineryEnvVars "github.com/uselagoon/machinery/utils/variables"
"log"
"net/http"
"os"
Expand All @@ -17,7 +18,7 @@ import (
)

var (
opensearchHost = os.Getenv("OPENSEARCH_HOST")
opensearchHost = machineryEnvVars.GetEnv("OPENSEARCH_HOST", "opensearch-2")
opensearchConnectionStr = fmt.Sprintf("http://%s:9200", opensearchHost)
)

Expand Down
6 changes: 3 additions & 3 deletions persistentstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"fmt"
machineryEnvVars "github.com/uselagoon/machinery/utils/variables"
"log"
"net/http"
"os"
"strconv"
"strings"

"os"
)

func persistentStorageHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -17,7 +17,7 @@ func persistentStorageHandler(w http.ResponseWriter, r *http.Request) {
}

func persistentStorageConnector(route string) string {
if route != os.Getenv("STORAGE_LOCATION") {
if route != machineryEnvVars.GetEnv("STORAGE_LOCATION", "") {
return "Storage location is not defined - ensure format matches '/storage?path=[path]'"
}
path := route + "/storage.txt"
Expand Down
11 changes: 6 additions & 5 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"fmt"
machineryEnvVars "github.com/uselagoon/machinery/utils/variables"
"log"
"net/http"
"os"
Expand All @@ -20,11 +21,11 @@ var (
func postgresHandler(w http.ResponseWriter, r *http.Request) {
postgresPath := r.URL.Path
localService, lagoonService := cleanRoute(postgresPath)
postgresUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
postgresPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
postgresHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
postgresPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "5432")
postgresDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")
postgresUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
postgresPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
postgresHost := machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
postgresPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "5432")
postgresDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")

postgresConnectionStr = fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s host=%s port=%s", postgresUser, postgresPassword, postgresDatabase, postgresSSL, postgresHost, postgresPort)
log.Print(fmt.Sprintf("Using %s as the connstring", postgresConnectionStr))
Expand Down

0 comments on commit c7c72dd

Please sign in to comment.