init go project

This commit is contained in:
juancwu 2025-10-10 08:14:33 -04:00
commit 5dde43e409
85 changed files with 16720 additions and 0 deletions

76
internal/config/config.go Normal file
View file

@ -0,0 +1,76 @@
package config
import (
"log/slog"
"os"
"time"
"github.com/joho/godotenv"
)
type Config struct {
AppName string
AppEnv string
AppURL string
Host string
Port string
DBDriver string
DBConnection string
JWTSecret string
JWTExpiry time.Duration
}
func Load() *Config {
if err := godotenv.Load(); err != nil {
slog.Info("no .env file found, using environment variables")
}
cfg := &Config{
AppName: envString("APP_NAME", "Budgething"),
AppEnv: envRequired("APP_ENV"),
AppURL: envRequired("APP_URL"),
Host: envString("HOST", "127.0.0.1"),
Port: envString("PORT", "9000"),
DBDriver: envString("DB_DRIVER", "sqlite"),
DBConnection: envString("DB_CONNECTION", "./data/local.db?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)"),
JWTSecret: envRequired("JWT_SECRET"),
JWTExpiry: envDuration("JWT_EXPIRY", 168*time.Hour), // 7 days default
}
return cfg
}
func envString(key, def string) string {
value := os.Getenv(key)
if value == "" {
value = def
}
return value
}
func envDuration(key string, def time.Duration) time.Duration {
value, ok := os.LookupEnv(key)
if !ok || value == "" {
return def
}
duration, err := time.ParseDuration(value)
if err != nil {
slog.Warn("config invalid duration, using default", "key", key, "value", value, "default", def)
return def
}
return duration
}
func envRequired(key string) string {
if value := os.Getenv(key); value != "" {
return value
}
slog.Error("config required env var missing", "key", key)
os.Exit(1)
return ""
}