aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/config
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/config')
-rw-r--r--backend/internal/config/config.go88
1 files changed, 54 insertions, 34 deletions
diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go
index f61f423..f302827 100644
--- a/backend/internal/config/config.go
+++ b/backend/internal/config/config.go
@@ -1,54 +1,74 @@
package config
import (
- "log"
+ "fmt"
"os"
"github.com/joho/godotenv"
)
-// Config holds the application configuration
+// Config holds application configuration
type Config struct {
+ // Database configuration
DatabaseDSN string
- JWTSecret string // Secret key for signing JWTs
- // Add other config fields here later (e.g., server port)
+
+ // JWT configuration
+ JWTSecret string
+ JWTExpiry int // in hours
+
+ // Server configuration
+ ServerPort int
+ ServerHost string
+
+ // Environment (development, staging, production)
+ Environment string
}
-// LoadConfig loads configuration from environment variables or a .env file
+// LoadConfig loads the application configuration from environment variables
func LoadConfig() (*Config, error) {
- // Attempt to load .env file (useful for development)
- // In production, rely on environment variables set directly
- err := godotenv.Load() // Load .env file from current directory or parent dirs
- if err != nil {
- log.Println("No .env file found, relying on environment variables")
+ // Load .env file if it exists
+ _ = godotenv.Load() // Ignore error if .env file doesn't exist
+
+ // Set defaults and override with environment variables
+ config := &Config{
+ DatabaseDSN: getEnv("DATABASE_DSN", "postgresql://postgres:postgres@localhost:5432/finance?sslmode=disable"),
+ JWTSecret: getEnv("JWT_SECRET", "supersecretkey"), // This should be changed in production
+ JWTExpiry: getEnvInt("JWT_EXPIRY", 24), // Default to 24 hours
+ ServerPort: getEnvInt("SERVER_PORT", 8080), // Default to 8080
+ ServerHost: getEnv("SERVER_HOST", "0.0.0.0"), // Default to all interfaces
+ Environment: getEnv("APP_ENV", "development"), // Default to development
}
- dsn := os.Getenv("DATABASE_DSN")
- if dsn == "" {
- // Set a default for local development if not provided
- log.Println("DATABASE_DSN not set, using default local PostgreSQL DSN")
- log.Println("IMPORTANT: Ensure PostgreSQL is running with the correct credentials.")
- log.Println("Try creating a database 'finance' and setting up a user with the correct permissions.")
- log.Println("Example commands:")
- log.Println(" createdb finance")
- log.Println(" createuser -P -s -e user_name")
-
- // Use more common/generic default credentials
- dsn = "host=localhost user=postgres password=postgres dbname=finance port=5432 sslmode=disable TimeZone=UTC"
- // Consider making the default conditional or removing it for stricter environments
+ return config, nil
+}
+
+// getEnv gets an environment variable or returns a default value
+func getEnv(key, defaultValue string) string {
+ value := os.Getenv(key)
+ if value == "" {
+ return defaultValue
+ }
+ return value
+}
+
+// getEnvInt gets an environment variable as an integer or returns a default value
+func getEnvInt(key string, defaultValue int) int {
+ value := os.Getenv(key)
+ if value == "" {
+ return defaultValue
}
- jwtSecret := os.Getenv("JWT_SECRET")
- if jwtSecret == "" {
- log.Println("WARNING: JWT_SECRET environment variable not set. Using an insecure default.")
- // !!! IMPORTANT: Use a strong, randomly generated secret in production !!!
- // For development only:
- jwtSecret = "insecure-default-dev-secret-change-me"
- // return nil, errors.New("JWT_SECRET environment variable is required")
+ intValue, err := parseInt(value)
+ if err != nil {
+ return defaultValue
}
- return &Config{
- DatabaseDSN: dsn,
- JWTSecret: jwtSecret,
- }, nil
+ return intValue
+}
+
+// parseInt parses a string to an int
+func parseInt(value string) (int, error) {
+ intValue := 0
+ _, err := fmt.Sscanf(value, "%d", &intValue)
+ return intValue, err
}