-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81a1cfa
commit 172555e
Showing
6 changed files
with
189 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |