Perbaikan template go
This commit is contained in:
@@ -7,109 +7,574 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// Service represents a service that interacts with a database.
|
||||
type Service interface {
|
||||
// Health returns a map of health status information.
|
||||
// The keys and values in the map are service-specific.
|
||||
Health() map[string]string
|
||||
// DatabaseType represents supported database types
|
||||
type DatabaseType string
|
||||
|
||||
// Close terminates the database connection.
|
||||
// It returns an error if the connection cannot be closed.
|
||||
const (
|
||||
Postgres DatabaseType = "postgres"
|
||||
MySQL DatabaseType = "mysql"
|
||||
SQLServer DatabaseType = "sqlserver"
|
||||
SQLite DatabaseType = "sqlite"
|
||||
MongoDB DatabaseType = "mongodb"
|
||||
)
|
||||
|
||||
// DatabaseConfig represents configuration for a single database connection
|
||||
type DatabaseConfig struct {
|
||||
Name string
|
||||
Type DatabaseType
|
||||
Host string
|
||||
Port string
|
||||
Database string
|
||||
Username string
|
||||
Password string
|
||||
Schema string
|
||||
SSLMode string
|
||||
Path string // For SQLite
|
||||
Options string // Additional connection options
|
||||
}
|
||||
|
||||
// Service represents a service that interacts with multiple databases
|
||||
type Service interface {
|
||||
// Health returns health status for all databases
|
||||
Health() map[string]map[string]string
|
||||
|
||||
// GetDB returns a specific SQL database connection by name
|
||||
GetDB(name string) (*sql.DB, error)
|
||||
|
||||
// GetMongoClient returns a specific MongoDB client by name
|
||||
GetMongoClient(name string) (*mongo.Client, error)
|
||||
|
||||
// Close terminates all database connections
|
||||
Close() error
|
||||
|
||||
// ListDBs returns list of available database names
|
||||
ListDBs() []string
|
||||
|
||||
// GetDBType returns the type of a specific database
|
||||
GetDBType(name string) (DatabaseType, error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
db *sql.DB
|
||||
sqlDatabases map[string]*sql.DB
|
||||
mongoClients map[string]*mongo.Client
|
||||
configs map[string]DatabaseConfig
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
var (
|
||||
database = os.Getenv("BLUEPRINT_DB_DATABASE")
|
||||
password = os.Getenv("BLUEPRINT_DB_PASSWORD")
|
||||
username = os.Getenv("BLUEPRINT_DB_USERNAME")
|
||||
port = os.Getenv("BLUEPRINT_DB_PORT")
|
||||
host = os.Getenv("BLUEPRINT_DB_HOST")
|
||||
schema = os.Getenv("BLUEPRINT_DB_SCHEMA")
|
||||
dbInstance *service
|
||||
dbManager *service
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// New creates a new database service with multiple connections
|
||||
func New() Service {
|
||||
// Reuse Connection
|
||||
if dbInstance != nil {
|
||||
return dbInstance
|
||||
once.Do(func() {
|
||||
dbManager = &service{
|
||||
sqlDatabases: make(map[string]*sql.DB),
|
||||
mongoClients: make(map[string]*mongo.Client),
|
||||
configs: make(map[string]DatabaseConfig),
|
||||
}
|
||||
|
||||
// Load database configurations from environment
|
||||
configs := loadDatabaseConfigs()
|
||||
|
||||
// Initialize all database connections
|
||||
for _, config := range configs {
|
||||
if err := dbManager.addDatabase(config); err != nil {
|
||||
log.Printf("Failed to connect to database %s: %v", config.Name, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return dbManager
|
||||
}
|
||||
|
||||
// loadDatabaseConfigs loads database configurations from environment variables
|
||||
func loadDatabaseConfigs() []DatabaseConfig {
|
||||
var configs []DatabaseConfig
|
||||
|
||||
// Load configurations from environment
|
||||
// Format: DB_{NAME}_{PROPERTY}
|
||||
|
||||
// Check for DB_ prefixed configurations
|
||||
envVars := os.Environ()
|
||||
dbConfigs := make(map[string]map[string]string)
|
||||
|
||||
for _, envVar := range envVars {
|
||||
parts := strings.SplitN(envVar, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := parts[0]
|
||||
value := parts[1]
|
||||
|
||||
if strings.HasPrefix(key, "DB_") {
|
||||
segments := strings.Split(key, "_")
|
||||
if len(segments) >= 3 {
|
||||
dbName := strings.ToLower(segments[1])
|
||||
property := strings.ToLower(strings.Join(segments[2:], "_"))
|
||||
|
||||
if dbConfigs[dbName] == nil {
|
||||
dbConfigs[dbName] = make(map[string]string)
|
||||
}
|
||||
dbConfigs[dbName][property] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable&search_path=%s", username, password, host, port, database, schema)
|
||||
|
||||
// Convert map to DatabaseConfig structs
|
||||
for name, config := range dbConfigs {
|
||||
dbType := DatabaseType(getEnvFromMap(config, "type", "postgres"))
|
||||
|
||||
dbConfig := DatabaseConfig{
|
||||
Name: name,
|
||||
Type: dbType,
|
||||
Host: getEnvFromMap(config, "host", "localhost"),
|
||||
Port: getEnvFromMap(config, "port", getDefaultPort(dbType)),
|
||||
Database: getEnvFromMap(config, "database", name),
|
||||
Username: getEnvFromMap(config, "username", ""),
|
||||
Password: getEnvFromMap(config, "password", ""),
|
||||
Schema: getEnvFromMap(config, "schema", ""),
|
||||
SSLMode: getEnvFromMap(config, "sslmode", "disable"),
|
||||
Path: getEnvFromMap(config, "path", ""),
|
||||
Options: getEnvFromMap(config, "options", ""),
|
||||
}
|
||||
|
||||
configs = append(configs, dbConfig)
|
||||
}
|
||||
|
||||
// If no configurations found, use default
|
||||
if len(configs) == 0 {
|
||||
configs = []DatabaseConfig{
|
||||
{
|
||||
Name: "primary",
|
||||
Type: Postgres,
|
||||
Host: getEnv("DB_PRIMARY_HOST", "localhost"),
|
||||
Port: getEnv("DB_PRIMARY_PORT", "5432"),
|
||||
Database: getEnv("DB_PRIMARY_DATABASE", "blueprint"),
|
||||
Username: getEnv("DB_PRIMARY_USERNAME", "postgres"),
|
||||
Password: getEnv("DB_PRIMARY_PASSWORD", ""),
|
||||
Schema: getEnv("DB_PRIMARY_SCHEMA", "public"),
|
||||
SSLMode: getEnv("DB_PRIMARY_SSLMODE", "disable"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return configs
|
||||
}
|
||||
|
||||
// getEnvFromMap helper function
|
||||
func getEnvFromMap(config map[string]string, key, defaultValue string) string {
|
||||
if value, exists := config[key]; exists {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// getEnv helper function
|
||||
func getEnv(key, defaultValue string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// getDefaultPort returns default port for database type
|
||||
func getDefaultPort(dbType DatabaseType) string {
|
||||
switch dbType {
|
||||
case Postgres:
|
||||
return "5432"
|
||||
case MySQL:
|
||||
return "3306"
|
||||
case SQLServer:
|
||||
return "1433"
|
||||
case MongoDB:
|
||||
return "27017"
|
||||
case SQLite:
|
||||
return ""
|
||||
default:
|
||||
return "5432"
|
||||
}
|
||||
}
|
||||
|
||||
// addDatabase adds a new database connection
|
||||
func (s *service) addDatabase(config DatabaseConfig) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
switch config.Type {
|
||||
case Postgres:
|
||||
return s.addPostgres(config)
|
||||
case MySQL:
|
||||
return s.addMySQL(config)
|
||||
case SQLServer:
|
||||
return s.addSQLServer(config)
|
||||
case SQLite:
|
||||
return s.addSQLite(config)
|
||||
case MongoDB:
|
||||
return s.addMongoDB(config)
|
||||
default:
|
||||
return fmt.Errorf("unsupported database type: %s", config.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// addPostgres adds PostgreSQL connection
|
||||
func (s *service) addPostgres(config DatabaseConfig) error {
|
||||
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s",
|
||||
config.Username,
|
||||
config.Password,
|
||||
config.Host,
|
||||
config.Port,
|
||||
config.Database,
|
||||
config.SSLMode,
|
||||
)
|
||||
|
||||
if config.Schema != "" {
|
||||
connStr += "&search_path=" + config.Schema
|
||||
}
|
||||
|
||||
db, err := sql.Open("pgx", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return fmt.Errorf("failed to open PostgreSQL connection: %w", err)
|
||||
}
|
||||
dbInstance = &service{
|
||||
db: db,
|
||||
}
|
||||
return dbInstance
|
||||
|
||||
return s.configureSQLDB(config.Name, db)
|
||||
}
|
||||
|
||||
// Health checks the health of the database connection by pinging the database.
|
||||
// It returns a map with keys indicating various health statistics.
|
||||
func (s *service) Health() map[string]string {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
// addMySQL adds MySQL connection
|
||||
func (s *service) addMySQL(config DatabaseConfig) error {
|
||||
connStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
|
||||
config.Username,
|
||||
config.Password,
|
||||
config.Host,
|
||||
config.Port,
|
||||
config.Database,
|
||||
)
|
||||
|
||||
db, err := sql.Open("mysql", connStr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open MySQL connection: %w", err)
|
||||
}
|
||||
|
||||
return s.configureSQLDB(config.Name, db)
|
||||
}
|
||||
|
||||
// addSQLServer adds SQL Server connection
|
||||
func (s *service) addSQLServer(config DatabaseConfig) error {
|
||||
connStr := fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=%s",
|
||||
config.Username,
|
||||
config.Password,
|
||||
config.Host,
|
||||
config.Port,
|
||||
config.Database,
|
||||
)
|
||||
|
||||
db, err := sql.Open("sqlserver", connStr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open SQL Server connection: %w", err)
|
||||
}
|
||||
|
||||
return s.configureSQLDB(config.Name, db)
|
||||
}
|
||||
|
||||
// addSQLite adds SQLite connection
|
||||
func (s *service) addSQLite(config DatabaseConfig) error {
|
||||
dbPath := config.Path
|
||||
if dbPath == "" {
|
||||
dbPath = fmt.Sprintf("./data/%s.db", config.Name)
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open SQLite connection: %w", err)
|
||||
}
|
||||
|
||||
return s.configureSQLDB(config.Name, db)
|
||||
}
|
||||
|
||||
// addMongoDB adds MongoDB connection
|
||||
func (s *service) addMongoDB(config DatabaseConfig) error {
|
||||
uri := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s",
|
||||
config.Username,
|
||||
config.Password,
|
||||
config.Host,
|
||||
config.Port,
|
||||
config.Database,
|
||||
)
|
||||
|
||||
clientOptions := options.Client().ApplyURI(uri)
|
||||
client, err := mongo.Connect(context.Background(), clientOptions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to MongoDB: %w", err)
|
||||
}
|
||||
|
||||
// Test connection
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stats := make(map[string]string)
|
||||
|
||||
// Ping the database
|
||||
err := s.db.PingContext(ctx)
|
||||
if err != nil {
|
||||
stats["status"] = "down"
|
||||
stats["error"] = fmt.Sprintf("db down: %v", err)
|
||||
log.Fatalf("db down: %v", err) // Log the error and terminate the program
|
||||
return stats
|
||||
if err := client.Ping(ctx, nil); err != nil {
|
||||
client.Disconnect(context.Background())
|
||||
return fmt.Errorf("failed to ping MongoDB: %w", err)
|
||||
}
|
||||
|
||||
// Database is up, add more statistics
|
||||
stats["status"] = "up"
|
||||
stats["message"] = "It's healthy"
|
||||
s.mongoClients[config.Name] = client
|
||||
s.configs[config.Name] = config
|
||||
log.Printf("Successfully connected to MongoDB: %s", config.Name)
|
||||
|
||||
// Get database stats (like open connections, in use, idle, etc.)
|
||||
dbStats := s.db.Stats()
|
||||
stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
|
||||
stats["in_use"] = strconv.Itoa(dbStats.InUse)
|
||||
stats["idle"] = strconv.Itoa(dbStats.Idle)
|
||||
stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
|
||||
stats["wait_duration"] = dbStats.WaitDuration.String()
|
||||
stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
|
||||
stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10)
|
||||
|
||||
// Evaluate stats to provide a health message
|
||||
if dbStats.OpenConnections > 40 { // Assuming 50 is the max for this example
|
||||
stats["message"] = "The database is experiencing heavy load."
|
||||
}
|
||||
|
||||
if dbStats.WaitCount > 1000 {
|
||||
stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks."
|
||||
}
|
||||
|
||||
if dbStats.MaxIdleClosed > int64(dbStats.OpenConnections)/2 {
|
||||
stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings."
|
||||
}
|
||||
|
||||
if dbStats.MaxLifetimeClosed > int64(dbStats.OpenConnections)/2 {
|
||||
stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising the connection usage pattern."
|
||||
}
|
||||
|
||||
return stats
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the database connection.
|
||||
// It logs a message indicating the disconnection from the specific database.
|
||||
// If the connection is successfully closed, it returns nil.
|
||||
// If an error occurs while closing the connection, it returns the error.
|
||||
// configureSQLDB configures common SQL database settings
|
||||
func (s *service) configureSQLDB(name string, db *sql.DB) error {
|
||||
// Configure connection pool
|
||||
db.SetMaxOpenConns(25)
|
||||
db.SetMaxIdleConns(25)
|
||||
db.SetConnMaxLifetime(5 * time.Minute)
|
||||
|
||||
// Test connection
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
db.Close()
|
||||
return fmt.Errorf("failed to ping database: %w", err)
|
||||
}
|
||||
|
||||
s.sqlDatabases[name] = db
|
||||
log.Printf("Successfully connected to SQL database: %s", name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// # Example multi-database configuration for different database types
|
||||
|
||||
// # PostgreSQL
|
||||
// DB_TYPE_PRIMARY=postgres
|
||||
// DB_HOST_PRIMARY=localhost
|
||||
// DB_PORT_PRIMARY=5432
|
||||
// DB_NAME_PRIMARY=myapp_postgres
|
||||
// DB_USER_PRIMARY=postgres
|
||||
// DB_PASS_PRIMARY=postgres_password
|
||||
// DB_SCHEMA_PRIMARY=public
|
||||
// DB_SSLMODE_PRIMARY=disable
|
||||
|
||||
// # MySQL
|
||||
// DB_TYPE_MYSQL=mysql
|
||||
// DB_HOST_MYSQL=localhost
|
||||
// DB_PORT_MYSQL=3306
|
||||
// DB_NAME_MYSQL=myapp_mysql
|
||||
// DB_USER_MYSQL=root
|
||||
// DB_PASS_MYSQL=mysql_password
|
||||
|
||||
// # SQL Server
|
||||
// DB_TYPE_SQLSERVER=mssql
|
||||
// DB_HOST_SQLSERVER=localhost
|
||||
// DB_PORT_SQLSERVER=1433
|
||||
// DB_NAME_SQLSERVER=myapp_mssql
|
||||
// DB_USER_SQLSERVER=sa
|
||||
// DB_PASS_SQLSERVER=mssql_password
|
||||
|
||||
// # MongoDB
|
||||
// DB_TYPE_MONGODB=mongodb
|
||||
// DB_HOST_MONGODB=localhost
|
||||
// DB_PORT_MONGODB=27017
|
||||
// DB_NAME_MONGODB=myapp_mongo
|
||||
// DB_USER_MONGODB=mongo_user
|
||||
// DB_PASS_MONGODB=mongo_password
|
||||
|
||||
// # SQLite
|
||||
// DB_TYPE_SQLITE=sqlite
|
||||
// DB_PATH_SQLITE=./data/myapp_sqlite.db
|
||||
|
||||
// Health checks the health of all database connections by pinging each database.
|
||||
// It returns a map with database names as keys and their health statistics as values.
|
||||
func (s *service) Health() map[string]map[string]string {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
result := make(map[string]map[string]string)
|
||||
|
||||
// Check SQL databases
|
||||
for name, db := range s.sqlDatabases {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stats := make(map[string]string)
|
||||
|
||||
// Ping the database
|
||||
err := db.PingContext(ctx)
|
||||
if err != nil {
|
||||
stats["status"] = "down"
|
||||
stats["error"] = fmt.Sprintf("db down: %v", err)
|
||||
stats["type"] = "sql"
|
||||
result[name] = stats
|
||||
continue
|
||||
}
|
||||
|
||||
// Database is up, add more statistics
|
||||
stats["status"] = "up"
|
||||
stats["message"] = "It's healthy"
|
||||
stats["type"] = "sql"
|
||||
|
||||
// Get database stats
|
||||
dbStats := db.Stats()
|
||||
stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
|
||||
stats["in_use"] = strconv.Itoa(dbStats.InUse)
|
||||
stats["idle"] = strconv.Itoa(dbStats.Idle)
|
||||
stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
|
||||
stats["wait_duration"] = dbStats.WaitDuration.String()
|
||||
stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
|
||||
stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10)
|
||||
|
||||
// Evaluate stats to provide health messages
|
||||
if dbStats.OpenConnections > 40 {
|
||||
stats["message"] = "The database is experiencing heavy load."
|
||||
}
|
||||
|
||||
if dbStats.WaitCount > 1000 {
|
||||
stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks."
|
||||
}
|
||||
|
||||
if dbStats.MaxIdleClosed > int64(dbStats.OpenConnections)/2 {
|
||||
stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings."
|
||||
}
|
||||
|
||||
if dbStats.MaxLifetimeClosed > int64(dbStats.OpenConnections)/2 {
|
||||
stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising the connection usage pattern."
|
||||
}
|
||||
|
||||
result[name] = stats
|
||||
}
|
||||
|
||||
// Check MongoDB connections
|
||||
for name, client := range s.mongoClients {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stats := make(map[string]string)
|
||||
|
||||
// Ping the MongoDB
|
||||
err := client.Ping(ctx, nil)
|
||||
if err != nil {
|
||||
stats["status"] = "down"
|
||||
stats["error"] = fmt.Sprintf("mongodb down: %v", err)
|
||||
stats["type"] = "mongodb"
|
||||
result[name] = stats
|
||||
continue
|
||||
}
|
||||
|
||||
// MongoDB is up
|
||||
stats["status"] = "up"
|
||||
stats["message"] = "It's healthy"
|
||||
stats["type"] = "mongodb"
|
||||
|
||||
result[name] = stats
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetDB returns a specific SQL database connection by name
|
||||
func (s *service) GetDB(name string) (*sql.DB, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
db, exists := s.sqlDatabases[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("database %s not found", name)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// GetMongoClient returns a specific MongoDB client by name
|
||||
func (s *service) GetMongoClient(name string) (*mongo.Client, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
client, exists := s.mongoClients[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("MongoDB client %s not found", name)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// ListDBs returns list of available database names
|
||||
func (s *service) ListDBs() []string {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
names := make([]string, 0, len(s.sqlDatabases)+len(s.mongoClients))
|
||||
|
||||
// Add SQL databases
|
||||
for name := range s.sqlDatabases {
|
||||
names = append(names, name)
|
||||
}
|
||||
|
||||
// Add MongoDB clients
|
||||
for name := range s.mongoClients {
|
||||
names = append(names, name)
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// GetDBType returns the type of a specific database
|
||||
func (s *service) GetDBType(name string) (DatabaseType, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
config, exists := s.configs[name]
|
||||
if !exists {
|
||||
return "", fmt.Errorf("database %s not found", name)
|
||||
}
|
||||
|
||||
return config.Type, nil
|
||||
}
|
||||
|
||||
// Close closes all database connections
|
||||
// It logs messages indicating disconnection from each database
|
||||
func (s *service) Close() error {
|
||||
log.Printf("Disconnected from database: %s", database)
|
||||
return s.db.Close()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
var errs []error
|
||||
|
||||
// Close SQL databases
|
||||
for name, db := range s.sqlDatabases {
|
||||
if err := db.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to close database %s: %w", name, err))
|
||||
} else {
|
||||
log.Printf("Disconnected from SQL database: %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
// Close MongoDB clients
|
||||
for name, client := range s.mongoClients {
|
||||
if err := client.Disconnect(context.Background()); err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to disconnect MongoDB client %s: %w", name, err))
|
||||
} else {
|
||||
log.Printf("Disconnected from MongoDB: %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
s.sqlDatabases = make(map[string]*sql.DB)
|
||||
s.mongoClients = make(map[string]*mongo.Client)
|
||||
s.configs = make(map[string]DatabaseConfig)
|
||||
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("errors closing databases: %v", errs)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user