Skip to content

Commit

Permalink
refactor(config): Rename struct and filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Oct 27, 2023
1 parent 133dfce commit da9c03c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pkg/s3-proxy/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Manager interface {
}

func NewManager(logger log.Logger) Manager {
return &managercontext{
return &managerimpl{
logger: logger,
internalFileWatchChannels: make([]chan bool, 0),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,49 @@ var mainConfigFolderPath = "conf/"

var validate = validator.New()

type managercontext struct {
type managerimpl struct {
cfg *Config
configs []*viper.Viper
onChangeHooks []func()
logger log.Logger
internalFileWatchChannels []chan bool
}

func (ctx *managercontext) AddOnChangeHook(hook func()) {
ctx.onChangeHooks = append(ctx.onChangeHooks, hook)
func (impl *managerimpl) AddOnChangeHook(hook func()) {
impl.onChangeHooks = append(impl.onChangeHooks, hook)
}

func (ctx *managercontext) Load() error {
func (impl *managerimpl) Load() error {
// List files
files, err := os.ReadDir(mainConfigFolderPath)
if err != nil {
return errors.WithStack(err)
}

// Generate viper instances for static configs
ctx.configs = generateViperInstances(files)
impl.configs = generateViperInstances(files)

// Load configuration
err = ctx.loadConfiguration()
err = impl.loadConfiguration()
if err != nil {
return err
}

// Loop over config files
funk.ForEach(ctx.configs, func(vip *viper.Viper) {
funk.ForEach(impl.configs, func(vip *viper.Viper) {
// Add hooks for on change events
vip.OnConfigChange(func(in fsnotify.Event) {
ctx.logger.Infof("Reload configuration detected for file %s", vip.ConfigFileUsed())
impl.logger.Infof("Reload configuration detected for file %s", vip.ConfigFileUsed())

// Reload config
err2 := ctx.loadConfiguration()
err2 := impl.loadConfiguration()
if err2 != nil {
ctx.logger.Error(err2)
impl.logger.Error(err2)
// Stop here and do not call hooks => configuration is unstable
return
}
// Call all hooks
funk.ForEach(ctx.onChangeHooks, func(hook func()) { hook() })
funk.ForEach(impl.onChangeHooks, func(hook func()) { hook() })
})
// Watch for configuration changes
vip.WatchConfig()
Expand All @@ -77,14 +77,14 @@ func (ctx *managercontext) Load() error {
}

// Imported and modified from viper v1.7.0.
func (ctx *managercontext) watchInternalFile(filePath string, forceStop chan bool, onChange func()) {
func (impl *managerimpl) watchInternalFile(filePath string, forceStop chan bool, onChange func()) {
initWG := sync.WaitGroup{}
initWG.Add(1)

go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
ctx.logger.Fatal(errors.WithStack(err))
impl.logger.Fatal(errors.WithStack(err))
}
defer watcher.Close()

Expand Down Expand Up @@ -129,7 +129,7 @@ func (ctx *managercontext) watchInternalFile(filePath string, forceStop chan boo

case err, ok := <-watcher.Errors:
if ok { // 'Errors' channel is not closed
ctx.logger.Errorf("watcher error: %v\n", err)
impl.logger.Errorf("watcher error: %v\n", err)
}

eventsWG.Done()
Expand All @@ -147,7 +147,7 @@ func (ctx *managercontext) watchInternalFile(filePath string, forceStop chan boo
initWG.Wait() // make sure that the go routine above fully ended before returning
}

func (*managercontext) loadDefaultConfigurationValues(vip *viper.Viper) {
func (*managerimpl) loadDefaultConfigurationValues(vip *viper.Viper) {
// Load default configuration
vip.SetDefault("log.level", DefaultLogLevel)
vip.SetDefault("log.format", DefaultLogFormat)
Expand Down Expand Up @@ -214,10 +214,10 @@ func generateViperInstances(files []os.DirEntry) []*viper.Viper {
return list
}

func (ctx *managercontext) loadConfiguration() error {
func (impl *managerimpl) loadConfiguration() error {
// Load must start by flushing all existing watcher on internal files
for i := 0; i < len(ctx.internalFileWatchChannels); i++ {
ch := ctx.internalFileWatchChannels[i]
for i := 0; i < len(impl.internalFileWatchChannels); i++ {
ch := impl.internalFileWatchChannels[i]
// Send the force stop
ch <- true
}
Expand All @@ -226,10 +226,10 @@ func (ctx *managercontext) loadConfiguration() error {
globalViper := viper.New()

// Put default values
ctx.loadDefaultConfigurationValues(globalViper)
impl.loadDefaultConfigurationValues(globalViper)

// Loop over configs
for _, vip := range ctx.configs {
for _, vip := range impl.configs {
err := vip.ReadInConfig()
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -268,30 +268,30 @@ func (ctx *managercontext) loadConfiguration() error {
}
// Initialize or flush watch internal file channels
internalFileWatchChannels := make([]chan bool, 0)
ctx.internalFileWatchChannels = internalFileWatchChannels
impl.internalFileWatchChannels = internalFileWatchChannels
// Loop over all credentials in order to watch file change
funk.ForEach(credentials, func(cred *CredentialConfig) {
// Check if credential is about a path
if cred.Path != "" {
// Create channel
ch := make(chan bool)
// Run the watch file
ctx.watchInternalFile(cred.Path, ch, func() {
impl.watchInternalFile(cred.Path, ch, func() {
// File change detected
ctx.logger.Infof("Reload credential file detected for path %s", cred.Path)
impl.logger.Infof("Reload credential file detected for path %s", cred.Path)

// Reload config
err2 := loadCredential(cred)
if err2 != nil {
ctx.logger.Error(err2)
impl.logger.Error(err2)
// Stop here and do not call hooks => configuration is unstable
return
}
// Call all hooks
funk.ForEach(ctx.onChangeHooks, func(hook func()) { hook() })
funk.ForEach(impl.onChangeHooks, func(hook func()) { hook() })
})
// Add channel to list of channels
ctx.internalFileWatchChannels = append(ctx.internalFileWatchChannels, ch)
impl.internalFileWatchChannels = append(impl.internalFileWatchChannels, ch)
}
})

Expand All @@ -300,14 +300,14 @@ func (ctx *managercontext) loadConfiguration() error {
return err
}

ctx.cfg = &out
impl.cfg = &out

return nil
}

// GetConfig allow to get configuration object.
func (ctx *managercontext) GetConfig() *Config {
return ctx.cfg
func (impl *managerimpl) GetConfig() *Config {
return impl.cfg
}

func loadAllCredentials(out *Config) ([]*CredentialConfig, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ targets:
// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managercontext{
ctx := &managerimpl{
logger: log.NewLogger(),
}

Expand Down Expand Up @@ -1588,7 +1588,7 @@ targets:
// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managercontext{
ctx := &managerimpl{
logger: log.NewLogger(),
}

Expand Down Expand Up @@ -1791,7 +1791,7 @@ targets:
// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managercontext{
ctx := &managerimpl{
logger: log.NewLogger(),
}

Expand Down Expand Up @@ -1991,7 +1991,7 @@ targets:
// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managercontext{
ctx := &managerimpl{
logger: log.NewLogger(),
}

Expand Down Expand Up @@ -2198,7 +2198,7 @@ targets:
// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managercontext{
ctx := &managerimpl{
logger: log.NewLogger(),
}

Expand Down Expand Up @@ -2430,7 +2430,7 @@ targets:
// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managercontext{
ctx := &managerimpl{
logger: log.NewLogger(),
}

Expand Down
File renamed without changes.

0 comments on commit da9c03c

Please sign in to comment.