Compare commits

...

2 Commits

Author SHA1 Message Date
871dd2e374 feat(config): Add SaveConfig method to persist configuration 2026-01-02 22:51:19 +02:00
Hein
ebd03d10ad feat(dbmanager): 🚑 Singleton for the database manager
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in -21m54s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in -21m29s
Build , Vet Test, and Lint / Build (push) Successful in -25m3s
Build , Vet Test, and Lint / Lint Code (push) Successful in -24m34s
Tests / Integration Tests (push) Failing after -25m39s
Tests / Unit Tests (push) Successful in -25m26s
2026-01-02 16:47:38 +02:00
2 changed files with 61 additions and 0 deletions

View File

@@ -122,6 +122,14 @@ func (m *Manager) Set(key string, value interface{}) {
m.v.Set(key, value)
}
// SaveConfig writes the current configuration to the specified path
func (m *Manager) SaveConfig(path string) error {
if err := m.v.WriteConfigAs(path); err != nil {
return fmt.Errorf("failed to save config to %s: %w", path, err)
}
return nil
}
// setDefaults sets default configuration values
func setDefaults(v *viper.Viper) {
// Server defaults

View File

@@ -50,6 +50,59 @@ type connectionManager struct {
wg sync.WaitGroup
}
var (
// singleton instance of the manager
instance Manager
// instanceMu protects the singleton instance
instanceMu sync.RWMutex
)
// SetupManager initializes the singleton database manager with the provided configuration.
// This function must be called before GetInstance().
// Returns an error if the manager is already initialized or if configuration is invalid.
func SetupManager(cfg ManagerConfig) error {
instanceMu.Lock()
defer instanceMu.Unlock()
if instance != nil {
return fmt.Errorf("manager already initialized")
}
mgr, err := NewManager(cfg)
if err != nil {
return fmt.Errorf("failed to create manager: %w", err)
}
instance = mgr
return nil
}
// GetInstance returns the singleton instance of the database manager.
// Returns an error if SetupManager has not been called yet.
func GetInstance() (Manager, error) {
instanceMu.RLock()
defer instanceMu.RUnlock()
if instance == nil {
return nil, fmt.Errorf("manager not initialized: call SetupManager first")
}
return instance, nil
}
// ResetInstance resets the singleton instance (primarily for testing purposes).
// WARNING: This should only be used in tests. Calling this in production code
// while the manager is in use can lead to undefined behavior.
func ResetInstance() {
instanceMu.Lock()
defer instanceMu.Unlock()
if instance != nil {
_ = instance.Close()
}
instance = nil
}
// NewManager creates a new database connection manager
func NewManager(cfg ManagerConfig) (Manager, error) {
// Apply defaults and validate configuration