
- Add database configuration with environment variable support - Support both PostgreSQL and SQLite database drivers - Implement connection pooling and timeout configuration - Add automatic database migration system - Include connection health checks and error handling - Configure SSL mode and connection parameters - Add utility functions for environment variable management
104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"git.secnex.io/secnex/certman/config"
|
|
"git.secnex.io/secnex/certman/models"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
// DB is the global database instance
|
|
var DB *gorm.DB
|
|
|
|
// Connect establishes database connection and runs migrations
|
|
func Connect() error {
|
|
config := config.GetConfig()
|
|
|
|
var err error
|
|
var dsn string
|
|
|
|
switch config.Driver {
|
|
case "postgres":
|
|
dsn = fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
|
config.Host, config.Port, config.User, config.Password, config.DBName, config.SSLMode)
|
|
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Info),
|
|
})
|
|
case "sqlite":
|
|
dsn = config.DBName + ".db"
|
|
DB, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Info),
|
|
})
|
|
default:
|
|
return fmt.Errorf("unsupported database driver: %s", config.Driver)
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to database: %w", err)
|
|
}
|
|
|
|
// Configure connection pool
|
|
sqlDB, err := DB.DB()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
|
}
|
|
|
|
// Set connection pool settings
|
|
sqlDB.SetMaxIdleConns(10)
|
|
sqlDB.SetMaxOpenConns(100)
|
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
|
|
|
log.Printf("Successfully connected to %s database", config.Driver)
|
|
|
|
// Run migrations
|
|
if err := Migrate(); err != nil {
|
|
return fmt.Errorf("failed to run migrations: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Migrate runs database migrations for all models
|
|
func Migrate() error {
|
|
log.Println("🚀 Running database migrations...")
|
|
|
|
err := DB.AutoMigrate(
|
|
&models.CertificateAuthority{},
|
|
&models.Certificate{},
|
|
&models.CertificateRequest{},
|
|
&models.CertificateRevocationList{},
|
|
&models.Organization{},
|
|
&models.User{},
|
|
)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("migration failed: %w", err)
|
|
}
|
|
|
|
log.Println("✅ Database migrations completed successfully")
|
|
return nil
|
|
}
|
|
|
|
// Close closes the database connection
|
|
func Close() error {
|
|
if DB != nil {
|
|
sqlDB, err := DB.DB()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
|
}
|
|
return sqlDB.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetDB returns the database instance
|
|
func GetDB() *gorm.DB {
|
|
return DB
|
|
}
|