2024-09-04 10:53:12 +02:00
|
|
|
package config
|
2024-08-28 15:26:00 +02:00
|
|
|
|
|
|
|
import (
|
2024-09-04 10:53:12 +02:00
|
|
|
"os"
|
2024-08-28 15:26:00 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/goraz/onion"
|
2024-09-04 10:53:12 +02:00
|
|
|
"github.com/rs/zerolog"
|
2024-08-28 15:26:00 +02:00
|
|
|
)
|
|
|
|
|
2024-09-04 10:53:12 +02:00
|
|
|
/* GetConfLoader
|
|
|
|
* Get the configuration loader for the application
|
|
|
|
* Parameters:
|
|
|
|
* - AppName: string : the name of the application
|
|
|
|
* Returns:
|
|
|
|
* - *onion.Onion : the configuration loader
|
|
|
|
* The configuration loader will load the configuration from the following sources:
|
|
|
|
* - the environment variables with the prefix APPNAME_
|
|
|
|
* - the file /etc/oc/appname.json
|
|
|
|
* - the file ./appname.json
|
|
|
|
* The configuration loader will merge the configuration from the different sources
|
|
|
|
* The configuration loader will give priority to the environment variables
|
|
|
|
* The configuration loader will give priority to the local file over the default file
|
|
|
|
*/
|
2024-08-28 15:26:00 +02:00
|
|
|
|
2024-09-02 15:21:16 +02:00
|
|
|
func GetConfLoader() *onion.Onion {
|
2024-09-04 10:53:12 +02:00
|
|
|
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
|
|
|
|
AppName := GetAppName()
|
2024-12-16 09:17:54 +01:00
|
|
|
EnvPrefix := strings.ToUpper(AppName[0:2]+AppName[3:]) + "_"
|
2024-09-04 14:11:08 +02:00
|
|
|
defaultConfigFile := "/etc/oc/" + AppName[3:] + ".json"
|
|
|
|
localConfigFile := "./" + AppName[3:] + ".json"
|
2024-08-28 15:26:00 +02:00
|
|
|
var configFile string
|
|
|
|
var o *onion.Onion
|
2024-12-16 09:17:54 +01:00
|
|
|
l3 := GetEnvVarLayer(EnvPrefix)
|
2024-08-28 15:26:00 +02:00
|
|
|
l2, err := onion.NewFileLayer(localConfigFile, nil)
|
|
|
|
if err == nil {
|
|
|
|
logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file")
|
|
|
|
configFile = localConfigFile
|
|
|
|
}
|
|
|
|
l1, err := onion.NewFileLayer(defaultConfigFile, nil)
|
|
|
|
if err == nil {
|
|
|
|
logger.Info().Msg("Config file found : " + defaultConfigFile)
|
|
|
|
configFile = defaultConfigFile
|
|
|
|
}
|
|
|
|
if configFile == "" {
|
|
|
|
logger.Info().Msg("No config file found, using env")
|
|
|
|
o = onion.New(l3)
|
|
|
|
} else if l1 != nil && l2 != nil {
|
|
|
|
o = onion.New(l1, l2, l3)
|
|
|
|
} else if l1 == nil {
|
|
|
|
o = onion.New(l2, l3)
|
|
|
|
} else if l2 == nil {
|
|
|
|
o = onion.New(l1, l3)
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
2024-12-16 09:17:54 +01:00
|
|
|
|
|
|
|
func GetEnvVarLayer(prefix string) onion.Layer {
|
|
|
|
envVars := make(map[string]interface{})
|
|
|
|
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
pair := strings.SplitN(e, "=", 2)
|
|
|
|
key := pair[0]
|
|
|
|
if strings.HasPrefix(key, prefix) {
|
|
|
|
envVars[strings.TrimPrefix(key, prefix)] = pair[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return onion.NewMapLayer(envVars)
|
|
|
|
}
|