aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/config/config.go
blob: f61f4232a93f7f618d82836c32dc606455a8fd6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package config

import (
	"log"
	"os"

	"github.com/joho/godotenv"
)

// Config holds the application configuration
type Config struct {
	DatabaseDSN string
	JWTSecret   string // Secret key for signing JWTs
	// Add other config fields here later (e.g., server port)
}

// LoadConfig loads configuration from environment variables or a .env file
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")
	}

	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
	}

	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")
	}

	return &Config{
		DatabaseDSN: dsn,
		JWTSecret:   jwtSecret,
	}, nil
}