2024-01-12 04:13:10 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-01-28 07:20:59 +00:00
|
|
|
_logger "git.lumeweb.com/LumeWeb/portal/logger"
|
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-01-28 07:20:59 +00:00
|
|
|
func NewConfig(logger *zap.Logger) (*viper.Viper, error) {
|
|
|
|
if logger == nil {
|
|
|
|
logger = _logger.NewFallbackLogger()
|
|
|
|
}
|
|
|
|
|
2024-01-12 04:13:10 +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.As(err, &viper.ConfigFileNotFoundError{}) {
|
2024-01-12 04:23:22 +00:00
|
|
|
logger.Info("Config file not found, using default settings.")
|
2024-01-15 04:54:01 +00:00
|
|
|
err := viper.SafeWriteConfig()
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = writeDefaults()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-15 04:54:01 +00:00
|
|
|
}
|
2024-01-28 07:20:59 +00:00
|
|
|
|
|
|
|
return viper.GetViper(), nil
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
2024-01-28 07:20:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeDefaults()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
2024-01-12 04:23:22 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
return viper.GetViper(), nil
|
2024-01-15 17:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeDefaults() error {
|
|
|
|
defaults := map[string]interface{}{
|
|
|
|
"core.post-upload-limit": 1024 * 1024 * 1000,
|
|
|
|
"core.log.level": "info",
|
2024-01-16 01:58:20 +00:00
|
|
|
"core.db.charset": "utf8mb4",
|
2024-01-16 03:36:17 +00:00
|
|
|
"core.db.port": 3306,
|
2024-01-16 04:59:18 +00:00
|
|
|
"core.db.name": "portal",
|
2024-01-15 17:02:15 +00:00
|
|
|
"protocol.s5.p2p.maxOutgoingPeerFailures": 10,
|
|
|
|
"protocol.s5.p2p.network": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := false
|
|
|
|
|
|
|
|
for key, value := range defaults {
|
|
|
|
if writeDefault(key, value) {
|
|
|
|
changes = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes {
|
|
|
|
err := viper.WriteConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-15 17:02:15 +00:00
|
|
|
func writeDefault(key string, value interface{}) bool {
|
|
|
|
if !viper.IsSet(key) {
|
|
|
|
viper.SetDefault(key, value)
|
|
|
|
return true
|
|
|
|
}
|
2024-01-15 16:50:31 +00:00
|
|
|
|
2024-01-15 17:02:15 +00:00
|
|
|
return false
|
2024-01-15 04:54:01 +00:00
|
|
|
}
|