diff --git a/backend/go.mod b/backend/go.mod index dc480ad..12b7ef6 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-playground/validator/v10 v10.22.0 github.com/gorilla/websocket v1.5.0 github.com/graph-gophers/dataloader/v7 v7.1.0 + github.com/joho/godotenv v1.5.1 github.com/labstack/echo/v4 v4.12.0 github.com/labstack/gommon v0.4.2 github.com/m-mizutani/goerr v0.1.14 diff --git a/backend/go.sum b/backend/go.sum index 326fe80..9764c05 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -97,6 +97,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4= diff --git a/backend/main.go b/backend/main.go index cf4981b..cc4c6ba 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,11 +1,22 @@ package main import ( + "backend/pkg/config" + "backend/pkg/repository" "backend/web/server" ) var migrationFilePath = "./db/migrations" func main() { - server.StartServer(migrationFilePath) + dbConfig := repository.DBConfig{ + Host: config.Cfg.PGHost, + User: config.Cfg.PGUser, + Password: config.Cfg.PGPassword, + DBName: config.Cfg.PGDBName, + Port: config.Cfg.PGPort, + SSLMode: config.Cfg.PGSSLMode, + MigrationFilePath: migrationFilePath, + } + server.StartServer(dbConfig) } diff --git a/backend/main_test.go b/backend/main_test.go index 8d515a8..2b9a3a2 100644 --- a/backend/main_test.go +++ b/backend/main_test.go @@ -6,6 +6,9 @@ import ( "backend/web/server" "context" "fmt" + "github.com/caarlos0/env/v11" + "github.com/joho/godotenv" + "github.com/m-mizutani/goerr" "net/http" "net/http/httptest" "testing" @@ -13,8 +16,21 @@ import ( "github.com/stretchr/testify/assert" "gorm.io/driver/postgres" "gorm.io/gorm" + "log" ) +func init() { + // Load .env file + if err := godotenv.Load(); err != nil { + log.Println("No .env file found") + } + + // Parse environment variables into the config.Cfg + if err := env.Parse(&config.Cfg); err != nil { + log.Fatalf("Failed to parse environment variables: %+v", err) + } +} + func setupTestDB(t *testing.T) (*httptest.Server, func()) { t.Helper() @@ -34,15 +50,6 @@ func setupTestDB(t *testing.T) (*httptest.Server, func()) { t.Fatalf("failed to run migrations: %+v", err) } - // Override the config values for testing - config.Cfg.PGHost = pg.GetConfig().Host - config.Cfg.PGUser = pg.GetConfig().User - config.Cfg.PGPassword = pg.GetConfig().Password - config.Cfg.PGDBName = pg.GetConfig().DBName - config.Cfg.PGPort = pg.GetConfig().Port - config.Cfg.PGSSLMode = "disable" - config.Cfg.Port = 8080 - // Initialize the Echo router using the NewRouter function e := server.NewRouter(pg.GetDB()) @@ -64,7 +71,7 @@ func setupProdDB() (*gorm.DB, error) { config.Cfg.PGSSLMode) db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { - return nil, err + return nil, goerr.Wrap(err) } return db, nil } @@ -103,6 +110,8 @@ func TestMainSmoke(t *testing.T) { }) t.Run("Test with Production Database", func(t *testing.T) { + // Comment out this when you want to do a smoke test against production database. + t.SkipNow() ts, cleanup := setupProdServer(t) defer cleanup() diff --git a/backend/pkg/config/config.go b/backend/pkg/config/config.go index 75f28f3..0986ef5 100644 --- a/backend/pkg/config/config.go +++ b/backend/pkg/config/config.go @@ -20,7 +20,7 @@ type Config struct { PGPassword string `env:"PG_PASSWORD" envDefault:"testpassword"` PGDBName string `env:"PG_DBNAME" envDefault:"flamingodb"` PGPort string `env:"PG_PORT" envDefault:"5432"` - PGSSLMode string `env:"PG_SSLMODE" envDefault:"disable"` + PGSSLMode string `env:"PG_SSLMODE" envDefault:"allow"` } // Cfg is the package-level variable that holds the parsed configuration diff --git a/backend/web/server/server.go b/backend/web/server/server.go index b0e64fa..e116347 100644 --- a/backend/web/server/server.go +++ b/backend/web/server/server.go @@ -80,17 +80,17 @@ func NewRouter(db *gorm.DB) *echo.Echo { return e } -func StartServer(migrationFilePath string) { +func StartServer(dbConfig repository.DBConfig) { // Initialize the database - dbConfig := repository.DBConfig{ - Host: config.Cfg.PGHost, - User: config.Cfg.PGUser, - Password: config.Cfg.PGPassword, - DBName: config.Cfg.PGDBName, - Port: config.Cfg.PGPort, - SSLMode: config.Cfg.PGSSLMode, - MigrationFilePath: migrationFilePath, - } + //dbConfig := repository.DBConfig{ + // Host: config.Cfg.PGHost, + // User: config.Cfg.PGUser, + // Password: config.Cfg.PGPassword, + // DBName: config.Cfg.PGDBName, + // Port: config.Cfg.PGPort, + // SSLMode: config.Cfg.PGSSLMode, + // MigrationFilePath: migrationFilePath, + //} db := repository.InitializeDatabase(dbConfig) router := NewRouter(db)