-
Notifications
You must be signed in to change notification settings - Fork 3
refactor(config): split config.go and rename fields to camelCase #157
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
|
|
||
| "github.com/joho/godotenv" | ||
| ) | ||
|
|
||
| func loadEnvFile() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not be using files for environment variable? Why is this function present? |
||
| if os.Getenv("APP_ENV") == "production" { | ||
| return | ||
| } | ||
|
|
||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| log.Fatalf("failed to determine current working directory: %v", err) | ||
| } | ||
|
|
||
| envPath, err := findEnvFile(cwd) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| log.Println("config: no .env file found - environment variables used as-is") | ||
| return | ||
| } | ||
| log.Fatalf("failed to locate .env file: %v", err) | ||
| } | ||
|
|
||
| if err := godotenv.Load(envPath); err != nil { | ||
| log.Fatalf("failed to load .env file %q: %v", envPath, err) | ||
| } | ||
|
|
||
| log.Printf("config: env loaded from %s", envPath) | ||
| } | ||
|
|
||
| func findEnvFile(startDir string) (string, error) { | ||
| dir := startDir | ||
| for { | ||
| candidate := filepath.Join(dir, ".env") | ||
| info, err := os.Stat(candidate) | ||
| if err == nil && !info.IsDir() { | ||
| return candidate, nil | ||
| } | ||
| if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
| return "", err | ||
| } | ||
|
|
||
| parent := filepath.Dir(dir) | ||
| if parent == dir { | ||
| break | ||
| } | ||
| dir = parent | ||
| } | ||
|
|
||
| return "", os.ErrNotExist | ||
| } | ||
|
|
||
| func postgresPortFromEnv() (int, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did not get the point of this function. Why do we need a function to get port of postgres? |
||
| rawPort := os.Getenv("POSTGRES_PORT") | ||
| if rawPort == "" { | ||
| return 5432, nil | ||
| } | ||
|
|
||
| port, err := strconv.Atoi(rawPort) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("invalid POSTGRES_PORT %q: must be numeric", rawPort) | ||
| } | ||
| if port <= 0 { | ||
| return 0, fmt.Errorf("invalid POSTGRES_PORT %q: must be greater than 0", rawPort) | ||
| } | ||
|
|
||
| return port, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| func validateDatabaseConfig(cfg AppConfig) error { | ||
| missing := make([]string, 0, 4) | ||
|
|
||
| if cfg.PostgresUser == "" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are if statements the only thing we can do? |
||
| missing = append(missing, "POSTGRES_USER") | ||
| } | ||
| if cfg.PostgresPassword == "" { | ||
| missing = append(missing, "POSTGRES_PASSWORD") | ||
| } | ||
| if cfg.PostgresDB == "" { | ||
| missing = append(missing, "POSTGRES_DB") | ||
| } | ||
| if cfg.PostgresHost == "" { | ||
| missing = append(missing, "POSTGRES_HOST") | ||
| } | ||
| if len(missing) > 0 { | ||
| return fmt.Errorf("missing required database config: %s", strings.Join(missing, ", ")) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is camel case related to some go conventions? Usually constants are defined as snake case capital letters