refactor: define a cluster config with redis and etcd support

This commit is contained in:
Derrick Hammer 2024-02-28 08:03:36 -05:00
parent 7edab13afe
commit 1a20a7d35f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 87 additions and 7 deletions

70
config/cluster.go Normal file
View File

@ -0,0 +1,70 @@
package config
import (
"errors"
"reflect"
"github.com/mitchellh/mapstructure"
)
type ClusterConfig struct {
Enabled bool `mapstructure:"enabled"`
Redis *RedisConfig `mapstructure:"redis"`
Etcd *EtcdConfig `mapstructure:"etcd"`
}
type RedisConfig struct {
Address string `mapstructure:"address"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
func (r *RedisConfig) Validate() error {
if r.Address == "" {
return errors.New("address is required")
}
return nil
}
func clusterConfigHook() mapstructure.DecodeHookFuncType {
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.Map || t != reflect.TypeOf(&ClusterConfig{}) {
return data, nil
}
var clusterConfig ClusterConfig
if err := mapstructure.Decode(data, &clusterConfig); err != nil {
return nil, err
}
// Check if the input data map includes "redis" configuration
if opts, ok := data.(map[string]interface{})["redis"].(map[string]interface{}); ok && opts != nil {
var redisOptions RedisConfig
if err := mapstructure.Decode(opts, &redisOptions); err != nil {
return nil, err
}
if err := redisOptions.Validate(); err != nil {
return nil, err
}
clusterConfig.Redis = &redisOptions
}
// Check if the input data map includes "etcd" configuration
if opts, ok := data.(map[string]interface{})["etcd"].(map[string]interface{}); ok && opts != nil {
var etcdOptions EtcdConfig
if err := mapstructure.Decode(opts, &etcdOptions); err != nil {
return nil, err
}
if err := etcdOptions.Validate(); err != nil {
return nil, err
}
clusterConfig.Etcd = &etcdOptions
}
return &clusterConfig, nil
}
}

View File

@ -47,7 +47,7 @@ func NewManager() (*Manager, error) {
return nil, err return nil, err
} }
err = v.Unmarshal(&config, viper.DecodeHook(cacheConfigHook())) err = v.Unmarshal(&config, viper.DecodeHook(clusterConfigHook()), viper.DecodeHook(cacheConfigHook()))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -13,4 +13,5 @@ type CoreConfig struct {
Storage StorageConfig `mapstructure:"storage"` Storage StorageConfig `mapstructure:"storage"`
Protocols []string `mapstructure:"protocols"` Protocols []string `mapstructure:"protocols"`
Mail MailConfig `mapstructure:"mail"` Mail MailConfig `mapstructure:"mail"`
Clustered *ClusterConfig `mapstructure:"clustered"`
} }

View File

@ -21,12 +21,6 @@ type CacheConfig struct {
Options interface{} `mapstructure:"options"` Options interface{} `mapstructure:"options"`
} }
type RedisConfig struct {
Address string `mapstructure:"address"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
type MemoryConfig struct { type MemoryConfig struct {
} }

15
config/etcd.go Normal file
View File

@ -0,0 +1,15 @@
package config
import "errors"
type EtcdConfig struct {
Endpoints []string `mapstructure:"endpoints"`
DialTimeout int `mapstructure:"dial_timeout"`
}
func (r *EtcdConfig) Validate() error {
if len(r.Endpoints) == 0 {
return errors.New("endpoints is required")
}
return nil
}