mirror of
https://github.com/Dvorinka/Trackeep.git
synced 2026-06-03 20:12:58 +00:00
954a1a1080
- Replace Redis with DragonflyDB for better performance and memory efficiency - Remove redundant environment variables (POSTGRES_*, ENCRYPTION_KEY, OAUTH_SERVICE_URL) - Consolidate database configuration to use single DB_* variables - Use JWT_SECRET for both JWT tokens and encryption - Remove PORT variable redundancy, use BACKEND_PORT consistently - Clean up docker-compose configurations for dev/prod consistency - Add DragonflyDB configuration with optimized memory usage - Remove redis.conf as it's no longer needed - Update health checks to use Redis-compatible CLI for DragonflyDB - Add missing VITE_API_URL to production frontend - Fix GitHub Actions to use correct go.sum path - Clean up development directories and unused files
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/trackeep/backend/migrations"
|
|
"go.uber.org/zap"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var DB *gorm.DB
|
|
|
|
// JWTSecret for signing tokens
|
|
var JWTSecret = getJWTSecret()
|
|
|
|
// getJWTSecret retrieves JWT secret from environment or uses a default
|
|
func getJWTSecret() string {
|
|
if secret := os.Getenv("JWT_SECRET"); secret != "" {
|
|
return secret
|
|
}
|
|
// Default secret for development (should be changed in production)
|
|
return "your-secret-key-change-in-production"
|
|
}
|
|
|
|
// InitDatabase initializes the database connection
|
|
func InitDatabase() {
|
|
// Initialize logger first
|
|
InitLogger()
|
|
logger := GetLogger()
|
|
|
|
// Check if demo mode is enabled
|
|
if os.Getenv("VITE_DEMO_MODE") == "true" {
|
|
logger.Info("Demo mode enabled - skipping database initialization")
|
|
return
|
|
}
|
|
|
|
var err error
|
|
|
|
// Configure GORM
|
|
gormConfig := &gorm.Config{}
|
|
|
|
dbType := os.Getenv("DB_TYPE")
|
|
if dbType == "" {
|
|
dbType = "postgres" // Always use PostgreSQL
|
|
}
|
|
|
|
switch dbType {
|
|
case "postgres":
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
|
|
os.Getenv("DB_HOST"),
|
|
os.Getenv("DB_USER"),
|
|
os.Getenv("DB_PASSWORD"),
|
|
os.Getenv("DB_NAME"),
|
|
os.Getenv("DB_PORT"),
|
|
os.Getenv("DB_SSL_MODE"),
|
|
)
|
|
DB, err = gorm.Open(postgres.Open(dsn), gormConfig)
|
|
logger.Info("Using PostgreSQL database")
|
|
default:
|
|
logger.Fatal("Unsupported database type", zap.String("type", dbType))
|
|
}
|
|
|
|
if err != nil {
|
|
logger.Fatal("Failed to connect to database", zap.Error(err))
|
|
}
|
|
|
|
logger.Info("Database connected successfully")
|
|
|
|
// Run database migrations
|
|
if err := migrations.RunMigrations(); err != nil {
|
|
logger.Fatal("Failed to run database migrations", zap.Error(err))
|
|
}
|
|
}
|
|
|
|
// GetDB returns the database instance
|
|
func GetDB() *gorm.DB {
|
|
// In demo mode, return nil since no database is available
|
|
if os.Getenv("VITE_DEMO_MODE") == "true" {
|
|
return nil
|
|
}
|
|
return DB
|
|
}
|