2024-01-12 04:13:10 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-02-22 07:09:31 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/go-units"
|
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-22 07:09:31 +00:00
|
|
|
type Config struct {
|
2024-02-28 11:11:04 +00:00
|
|
|
Core CoreConfig `mapstructure:"core"`
|
|
|
|
Protocol map[string]ProtocolConfig `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,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.setDefaults(m.coreDefaults(), "")
|
|
|
|
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-24 13:57:36 +00:00
|
|
|
err = v.Unmarshal(&config, 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-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 {
|
|
|
|
defaults := cfg.Defaults()
|
|
|
|
|
2024-02-23 12:54:45 +00:00
|
|
|
protocolPrefix := fmt.Sprintf("protocol.%s", name)
|
|
|
|
|
|
|
|
m.setDefaults(defaults, 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-23 12:54:45 +00:00
|
|
|
return m.viper.Sub(protocolPrefix).Unmarshal(cfg)
|
2024-02-22 07:09:31 +00:00
|
|
|
}
|
2024-01-15 17:02:15 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
func (m *Manager) setDefaults(defaults map[string]interface{}, prefix string) {
|
2024-01-15 17:02:15 +00:00
|
|
|
for key, value := range defaults {
|
2024-02-22 07:09:31 +00:00
|
|
|
if prefix != "" {
|
|
|
|
key = fmt.Sprintf("%s.%s", prefix, key)
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
2024-02-22 07:09:31 +00:00
|
|
|
if m.setDefault(key, value) {
|
|
|
|
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-22 07:09:31 +00:00
|
|
|
func (m *Manager) coreDefaults() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2024-02-23 13:26:27 +00:00
|
|
|
"core.post_upload_limit": units.MiB * 100,
|
2024-02-22 07:09:31 +00:00
|
|
|
"core.log.level": "info",
|
|
|
|
"core.db.charset": "utf8mb4",
|
|
|
|
"core.db.port": 3306,
|
|
|
|
"core.db.name": "portal",
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
2024-02-22 07:09:31 +00:00
|
|
|
}
|
2024-01-15 16:50:31 +00:00
|
|
|
|
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
|
|
|
|
}
|