mirror of
https://github.com/sapphiregaze/discord-gorp.git
synced 2025-12-07 13:20:30 -08:00
Added Hot Reload For Configs:
- Added hot reload for config changes - Slight change to config path for OS agnostic file path
This commit is contained in:
2
go.mod
2
go.mod
@@ -3,13 +3,13 @@ module github.com/sapphiregaze/discord-gorp
|
|||||||
go 1.22.7
|
go 1.22.7
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0
|
||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/lmittmann/tint v1.0.5
|
github.com/lmittmann/tint v1.0.5
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/magiconair/properties v1.8.7 // indirect
|
github.com/magiconair/properties v1.8.7 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
"github.com/sapphiregaze/discord-gorp/pkg/logger"
|
"github.com/sapphiregaze/discord-gorp/pkg/logger"
|
||||||
@@ -76,7 +78,7 @@ func Load() (*Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
configDir := filepath.Join(homeDir, ".config/discord-gorp")
|
configDir := filepath.Join(homeDir, ".config", "discord-gorp")
|
||||||
configPath := filepath.Join(configDir, "config.yaml")
|
configPath := filepath.Join(configDir, "config.yaml")
|
||||||
|
|
||||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||||
@@ -96,14 +98,39 @@ func Load() (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
viper.SetConfigFile(configPath)
|
viper.SetConfigFile(configPath)
|
||||||
|
viper.SetConfigType("yaml")
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("error reading config file: %w", err)
|
||||||
}
|
}
|
||||||
|
logger.Info(fmt.Sprintf("Initial config loaded from %s", configPath))
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
if err := viper.Unmarshal(&config); err != nil {
|
if err := viper.Unmarshal(&config); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("error unmarshalling config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lastChange time.Time
|
||||||
|
|
||||||
|
viper.WatchConfig()
|
||||||
|
logger.Info(fmt.Sprintf("Watching for config changes at %s", configPath))
|
||||||
|
|
||||||
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||||
|
// debounce mechanism to ensure only one reload happens per file change/save
|
||||||
|
if time.Since(lastChange) < time.Millisecond*100 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lastChange = time.Now()
|
||||||
|
|
||||||
|
logger.Info(fmt.Sprintf("Config file changed at %s", e.Name))
|
||||||
|
if err := viper.Unmarshal(&config); err != nil {
|
||||||
|
logger.Error(fmt.Sprintf("Failed to reload config: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("Config reloaded successfully")
|
||||||
|
})
|
||||||
|
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user