Skip to content

Commit

Permalink
Updated to write & read from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Oct 17, 2022
1 parent bb3994d commit 81a1cfa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ RUN go get github.com/joho/godotenv

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

ENV MARIADB_PASSWORD=api \
MARIADB_USER=api \
ENV MARIADB_PASSWORD=test \
MARIADB_USER=test \
MARIADB_DATABASE=infrastructure \
POSTGRES_USER=pqgotest \
POSTGRES_PASSWORD=pqgotest \
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ services:
ports:
- '3305:3305'
environment:
- MARIADB_PASSWORD=api
- MARIADB_USER=api
- MARIADB_PASSWORD=test
- MARIADB_USER=test
- MARIADB_DATABASE=infrastructure
- MARIADB_ROOT_PASSWORD=api
- MARIADB_ROOT_PASSWORD=test

postgres:
image: postgres:14.1-alpine
Expand Down
79 changes: 68 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,94 @@ var (
postgresUser, postgresPassword, postgresDB, postgresSSL, postgresHost)
)

type funcType func() string

var funcToCall []funcType

func main() {

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

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

func handleReq(w http.ResponseWriter, r *http.Request) {
resp := make(map[string]string)
resp[mariaHost] = dbConnector(mariaDriver, mariaConnectionStr)
resp[postgresHost] = dbConnector(postgresHost, postgresConnectionStr)
jsonResp, err := json.Marshal(resp)
funcToCall = append(funcToCall, mariaDBConnector, postgresDBConnector)
for _, conFunc := range funcToCall {
resp := make(map[string]string)
resp["Connection"] = 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 {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
panic(err.Error())
} else {
fmt.Println(insertResult)
}
w.Write(jsonResp)

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

return envVars
}

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

defer db.Close()

var version string
err = db.QueryRow("select version()").Scan(&version)
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 version

return envVars
}

0 comments on commit 81a1cfa

Please sign in to comment.