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{}

View File

@ -2,6 +2,7 @@ package controllers
import (
OCCatalog_cli "oc-search/api-client/oc-catalog"
conf "oc-search/conf"
"github.com/beego/beego/logs"
beego "github.com/beego/beego/v2/server/web"
@ -14,6 +15,11 @@ type MainController struct {
var OCCatalogAPI *OCCatalog_cli.APIClient
// This method should not be called init() because it will
// lead to the parameter BasePath to not be able to
// retrieve its value from a config file, that will
// be read after the init has happened (at import of the package)
func init() {
// OcCatalogURL := conf.GetConfig().OcCatalogUrl
@ -22,7 +28,7 @@ func init() {
// }
// auth := context.WithValue(context.Background(), OCCatalog_cli.ContextAPIKey, OCCatalog_cli.APIKey{Key: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2MTI1OTI3MDIsInVzZXJfaWQiOiJ4ZCJ9.kTLb1FtpdnaobUpe5u9Jw8S7Cc6gf7ExmU4U3XMcC2o"})
OCCatalogAPI = OCCatalog_cli.NewAPIClient(&OCCatalog_cli.Configuration{BasePath: "http://"+ "oc-catalog:8080" +"/v1",
OCCatalogAPI = OCCatalog_cli.NewAPIClient(&OCCatalog_cli.Configuration{BasePath: "http://"+ conf.GetConfig().OcCatalogUrl +"/v1",
DefaultHeader: map[string]string{
"authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2MTMwMDI0NjAsInVzZXJfaWQiOiJhc2QifQ.TXT18aeulnCrtedKKFVaD0BapOTdVAFcJJdVS7zk0I8",
},

25
main.go
View File

@ -7,7 +7,6 @@ import (
"github.com/beego/beego/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/goraz/onion"
)
const defaultConfigFile = "/etc/oc/search.json"
@ -17,31 +16,7 @@ func main() {
log := logs.NewLogger(10000)
log.SetLogger("console")
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)
}
conf.GetConfig().OcCatalogUrl = o.GetStringDefault("oc-catalog", "localhost:49618")
beego.BConfig.WebConfig.Session.SessionOn = true
beego.SetStaticPath("/favicon.ico", "/static/favicon.ico")
beego.BConfig.WebConfig.DirectoryIndex = true