「🔨」 fix(Mariadb): entrypoint working as expected
This commit is contained in:
@ -3,11 +3,24 @@ package main
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"os"
|
||||
|
||||
"git.keyzox.me/42_adjoly/inception/internal/env"
|
||||
"git.keyzox.me/42_adjoly/inception/internal/log"
|
||||
)
|
||||
|
||||
|
||||
func escapeIdentifier(identifier string) string {
|
||||
// Replace backticks with double backticks to safely escape identifiers
|
||||
return fmt.Sprintf("`%s`", strings.ReplaceAll(identifier, "'", "\""))
|
||||
}
|
||||
|
||||
func escapePassword(password string) string {
|
||||
// Escape single quotes in passwords
|
||||
return strings.ReplaceAll(password, "'", "\\'")
|
||||
}
|
||||
|
||||
func checkHealth(host, user, pass, port, dbName string) bool {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, pass, host, port, dbName)
|
||||
|
||||
@ -23,14 +36,15 @@ func checkHealth(host, user, pass, port, dbName string) bool {
|
||||
}
|
||||
|
||||
func main() {
|
||||
pass := env.FileEnv("MYSQL_PASSWORD", "default")
|
||||
user := env.FileEnv("MYSQL_USER", "mariadb")
|
||||
dbName := env.EnvCheck("MYSQL_DATABASE", "default")
|
||||
dbHost := "localhost"
|
||||
pass := escapePassword(env.FileEnv("MYSQL_PASSWORD", "default"))
|
||||
user := escapeIdentifier(env.FileEnv("MYSQL_USER", "mariadb"))
|
||||
dbName := escapeIdentifier(env.EnvCheck("MYSQL_DATABASE", "default"))
|
||||
dbHost := escapeIdentifier("localhost")
|
||||
|
||||
res := checkHealth(dbHost, user, pass, "3306", dbName)
|
||||
if res == true{
|
||||
_log.Log("note", "Mariadb is healthy")
|
||||
os.Exit(0)
|
||||
}
|
||||
_log.Log("error", "Health check failed")
|
||||
}
|
||||
|
Reference in New Issue
Block a user