chore: padroniza conexão de banco via DATABASE_URL
This commit is contained in:
parent
7196d5a389
commit
eab6c26aca
9 changed files with 23 additions and 138 deletions
|
|
@ -17,20 +17,12 @@ func main() {
|
||||||
log.Println("Warning: Error loading .env file, relying on system env")
|
log.Println("Warning: Error loading .env file, relying on system env")
|
||||||
}
|
}
|
||||||
|
|
||||||
host := os.Getenv("DB_HOST")
|
dbURL := os.Getenv("DATABASE_URL")
|
||||||
if host == "" {
|
if dbURL == "" {
|
||||||
// Fallback for debugging if env not picked up
|
log.Fatal("DATABASE_URL environment variable not set")
|
||||||
host = "localhost"
|
|
||||||
}
|
}
|
||||||
port := os.Getenv("DB_PORT")
|
|
||||||
user := os.Getenv("DB_USER")
|
|
||||||
password := os.Getenv("DB_PASSWORD")
|
|
||||||
dbname := os.Getenv("DB_NAME")
|
|
||||||
|
|
||||||
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
db, err := sql.Open("postgres", dbURL)
|
||||||
host, port, user, password, dbname)
|
|
||||||
|
|
||||||
db, err := sql.Open("postgres", connStr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,20 @@ package main
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db, err := sql.Open("postgres", "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require")
|
dbURL := os.Getenv("DATABASE_URL")
|
||||||
|
if dbURL == "" {
|
||||||
|
log.Fatal("DATABASE_URL environment variable not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("postgres", dbURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("❌ Connection error:", err)
|
fmt.Println("❌ Connection error:", err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -14,18 +14,7 @@ func main() {
|
||||||
godotenv.Load(".env")
|
godotenv.Load(".env")
|
||||||
dbURL := os.Getenv("DATABASE_URL")
|
dbURL := os.Getenv("DATABASE_URL")
|
||||||
if dbURL == "" {
|
if dbURL == "" {
|
||||||
// Fallback
|
log.Fatal("DATABASE_URL environment variable not set")
|
||||||
host := os.Getenv("DB_HOST")
|
|
||||||
port := os.Getenv("DB_PORT")
|
|
||||||
user := os.Getenv("DB_USER")
|
|
||||||
pass := os.Getenv("DB_PASSWORD")
|
|
||||||
name := os.Getenv("DB_NAME")
|
|
||||||
ssl := os.Getenv("DB_SSLMODE")
|
|
||||||
if host != "" {
|
|
||||||
dbURL = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", user, pass, host, port, name, ssl)
|
|
||||||
} else {
|
|
||||||
log.Fatal("DB URL not found")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("postgres", dbURL)
|
db, err := sql.Open("postgres", dbURL)
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,7 @@ func main() {
|
||||||
|
|
||||||
dbURL := os.Getenv("DATABASE_URL")
|
dbURL := os.Getenv("DATABASE_URL")
|
||||||
if dbURL == "" {
|
if dbURL == "" {
|
||||||
host := os.Getenv("DB_HOST")
|
log.Fatal("DATABASE_URL environment variable not set")
|
||||||
port := os.Getenv("DB_PORT")
|
|
||||||
user := os.Getenv("DB_USER")
|
|
||||||
pass := os.Getenv("DB_PASSWORD")
|
|
||||||
name := os.Getenv("DB_NAME")
|
|
||||||
ssl := os.Getenv("DB_SSLMODE")
|
|
||||||
if host != "" {
|
|
||||||
dbURL = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", user, pass, host, port, name, ssl)
|
|
||||||
} else {
|
|
||||||
// Last resort
|
|
||||||
dbURL = "postgres://postgres:postgres@localhost:5432/gohorsejobs?sslmode=disable"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("postgres", dbURL)
|
db, err := sql.Open("postgres", dbURL)
|
||||||
|
|
|
||||||
|
|
@ -40,44 +40,12 @@ func InitDB() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildConnectionString() (string, error) {
|
func BuildConnectionString() (string, error) {
|
||||||
// Prefer DATABASE_URL if set (standard format)
|
|
||||||
if dbURL := os.Getenv("DATABASE_URL"); dbURL != "" {
|
if dbURL := os.Getenv("DATABASE_URL"); dbURL != "" {
|
||||||
log.Println("Using DATABASE_URL for connection")
|
log.Println("Using DATABASE_URL for connection")
|
||||||
return dbURL, nil
|
return dbURL, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to individual params for backward compatibility
|
return "", fmt.Errorf("DATABASE_URL environment variable not set")
|
||||||
host := os.Getenv("DB_HOST")
|
|
||||||
if host == "" {
|
|
||||||
return "", fmt.Errorf("DATABASE_URL or DB_HOST environment variable not set")
|
|
||||||
}
|
|
||||||
user := os.Getenv("DB_USER")
|
|
||||||
if user == "" {
|
|
||||||
return "", fmt.Errorf("DB_USER environment variable not set")
|
|
||||||
}
|
|
||||||
password := os.Getenv("DB_PASSWORD")
|
|
||||||
if password == "" {
|
|
||||||
return "", fmt.Errorf("DB_PASSWORD environment variable not set")
|
|
||||||
}
|
|
||||||
dbname := os.Getenv("DB_NAME")
|
|
||||||
if dbname == "" {
|
|
||||||
return "", fmt.Errorf("DB_NAME environment variable not set")
|
|
||||||
}
|
|
||||||
port := os.Getenv("DB_PORT")
|
|
||||||
if port == "" {
|
|
||||||
port = "5432"
|
|
||||||
}
|
|
||||||
|
|
||||||
sslmode := os.Getenv("DB_SSLMODE")
|
|
||||||
if sslmode == "" {
|
|
||||||
sslmode = "require" // Default to require for production security
|
|
||||||
}
|
|
||||||
|
|
||||||
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
|
||||||
host, port, user, password, dbname, sslmode)
|
|
||||||
log.Println("Using individual DB_* params for connection")
|
|
||||||
return connStr, nil
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunMigrations() {
|
func RunMigrations() {
|
||||||
|
|
|
||||||
|
|
@ -12,21 +12,9 @@ import (
|
||||||
func TestBuildConnectionString(t *testing.T) {
|
func TestBuildConnectionString(t *testing.T) {
|
||||||
// Backup env vars
|
// Backup env vars
|
||||||
oldURL := os.Getenv("DATABASE_URL")
|
oldURL := os.Getenv("DATABASE_URL")
|
||||||
oldHost := os.Getenv("DB_HOST")
|
|
||||||
oldUser := os.Getenv("DB_USER")
|
|
||||||
oldPass := os.Getenv("DB_PASSWORD")
|
|
||||||
oldName := os.Getenv("DB_NAME")
|
|
||||||
oldPort := os.Getenv("DB_PORT")
|
|
||||||
oldSSL := os.Getenv("DB_SSLMODE")
|
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
os.Setenv("DATABASE_URL", oldURL)
|
os.Setenv("DATABASE_URL", oldURL)
|
||||||
os.Setenv("DB_HOST", oldHost)
|
|
||||||
os.Setenv("DB_USER", oldUser)
|
|
||||||
os.Setenv("DB_PASSWORD", oldPass)
|
|
||||||
os.Setenv("DB_NAME", oldName)
|
|
||||||
os.Setenv("DB_PORT", oldPort)
|
|
||||||
os.Setenv("DB_SSLMODE", oldSSL)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Case 1: DATABASE_URL
|
// Case 1: DATABASE_URL
|
||||||
|
|
@ -39,29 +27,11 @@ func TestBuildConnectionString(t *testing.T) {
|
||||||
t.Errorf("Mismatch URL")
|
t.Errorf("Mismatch URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 2: Individual Params
|
// Case 2: Missing DATABASE_URL
|
||||||
os.Unsetenv("DATABASE_URL")
|
os.Unsetenv("DATABASE_URL")
|
||||||
os.Setenv("DB_HOST", "localhost")
|
|
||||||
os.Setenv("DB_USER", "user")
|
|
||||||
os.Setenv("DB_PASSWORD", "pass")
|
|
||||||
os.Setenv("DB_NAME", "mydb")
|
|
||||||
os.Setenv("DB_PORT", "5432")
|
|
||||||
os.Setenv("DB_SSLMODE", "disable")
|
|
||||||
|
|
||||||
s, err = database.BuildConnectionString()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
expected := "host=localhost port=5432 user=user password=pass dbname=mydb sslmode=disable"
|
|
||||||
if s != expected {
|
|
||||||
t.Errorf("Expected %s, got %s", expected, s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 3: Missing Param
|
|
||||||
os.Unsetenv("DB_HOST")
|
|
||||||
_, err = database.BuildConnectionString()
|
_, err = database.BuildConnectionString()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("Expected error for missing host")
|
t.Error("Expected error for missing DATABASE_URL")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,23 +44,8 @@ func TestMain(m *testing.M) {
|
||||||
func setTestEnv() {
|
func setTestEnv() {
|
||||||
// Only set if not already set (allows override via .env file)
|
// Only set if not already set (allows override via .env file)
|
||||||
// These are defaults for when running tests without sourcing .env
|
// These are defaults for when running tests without sourcing .env
|
||||||
if os.Getenv("DB_HOST") == "" {
|
if os.Getenv("DATABASE_URL") == "" {
|
||||||
os.Setenv("DB_HOST", "db-60059.dc-sp-1.absamcloud.com")
|
os.Setenv("DATABASE_URL", "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require")
|
||||||
}
|
|
||||||
if os.Getenv("DB_USER") == "" {
|
|
||||||
os.Setenv("DB_USER", "yuki")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_PASSWORD") == "" {
|
|
||||||
os.Setenv("DB_PASSWORD", "xl1zfmr6e9bb")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_NAME") == "" {
|
|
||||||
os.Setenv("DB_NAME", "gohorsejobs_dev")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_PORT") == "" {
|
|
||||||
os.Setenv("DB_PORT", "26868")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_SSLMODE") == "" {
|
|
||||||
os.Setenv("DB_SSLMODE", "require")
|
|
||||||
}
|
}
|
||||||
if os.Getenv("JWT_SECRET") == "" {
|
if os.Getenv("JWT_SECRET") == "" {
|
||||||
os.Setenv("JWT_SECRET", "gohorse-super-secret-key-2024-production")
|
os.Setenv("JWT_SECRET", "gohorse-super-secret-key-2024-production")
|
||||||
|
|
|
||||||
|
|
@ -31,23 +31,8 @@ func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setTestEnv() {
|
func setTestEnv() {
|
||||||
if os.Getenv("DB_HOST") == "" {
|
if os.Getenv("DATABASE_URL") == "" {
|
||||||
os.Setenv("DB_HOST", "db-60059.dc-sp-1.absamcloud.com")
|
os.Setenv("DATABASE_URL", "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require")
|
||||||
}
|
|
||||||
if os.Getenv("DB_USER") == "" {
|
|
||||||
os.Setenv("DB_USER", "yuki")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_PASSWORD") == "" {
|
|
||||||
os.Setenv("DB_PASSWORD", "xl1zfmr6e9bb")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_NAME") == "" {
|
|
||||||
os.Setenv("DB_NAME", "gohorsejobs_dev")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_PORT") == "" {
|
|
||||||
os.Setenv("DB_PORT", "26868")
|
|
||||||
}
|
|
||||||
if os.Getenv("DB_SSLMODE") == "" {
|
|
||||||
os.Setenv("DB_SSLMODE", "require")
|
|
||||||
}
|
}
|
||||||
if os.Getenv("JWT_SECRET") == "" {
|
if os.Getenv("JWT_SECRET") == "" {
|
||||||
os.Setenv("JWT_SECRET", "gohorse-super-secret-key-2024-production")
|
os.Setenv("JWT_SECRET", "gohorse-super-secret-key-2024-production")
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ func TestVerifyLogin(t *testing.T) {
|
||||||
// Config
|
// Config
|
||||||
dbURL := os.Getenv("DATABASE_URL")
|
dbURL := os.Getenv("DATABASE_URL")
|
||||||
if dbURL == "" {
|
if dbURL == "" {
|
||||||
dbURL = "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require"
|
t.Skip("Skipping: DATABASE_URL not set")
|
||||||
}
|
}
|
||||||
// Updated to match deployed backend .env
|
// Updated to match deployed backend .env
|
||||||
pepper := "some-random-string-for-password-hashing"
|
pepper := "some-random-string-for-password-hashing"
|
||||||
|
|
@ -82,7 +82,7 @@ func TestVerifyLoginNoPepper(t *testing.T) {
|
||||||
|
|
||||||
dbURL := os.Getenv("DATABASE_URL")
|
dbURL := os.Getenv("DATABASE_URL")
|
||||||
if dbURL == "" {
|
if dbURL == "" {
|
||||||
dbURL = "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require"
|
t.Skip("Skipping: DATABASE_URL not set")
|
||||||
}
|
}
|
||||||
password := "Admin@2025!"
|
password := "Admin@2025!"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue