Skip to content

Commit

Permalink
Updating connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Oct 24, 2022
1 parent 81a1cfa commit 172555e
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 118 deletions.
4 changes: 4 additions & 0 deletions .lagoon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docker-compose-yaml: docker-compose.yml

environment_variables:
git_sha: 'true'
16 changes: 9 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ RUN go get github.com/joho/godotenv

RUN go build && chmod +x ./go-dbaas

ENV MARIADB_PASSWORD=test \
MARIADB_USER=test \
MARIADB_DATABASE=infrastructure \
POSTGRES_USER=pqgotest \
POSTGRES_PASSWORD=pqgotest \
POSTGRES_DB=infrastructure
ENV MARIADB_PASSWORD=lagoon \
MARIADB_USER=lagoon \
MARIADB_DATABASE=lagoon \
MARIADB_HOST=mariadb \
POSTGRES_USER=lagoon \
POSTGRES_PASSWORD=lagoon \
POSTGRES_DATABASE=lagoon \
POSTGRES_HOST=postgres

EXPOSE 8080
EXPOSE 3000

CMD sleep 10 && ./go-dbaas
35 changes: 20 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@ services:
build:
context: .
dockerfile: Dockerfile
labels:
lagoon.type: basic
ports:
- '8080:8080'
- '3000:3000'
depends_on:
- mariadb
- postgres
container_name: go-web
# environment:
# - 'DB_PORT=3305'
# - 'DB_HOST=database'
environment:
- LAGOON_TEST_VAR=test

mariadb:
image: mariadb:10.4
image: uselagoon/mariadb-10.6
labels:
lagoon.type: mariadb
ports:
- '3305:3305'
- '3306'
environment:
- MARIADB_PASSWORD=test
- MARIADB_USER=test
- MARIADB_DATABASE=infrastructure
- MARIADB_ROOT_PASSWORD=test
- MARIADB_PASSWORD=lagoon
- MARIADB_USER=lagoon
- MARIADB_DATABASE=lagoon
- MARIADB_ROOT_PASSWORD=Lag00n

postgres:
image: postgres:14.1-alpine
image: uselagoon/postgres-14
labels:
lagoon.type: postgres
ports:
- '5432:5432'
- '5432'
environment:
- POSTGRES_USER=pqgotest
- POSTGRES_PASSWORD=pqgotest
- POSTGRES_DB=infrastructure
- POSTGRES_USER=lagoon
- POSTGRES_PASSWORD=lagoon
- POSTGRES_DB=lagoon
103 changes: 7 additions & 96 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,124 +1,35 @@
package main

import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"

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

var (
mariaUser = os.Getenv("MARIADB_USER")
mariaPassword = os.Getenv("MARIADB_PASSWORD")
mariaDB = os.Getenv("MARIADB_DATABASE")
mariaHost = "mariadb"
mariaPort = 3306
mariaDriver = "mysql"
mariaConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", mariaUser, mariaPassword, mariaHost, mariaPort, mariaDB)

postgresUser = os.Getenv("POSTGRES_USER")
postgresPassword = os.Getenv("POSTGRES_PASSWORD")
postgresDB = os.Getenv("POSTGRES_DB")
postgresHost = "postgres"
postgresSSL = "disable"
postgresConnectionStr = fmt.Sprintf(
"user=%s password=%s dbname=%s sslmode=%s host=%s",
postgresUser, postgresPassword, postgresDB, postgresSSL, postgresHost)
)

type funcType func() string
type funcType func() map[string]string

var funcToCall []funcType

func main() {

handler := http.HandlerFunc(handleReq)
mariaHandler := http.HandlerFunc(mariaHandler)
postgresHandler := http.HandlerFunc(postgresHandler)
http.Handle("/", handler)
http.Handle("/mariadb", handler)
http.Handle("/mariadb", mariaHandler)
http.Handle("/postgres", postgresHandler)

log.Fatal(http.ListenAndServe(":8080", nil))
log.Fatal(http.ListenAndServe(":3000", nil))
}

func handleReq(w http.ResponseWriter, r *http.Request) {
funcToCall = append(funcToCall, mariaDBConnector, postgresDBConnector)
for _, conFunc := range funcToCall {
resp := make(map[string]string)
resp["Connection"] = conFunc()
resp := conFunc()
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
}

func mariaDBConnector() string {
db, err := sql.Open(mariaDriver, mariaConnectionStr)
if err != nil {
log.Print(err)
}

defer db.Close()

createTable := "CREATE TABLE IF NOT EXISTS env(environment text)"
createResult, err := db.Exec(createTable)
if err != nil {
panic(err.Error())
} else {
fmt.Println(createResult)
}

query := "INSERT INTO env(environment) VALUES (?)"
insertResult, err := db.Exec(query, mariaConnectionStr)
if err != nil {
panic(err.Error())
} else {
fmt.Println(insertResult)
}

var envVars string
err = db.QueryRow("select * from env").Scan(&envVars)
if err != nil {
log.Print(err)
}

return envVars
}

func postgresDBConnector() string {
db, err := sql.Open(postgresHost, postgresConnectionStr)
if err != nil {
log.Print(err)
}

defer db.Close()

createTable := "CREATE TABLE IF NOT EXISTS env(environment text)"
createResult, err := db.Exec(createTable)
if err != nil {
panic(err.Error())
} else {
fmt.Println(createResult)
}

query := "INSERT INTO env(environment) VALUES ($1)"
insertResult, err := db.Exec(query, postgresConnectionStr)
if err != nil {
panic(err.Error())
} else {
fmt.Println(insertResult)
}

var envVars string
err = db.QueryRow("select * from env").Scan(&envVars)
if err != nil {
log.Print(err)
}

return envVars
}
74 changes: 74 additions & 0 deletions mariadb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

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

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

var (
mariaUser = os.Getenv("MARIADB_USER")
mariaPassword = os.Getenv("MARIADB_PASSWORD")
mariaDB = os.Getenv("MARIADB_DATABASE")
mariaHost = os.Getenv("MARIADB_HOST")
mariaPort = 3306
mariaDriver = "mysql"
mariaConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", mariaUser, mariaPassword, mariaHost, mariaPort, mariaDB)
)

func mariaHandler(w http.ResponseWriter, r *http.Request) {
resp := mariaDBConnector()
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}

func mariaDBConnector() map[string]string {
db, err := sql.Open(mariaDriver, mariaConnectionStr)
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 {
panic(err.Error())
}

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 {
panic(err.Error())
}
}
// mdbQuery := "MARIADB%"
gitSHA := "LAGOON_%"
rows, err := db.Query(`SELECT * FROM env where env_key LIKE ?`, gitSHA)
if err != nil {
log.Print(err)
}

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

return results
}
75 changes: 75 additions & 0 deletions postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

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

_ "github.com/lib/pq"
)

var (
postgresUser = os.Getenv("POSTGRES_USER")
postgresPassword = os.Getenv("POSTGRES_PASSWORD")
postgresDB = os.Getenv("POSTGRES_DATABASE")
postgresHost = os.Getenv("POSTGRES_HOST")
postgresSSL = "disable"
postgresConnectionStr = fmt.Sprintf(
"user=%s password=%s dbname=%s sslmode=%s host=%s",
postgresUser, postgresPassword, postgresDB, postgresSSL, postgresHost)
)

func postgresHandler(w http.ResponseWriter, r *http.Request) {
resp := postgresDBConnector()
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}

func postgresDBConnector() map[string]string {
db, err := sql.Open(postgresHost, postgresConnectionStr)
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 {
panic(err.Error())
}

query := "INSERT INTO env(env_key, env_value) VALUES ($1, $2)"
for _, e := range os.Environ() {

pair := strings.SplitN(e, "=", 2)
_, err := db.Exec(query, pair[0], pair[1])
if err != nil {
panic(err.Error())
}
}

// pgQuery := "POSTGRES%"
gitSHA := "LAGOON_%"
rows, err := db.Query(`SELECT * FROM env where env_key LIKE $1`, gitSHA)
if err != nil {
log.Print(err)
}

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 172555e

Please sign in to comment.