Skip to content

Commit

Permalink
Included mysql in test suite - placeholder images to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Jan 12, 2023
1 parent 78b7efc commit f276a55
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ services:
ports:
- '27017'

mysql-5-7:
image: chrisgio/testimages:mysql-5.7 # Placeholder image - to be updated
labels:
lagoon.type: mariadb
ports:
- "3306"

mysql-8-0:
image: chrisgio/testimages:mysql-8.0 # Placeholder image - to be updated
labels:
lagoon.type: mariadb
ports:
- "3306"

opensearch-2:
image: uselagoon/opensearch-2:latest
environment:
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
r.HandleFunc("/{solr:solr-.*}", solrHandler)
r.HandleFunc("/{mongo:mongo-.*}", mongoHandler)
r.HandleFunc("/{opensearch:opensearch-.*}", opensearchHandler)
r.HandleFunc("/{mysql:mysql-.*}", mysqlHandler)
r.HandleFunc("/", handleReq)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":3000", nil))
Expand Down Expand Up @@ -60,17 +61,17 @@ func connectorKeyValues(values []string) string {

func cleanRoute(basePath string) (string, string) {
cleanRoute := strings.ReplaceAll(basePath, "/", "")
localService := strings.ReplaceAll(cleanRoute, "10.", "10-")
localService := strings.ReplaceAll(cleanRoute, ".", "-")
replaceHyphen := strings.ReplaceAll(localService, "-", "_")
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
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}
75 changes: 75 additions & 0 deletions mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"strings"

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

var (
mysqlVersion string
mysqlConnectionStr string
)

func mysqlHandler(w http.ResponseWriter, r *http.Request) {
mysqlPath := r.URL.Path
localService, lagoonService := cleanRoute(mysqlPath)
mysqlUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mysqlPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mysqlHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
mysqlPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306")
mysqlDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")

mysqlConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDatabase)
log.Print(fmt.Sprintf("Using %s as the connstring", mysqlConnectionStr))

fmt.Fprintf(w, dbConnectorPairs(mysqlConnector(mysqlConnectionStr), mysqlVersion))
}

func mysqlConnector(connectionString string) map[string]string {
db, err := sql.Open("mysql", connectionString)
if err != nil {
log.Print(err)
}

defer db.Close()

createTable := "CREATE TABLE IF NOT EXISTS env(env_key text, env_value text)"
_, err = db.Exec(createTable)
if err != nil {
log.Print(err)
}

query := "INSERT INTO env(env_key, env_value) VALUES (?, ?)"

for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
_, err := db.Exec(query, pair[0], pair[1])
if err != nil {
log.Print(err)
}
}

q := "LAGOON_%"
rows, err := db.Query(`SELECT * FROM env where env_key LIKE ?`, q)
if err != nil {
log.Print(err)
}

db.QueryRow("SELECT VERSION()").Scan(&mysqlVersion)

defer rows.Close()
results := make(map[string]string)
for rows.Next() {
var envKey, envValue string
_ = rows.Scan(&envKey, &envValue)
results[envKey] = envValue
}

return results
}

0 comments on commit f276a55

Please sign in to comment.