aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/config/config.go
diff options
context:
space:
mode:
authorLibravatarLibravatar Biswa Kalyan Bhuyan <biswa@surgot.in> 2025-04-24 08:18:27 +0530
committerLibravatarLibravatar Biswa Kalyan Bhuyan <biswa@surgot.in> 2025-04-24 08:18:27 +0530
commit50d5e6534f5e593297a09323e683c7c8b850117b (patch)
tree339d6e8b123c5d4caa4129971e2cb1b960b12a89 /backend/internal/config/config.go
parent76066679b5bdab53419492066c4e80d2ed3be518 (diff)
downloadfinance-50d5e6534f5e593297a09323e683c7c8b850117b.tar.gz
finance-50d5e6534f5e593297a09323e683c7c8b850117b.tar.bz2
finance-50d5e6534f5e593297a09323e683c7c8b850117b.zip
feat: added basic backend features to it
- Set up API framework (Gin Gonic) - Set up ORM/DB library (GORM) - Design database schema (Users, Accounts, Transactions, Loans, Goals) - Set up database connection and migrations
Diffstat (limited to 'backend/internal/config/config.go')
-rw-r--r--backend/internal/config/config.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go
new file mode 100644
index 0000000..f61f423
--- /dev/null
+++ b/backend/internal/config/config.go
@@ -0,0 +1,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
+}