configuration done in conf/conf.go init()

This commit is contained in:
pb
2024-02-16 10:23:22 +01:00
parent 6b67dfe729
commit 886023b2b9
3 changed files with 48 additions and 27 deletions

View File

@@ -1,6 +1,11 @@
package conf
import "sync"
import (
"sync"
"github.com/beego/beego/logs"
"github.com/goraz/onion"
)
type Config struct {
OcCatalogUrl string
@@ -9,6 +14,41 @@ type Config struct {
var instance *Config
var once sync.Once
const defaultConfigFile = "/etc/oc/search.json"
const localConfigFile = "./local_search.json"
func init(){
configFile := ""
var o *onion.Onion
l3 := onion.NewEnvLayerPrefix("_", "OCSEARCH_")
l2, err := onion.NewFileLayer(defaultConfigFile, nil)
if err == nil {
logs.Info("Config file found : " + defaultConfigFile)
configFile = defaultConfigFile
}
l1, err := onion.NewFileLayer(localConfigFile, nil)
if err == nil {
logs.Info("Local config file found " + localConfigFile + ", overriding default file")
configFile = localConfigFile
}
if configFile == "" {
logs.Info("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)
}
GetConfig().OcCatalogUrl = o.GetStringDefault("oc-catalog", "localhost:49618")
}
func GetConfig() *Config {
once.Do(func() {
instance = &Config{}