146 lines
4.6 KiB
Go
146 lines
4.6 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
Env string
|
|
|
|
// PostgreSQL (individual params matching docker-compose & existing .env)
|
|
DBHost string
|
|
DBPort int
|
|
DBUser string
|
|
DBPassword string
|
|
DBName string
|
|
|
|
// Auth
|
|
JWTSecret string
|
|
|
|
// Public URL (used in emails & push links; runtime env)
|
|
AppURL string
|
|
|
|
// Seed
|
|
RunSeed bool
|
|
SeedAdminEmail string
|
|
SeedAdminPassword string
|
|
SeedAdminName string
|
|
SeedAsesorEmail string
|
|
SeedAsesorPass string
|
|
SeedAsesorName string
|
|
|
|
// MiniMax AI (Anthropic-compatible endpoint)
|
|
MinimaxAPIKey string
|
|
MinimaxBaseURL string
|
|
MinimaxModel string
|
|
|
|
// Web Push (VAPID)
|
|
VAPIDPublicKey string
|
|
VAPIDPrivateKey string
|
|
VAPIDSubject string
|
|
|
|
// SMTP
|
|
SMTPHost string
|
|
SMTPPort int
|
|
SMTPUser string
|
|
SMTPPass string
|
|
SMTPFrom string
|
|
SMTPFromName string
|
|
|
|
// Cobros & Uploads
|
|
CobrosRecordatorioDias int
|
|
UploadDir string
|
|
|
|
// Notifications & Webhooks
|
|
AdminNotifyEmail string
|
|
WebhookN8NURL string
|
|
WebhookN8NSecret string
|
|
WebhookWhatsAppSendURL string
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
_ = godotenv.Load()
|
|
|
|
dbName := firstNonEmpty(os.Getenv("DB_DATABASE"), os.Getenv("DB_NAME"), "cotizador_e3")
|
|
dbPort := parseIntDefault(os.Getenv("DB_PORT"), 5432)
|
|
smtpPort := parseIntDefault(os.Getenv("SMTP_PORT"), 587)
|
|
recDias := parseIntDefault(os.Getenv("COBROS_RECORDATORIO_DIAS"), 3)
|
|
|
|
cfg := &Config{
|
|
Port: firstNonEmpty(os.Getenv("PORT"), "3000"),
|
|
Env: firstNonEmpty(os.Getenv("NODE_ENV"), os.Getenv("APP_ENV"), "production"),
|
|
DBHost: firstNonEmpty(os.Getenv("DB_HOST"), "localhost"),
|
|
DBPort: dbPort,
|
|
DBUser: firstNonEmpty(os.Getenv("DB_USER"), "postgres"),
|
|
DBPassword: firstNonEmpty(os.Getenv("DB_PASSWORD"), "postgres"),
|
|
DBName: dbName,
|
|
JWTSecret: strings.TrimSpace(os.Getenv("JWT_SECRET")),
|
|
AppURL: strings.TrimRight(firstNonEmpty(os.Getenv("APP_URL"), "http://localhost:3000"), "/"),
|
|
RunSeed: strings.EqualFold(strings.TrimSpace(os.Getenv("RUN_SEED")), "true"),
|
|
SeedAdminEmail: firstNonEmpty(os.Getenv("SEED_ADMIN_EMAIL"), "[email protected]"),
|
|
SeedAdminPassword: os.Getenv("SEED_ADMIN_PASSWORD"),
|
|
SeedAdminName: firstNonEmpty(os.Getenv("SEED_ADMIN_NAME"), "Administrador"),
|
|
SeedAsesorEmail: strings.TrimSpace(os.Getenv("SEED_ASESOR_EMAIL")),
|
|
SeedAsesorPass: os.Getenv("SEED_ASESOR_PASSWORD"),
|
|
SeedAsesorName: firstNonEmpty(os.Getenv("SEED_ASESOR_NAME"), "Asesor"),
|
|
MinimaxAPIKey: strings.TrimSpace(os.Getenv("MINIMAX_API_KEY")),
|
|
MinimaxBaseURL: strings.TrimRight(firstNonEmpty(os.Getenv("MINIMAX_BASE_URL"), "https://api.minimax.io/anthropic"), "/"),
|
|
MinimaxModel: firstNonEmpty(os.Getenv("MINIMAX_MODEL"), "MiniMax-M3"),
|
|
VAPIDPublicKey: strings.TrimSpace(os.Getenv("VAPID_PUBLIC_KEY")),
|
|
VAPIDPrivateKey: strings.TrimSpace(os.Getenv("VAPID_PRIVATE_KEY")),
|
|
VAPIDSubject: firstNonEmpty(os.Getenv("VAPID_SUBJECT"), "mailto:[email protected]"),
|
|
SMTPHost: strings.TrimSpace(os.Getenv("SMTP_HOST")),
|
|
SMTPPort: smtpPort,
|
|
SMTPUser: strings.TrimSpace(os.Getenv("SMTP_USER")),
|
|
SMTPPass: os.Getenv("SMTP_PASS"),
|
|
SMTPFrom: firstNonEmpty(os.Getenv("SMTP_FROM"), os.Getenv("SMTP_USER")),
|
|
SMTPFromName: firstNonEmpty(os.Getenv("SMTP_FROM_NAME"), "Uriel Jareth Consulting"),
|
|
CobrosRecordatorioDias: recDias,
|
|
UploadDir: firstNonEmpty(os.Getenv("UPLOAD_DIR"), "./uploads"),
|
|
AdminNotifyEmail: strings.TrimSpace(os.Getenv("ADMIN_NOTIFY_EMAIL")),
|
|
WebhookN8NURL: strings.TrimSpace(os.Getenv("WEBHOOK_N8N_URL")),
|
|
WebhookN8NSecret: strings.TrimSpace(os.Getenv("WEBHOOK_N8N_SECRET")),
|
|
WebhookWhatsAppSendURL: strings.TrimSpace(os.Getenv("WEBHOOK_WHATSAPP_SEND_URL")),
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func (c *Config) RequireJWTSecret() error {
|
|
if c.JWTSecret == "" {
|
|
return fmt.Errorf("JWT_SECRET es obligatorio y no esta configurado")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) IsProduction() bool {
|
|
return strings.EqualFold(c.Env, "production")
|
|
}
|
|
|
|
func firstNonEmpty(vals ...string) string {
|
|
for _, v := range vals {
|
|
if strings.TrimSpace(v) != "" {
|
|
return strings.TrimSpace(v)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func parseIntDefault(s string, def int) int {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return def
|
|
}
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|