2024-01-12 04:13:10 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-02-22 07:09:31 +00:00
|
|
|
"fmt"
|
2024-02-28 13:47:33 +00:00
|
|
|
"reflect"
|
2024-02-22 07:09:31 +00:00
|
|
|
|
2024-01-12 04:13:10 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-01-28 07:20:59 +00:00
|
|
|
"go.uber.org/zap"
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ConfigFilePaths = []string{
|
|
|
|
"/etc/lumeweb/portal/",
|
|
|
|
"$HOME/.lumeweb/portal/",
|
|
|
|
".",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
type Defaults interface {
|
|
|
|
Defaults() map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Validator interface {
|
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
type Config struct {
|
2024-02-28 14:42:06 +00:00
|
|
|
Core CoreConfig `mapstructure:"core"`
|
|
|
|
Protocol map[string]interface{} `mapstructure:"protocol"`
|
2024-02-22 07:09:31 +00:00
|
|
|
}
|
2024-01-28 07:20:59 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
type Manager struct {
|
|
|
|
viper *viper.Viper
|
|
|
|
root *Config
|
|
|
|
changes bool
|
|
|
|
}
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-02-27 08:54:00 +00:00
|
|
|
func NewManager() (*Manager, error) {
|
2024-02-27 08:57:17 +00:00
|
|
|
v, err := newConfig()
|
2024-02-22 07:09:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
var config Config
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
m := &Manager{
|
|
|
|
viper: v,
|
|
|
|
root: &config,
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
m.setDefaultsForObject(m.root.Core, "")
|
2024-02-22 07:09:31 +00:00
|
|
|
err = m.maybeSave()
|
2024-01-12 04:13:10 +00:00
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:03:36 +00:00
|
|
|
err = v.Unmarshal(&config, viper.DecodeHook(clusterConfigHook()), viper.DecodeHook(cacheConfigHook()))
|
2024-01-28 07:20:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
2024-01-12 04:23:22 +00:00
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
err = m.validateObject(m.root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-28 14:36:01 +00:00
|
|
|
err = m.maybeConfigureCluster()
|
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2024-02-22 08:19:18 +00:00
|
|
|
return m, nil
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
func (m *Manager) ConfigureProtocol(name string, cfg ProtocolConfig) error {
|
2024-02-23 12:54:45 +00:00
|
|
|
protocolPrefix := fmt.Sprintf("protocol.%s", name)
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
m.setDefaultsForObject(cfg, protocolPrefix)
|
2024-02-22 07:09:31 +00:00
|
|
|
err := m.maybeSave()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
err = m.viper.Sub(protocolPrefix).Unmarshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.validateObject(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-28 14:43:06 +00:00
|
|
|
m.root.Protocol[name] = cfg
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
return nil
|
2024-02-22 07:09:31 +00:00
|
|
|
}
|
2024-01-15 17:02:15 +00:00
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
func (m *Manager) setDefaultsForObject(obj interface{}, prefix string) {
|
|
|
|
// Reflect on the object to traverse its fields
|
|
|
|
objValue := reflect.ValueOf(obj)
|
|
|
|
objType := reflect.TypeOf(obj)
|
|
|
|
|
|
|
|
// If the object is a pointer, we need to work with its element
|
|
|
|
if objValue.Kind() == reflect.Ptr {
|
|
|
|
objValue = objValue.Elem()
|
|
|
|
objType = objType.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the object itself implements Defaults
|
|
|
|
if setter, ok := obj.(Defaults); ok {
|
|
|
|
m.applyDefaults(setter, prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively handle struct fields
|
|
|
|
for i := 0; i < objValue.NumField(); i++ {
|
|
|
|
field := objValue.Field(i)
|
|
|
|
fieldType := objType.Field(i)
|
2024-02-28 14:48:20 +00:00
|
|
|
|
|
|
|
// Check if the field is exported and can be interfaced
|
|
|
|
if !field.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
mapstructureTag := fieldType.Tag.Get("mapstructure")
|
|
|
|
|
|
|
|
// Construct new prefix based on the mapstructure tag, if available
|
|
|
|
newPrefix := prefix
|
|
|
|
if mapstructureTag != "" && mapstructureTag != "-" {
|
|
|
|
if newPrefix != "" {
|
|
|
|
newPrefix += "."
|
|
|
|
}
|
|
|
|
newPrefix += mapstructureTag
|
|
|
|
}
|
|
|
|
|
|
|
|
// If field is a struct or pointer to a struct, recurse
|
|
|
|
if field.Kind() == reflect.Struct || (field.Kind() == reflect.Ptr && field.Elem().Kind() == reflect.Struct) {
|
|
|
|
if field.Kind() == reflect.Ptr && field.IsNil() {
|
|
|
|
// Initialize nil pointer to struct
|
|
|
|
field.Set(reflect.New(fieldType.Type.Elem()))
|
|
|
|
}
|
|
|
|
m.setDefaultsForObject(field.Interface(), newPrefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) validateObject(obj interface{}) error {
|
|
|
|
// Reflect on the object to traverse its fields
|
|
|
|
objValue := reflect.ValueOf(obj)
|
|
|
|
objType := reflect.TypeOf(obj)
|
|
|
|
|
|
|
|
// If the object is a pointer, we need to work with its element
|
|
|
|
if objValue.Kind() == reflect.Ptr {
|
|
|
|
objValue = objValue.Elem()
|
|
|
|
objType = objType.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the object itself implements Defaults
|
|
|
|
if validator, ok := obj.(Validator); ok {
|
|
|
|
err := validator.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively handle struct fields
|
|
|
|
for i := 0; i < objValue.NumField(); i++ {
|
|
|
|
field := objValue.Field(i)
|
|
|
|
fieldType := objType.Field(i)
|
|
|
|
|
2024-02-28 14:50:42 +00:00
|
|
|
if !field.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:47:33 +00:00
|
|
|
// If field is a struct or pointer to a struct, recurse
|
|
|
|
if field.Kind() == reflect.Struct || (field.Kind() == reflect.Ptr && field.Elem().Kind() == reflect.Struct) {
|
|
|
|
if field.Kind() == reflect.Ptr && field.IsNil() {
|
|
|
|
// Initialize nil pointer to struct
|
|
|
|
field.Set(reflect.New(fieldType.Type.Elem()))
|
|
|
|
}
|
|
|
|
err := m.validateObject(field.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) applyDefaults(setter Defaults, prefix string) {
|
|
|
|
defaults := setter.Defaults()
|
2024-01-15 17:02:15 +00:00
|
|
|
for key, value := range defaults {
|
2024-02-28 13:47:33 +00:00
|
|
|
fullKey := key
|
2024-02-22 07:09:31 +00:00
|
|
|
if prefix != "" {
|
2024-02-28 13:47:33 +00:00
|
|
|
fullKey = fmt.Sprintf("%s.%s", prefix, key)
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
2024-02-28 13:47:33 +00:00
|
|
|
if m.setDefault(fullKey, value) {
|
2024-02-22 07:09:31 +00:00
|
|
|
m.changes = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) setDefault(key string, value interface{}) bool {
|
|
|
|
if !m.viper.IsSet(key) {
|
|
|
|
m.viper.SetDefault(key, value)
|
|
|
|
return true
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) maybeSave() error {
|
|
|
|
if m.changes {
|
|
|
|
ret := m.viper.WriteConfig()
|
|
|
|
if ret != nil {
|
|
|
|
return ret
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
2024-02-22 07:09:31 +00:00
|
|
|
m.changes = false
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 04:23:22 +00:00
|
|
|
return nil
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
2024-01-15 04:54:01 +00:00
|
|
|
|
2024-02-28 14:21:20 +00:00
|
|
|
func (m *Manager) maybeConfigureCluster() error {
|
2024-02-28 14:45:34 +00:00
|
|
|
if m.root.Core.Clustered != nil && m.root.Core.Clustered.Enabled {
|
2024-02-28 14:21:20 +00:00
|
|
|
m.root.Core.DB.Cache.Mode = "redis"
|
|
|
|
m.root.Core.DB.Cache.Options = m.root.Core.Clustered.Redis
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
func (m *Manager) Config() *Config {
|
|
|
|
return m.root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Viper() *viper.Viper {
|
|
|
|
return m.viper
|
|
|
|
}
|
|
|
|
|
2024-02-22 08:38:05 +00:00
|
|
|
func (m *Manager) Save() error {
|
|
|
|
err := m.viper.WriteConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.viper.Unmarshal(&m.root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-27 08:57:17 +00:00
|
|
|
func newConfig() (*viper.Viper, error) {
|
|
|
|
logger := newFallbackLogger()
|
2024-02-22 07:09:31 +00:00
|
|
|
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
|
|
|
|
for _, path := range ConfigFilePaths {
|
|
|
|
viper.AddConfigPath(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetEnvPrefix("LUME_WEB_PORTAL")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, &viper.ConfigFileNotFoundError{}) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("Config file not found, using default settings.")
|
|
|
|
err := viper.SafeWriteConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return viper.GetViper(), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return viper.GetViper(), nil
|
2024-01-15 04:54:01 +00:00
|
|
|
}
|
2024-02-27 08:57:17 +00:00
|
|
|
func newFallbackLogger() *zap.Logger {
|
|
|
|
l, _ := zap.NewDevelopment()
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|