package config import ( "fmt" _ "github.com/joho/godotenv/autoload" "log" "os" "time" ) type Config struct { AppEnv string DbUrl string ServerPort string JWTSecret string JWTExpiry time.Duration RefreshExpiry time.Duration } func Load() Config { var dbUrl string { username := getEnv("DB_USERNAME") password := getEnv("DB_PASSWORD") name := getEnv("DB_NAME") port := getEnv("DB_PORT") host := getEnv("DB_HOST") dbUrl = fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable", username, password, host, port, name) } appEnv := os.Getenv("APP_ENV") if appEnv == "" { appEnv = "development" } jwtSecret := getEnv("JWT_SECRET") if jwtSecret == "" { log.Fatal("JWT_SECRET env variable is required") } cfg := Config{ AppEnv: appEnv, DbUrl: dbUrl, ServerPort: "8080", JWTSecret: jwtSecret, JWTExpiry: time.Minute * 15, RefreshExpiry: time.Hour * 24 * 7, } return cfg } func getEnv(key string) string { v := os.Getenv(key) if v == "" { log.Fatalf("The env variable %v is not defined and the application can not operate without\n", key) } return v } func Provide() *Config { cfg := Load() return &cfg }