Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(API) error message for empty environment variables #1260

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func main() {
if err != nil {
log.Println("env file is not found, continuing without .env file")
}
config := utils.LoadEnvVars()
config, err := utils.LoadEnvVars()
if err != nil {
panic(err)
}

appConfig, err := utils.Setup(version)
if err != nil {
Expand Down
54 changes: 43 additions & 11 deletions api/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func Setup(options ...string) (AppConfig, error) {
version = "test"
}

config := LoadEnvVars()
config, err := LoadEnvVars()
if err != nil {
return appConfig, err
}
// pgsql connect
pgxPool, pgxError := pgxpool.New(context.Background(), config["DATABASE_URL"].(string))
if pgxError != nil {
Expand Down Expand Up @@ -189,7 +192,10 @@ func Setup(options ...string) (AppConfig, error) {
}

func EncryptText(textToEncrypt string) (string, error) {
config := LoadEnvVars()
config, err := LoadEnvVars()
if err != nil {
return "", err
}
password := config["ENCRYPT_PASSWORD"].(string)
// Generate a random 16-byte IV
iv := make([]byte, 16)
Expand Down Expand Up @@ -221,7 +227,10 @@ func EncryptText(textToEncrypt string) (string, error) {
}

func DecryptText(encryptedText string) (string, error) {
config := LoadEnvVars()
config, err := LoadEnvVars()
if err != nil {
return "", err
}
password := config["ENCRYPT_PASSWORD"].(string)

// Extract the IV and ciphertext from the string
Expand Down Expand Up @@ -254,15 +263,38 @@ func DecryptText(encryptedText string) (string, error) {
return string(decryptedText), nil
}

func LoadEnvVars() map[string]interface{} {
return map[string]interface{}{
"DATABASE_URL": os.Getenv("DATABASE_URL"),
"REDIS_HOST": os.Getenv("REDIS_HOST"),
"REDIS_PORT": os.Getenv("REDIS_PORT"),
"APP_PORT": os.Getenv("APP_PORT"),
"TEST_MODE": os.Getenv("TEST_MODE"),
"ENCRYPT_PASSWORD": os.Getenv("ENCRYPT_PASSWORD"),
func LoadEnvVars() (map[string]interface{}, error) {
databaseURL := os.Getenv("DATABASE_URL")
redisHost := os.Getenv("REDIS_HOST")
Copy link
Member

@martinkersner martinkersner Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be nice to have default values for REDIS_HOST and REDIS_PORT as localhost and 6379 respectively.

redisPort := os.Getenv("REDIS_PORT")
appPort := os.Getenv("APP_PORT")
testMode := os.Getenv("TEST_MODE")
encryptPassword := os.Getenv("ENCRYPT_PASSWORD")

if databaseURL == "" {
return nil, errors.New("DATABASE_URL is not set")
}
if redisHost == "" {
return nil, errors.New("REDIS_HOST is not set")
}
if redisPort == "" {
return nil, errors.New("REDIS_PORT is not set")
}
if appPort == "" {
appPort = "3000"
}
if encryptPassword == "" {
return nil, errors.New("ENCRYPT_PASSWORD is not set")
}

return map[string]interface{}{
"DATABASE_URL": databaseURL,
"REDIS_HOST": redisHost,
"REDIS_PORT": redisPort,
"APP_PORT": appPort,
"TEST_MODE": testMode,
"ENCRYPT_PASSWORD": encryptPassword,
}, nil
}

func CustomErrorHandler(c *fiber.Ctx, err error) error {
Expand Down
Loading