Skip to content

Commit

Permalink
refactor: replace string literal env var keys with static consts
Browse files Browse the repository at this point in the history
  • Loading branch information
HamoonZamiri committed Aug 2, 2024
1 parent 40c3d05 commit e69437f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
14 changes: 14 additions & 0 deletions backend/config/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config

// Constant keys representing environment variables to avoid mismatch
const (
DB_PASSWORD string = "DB_PASSWORD"
DB_USER string = "DB_USER"
DB_NAME string = "DB_NAME"
JWT_SECRET string = "JWT_SECRET"
PORT string = "PORT"
TEST_DB_CONN_STRING string = "TEST_DB_CONN_STRING"
CI string = "CI"
TEST_DB_NAME string = "TEST_DB_NAME"
ENV string = "ENV"
)
8 changes: 4 additions & 4 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func Run() error {
var dbInstance *sqlx.DB

if configService.MustGetEnv("ENV") == "test" {
dbInstance, _ = db.NewWithConnString(configService.MustGetEnv("TEST_DB_CONN_STRING"))
dbInstance, _ = db.NewWithConnString(configService.MustGetEnv(config.TEST_DB_CONN_STRING))
} else {
dbInstance, _ = db.New(configService.MustGetEnv("DB_NAME"), configService.MustGetEnv("DB_PASSWORD"),
configService.MustGetEnv("DB_NAME"))
dbInstance, _ = db.New(configService.MustGetEnv(config.DB_NAME), configService.MustGetEnv(config.DB_PASSWORD),
configService.MustGetEnv(config.DB_NAME))
}
if dbInstance == nil {
panic("db instance is nil")
Expand All @@ -64,7 +64,7 @@ func Run() error {
goalHandler := gh.NewGoalHandler(goalService, goalDomainLogger)

srv := NewServer(userHandler, goalHandler, configService)
port := configService.MustGetEnv("PORT")
port := configService.MustGetEnv(config.PORT)
httpServer := &http.Server{
Addr: ":" + port,
Handler: srv,
Expand Down
10 changes: 5 additions & 5 deletions backend/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func TestMain(m *testing.M) {

var err error
configService = config.NewConfigService(options.None[string]())
configService.SetEnv("ENV", "test")
dbName := configService.MustGetEnv("TEST_DB_NAME")
dbUser := configService.MustGetEnv("DB_USER")
dbPassword := configService.MustGetEnv("DB_PASSWORD")
configService.SetEnv(config.ENV, "test")
dbName := configService.MustGetEnv(config.TEST_DB_NAME)
dbUser := configService.MustGetEnv(config.DB_USER)
dbPassword := configService.MustGetEnv(config.DB_PASSWORD)

pgContainer, err := postgres.Run(ctx, "docker.io/postgres:16-alpine",
postgres.WithDatabase(dbName),
Expand All @@ -123,7 +123,7 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
configService.SetEnv("TEST_DB_CONN_STRING", connStr)
configService.SetEnv(config.TEST_DB_CONN_STRING, connStr)

dbx, err = db.NewWithConnString(connStr)
if err != nil {
Expand Down

0 comments on commit e69437f

Please sign in to comment.