Skip to content

Commit 8c253e9

Browse files
authored
Merge pull request #3 from cuappdev/deployment-fix
Fixed logging and load env
2 parents 066c38d + 1349747 commit 8c253e9

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/jackc/puddle/v2 v2.2.2 // indirect
2626
github.com/jinzhu/inflection v1.0.0 // indirect
2727
github.com/jinzhu/now v1.1.5 // indirect
28+
github.com/joho/godotenv v1.5.1 // indirect
2829
github.com/json-iterator/go v1.1.12 // indirect
2930
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
3031
github.com/kr/text v0.2.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
4141
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
4242
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
4343
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
44+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
45+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
4446
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
4547
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
4648
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ package main
33
import (
44
"log"
55
"github.com/gin-gonic/gin"
6-
6+
"github.com/joho/godotenv"
77
"github.com/cuappdev/hustle-backend/models"
88
"github.com/cuappdev/hustle-backend/controllers"
99
)
1010

1111
func main() {
12+
err := godotenv.Load()
13+
if err != nil {
14+
log.Println("Error loading .env file.")
15+
}
16+
1217
log.Println("Starting hustle-backend...")
1318
r := gin.Default()
1419
log.Println("Connecting to database...")

models/setup.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67
"time"
@@ -10,7 +11,7 @@ import (
1011

1112
var DB *gorm.DB
1213

13-
func ConnectDatabase() {
14+
func ConnectDatabase() error {
1415
// Build connection string from env variables
1516
host := getEnv("DB_HOST", "localhost")
1617
user := getEnv("DB_USER", "postgres")
@@ -19,31 +20,31 @@ func ConnectDatabase() {
1920
password := getEnv("DB_PASSWORD", "")
2021
sslmode := getEnv("DB_SSLMODE", "require")
2122

22-
dsn := "host=" + host + " user=" + user + " password=" + password + " dbname=" + dbname + " port=" + port + " sslmode=" + sslmode + " TimeZone=UTC"
23-
23+
log.Println("Connecting to database with DSN:", dsn)
24+
2425
database, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
2526
if err != nil {
26-
return err
27+
return fmt.Errorf("failed to connect to database: %w", err)
2728
}
2829

2930
sqlDB, err := database.DB()
3031
if err != nil {
31-
return err
32+
return fmt.Errorf("failed to get database instance: %w", err)
3233
}
3334

3435
sqlDB.SetMaxIdleConns(10)
3536
sqlDB.SetMaxOpenConns(100)
3637
sqlDB.SetConnMaxLifetime(time.Hour)
3738

38-
// Make sure to include all models to migrate here
39+
// Make sure to include all models to migrate here
3940
err = database.AutoMigrate(&User{})
4041
if err != nil {
41-
log.Printf("Failed to migrate database: %v", err)
42-
return err
42+
return fmt.Errorf("failed to migrate database: %w", err)
4343
}
4444

4545
DB = database
4646
log.Println("Database connected successfully")
47+
return nil
4748
}
4849

4950
func getEnv(key, defaultValue string) string {

0 commit comments

Comments
 (0)