From 17f0998ffb249baba5953bcd38b4d52144147e15 Mon Sep 17 00:00:00 2001 From: pb Date: Tue, 31 Oct 2023 11:23:39 +0100 Subject: [PATCH] Modified the conf loading process, now with onion in conf/conf.go --- conf_template.json | 5 +++++ controllers/default.go | 18 ---------------- main.go | 46 ++++++++++++++++++++++++++++++++++++++- services/init.go | 1 + services/mongo.go | 49 +++++++++++++++++++++++------------------- 5 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 conf_template.json delete mode 100644 controllers/default.go diff --git a/conf_template.json b/conf_template.json new file mode 100644 index 0000000..1b40758 --- /dev/null +++ b/conf_template.json @@ -0,0 +1,5 @@ +{ + "MongoURL" : "", + "DCNAME" : "", + "DBPOINT" : "" +} \ No newline at end of file diff --git a/controllers/default.go b/controllers/default.go deleted file mode 100644 index ae1bb1b..0000000 --- a/controllers/default.go +++ /dev/null @@ -1,18 +0,0 @@ -package controllers - -import ( - beego "github.com/beego/beego/v2/server/web" -) - -// MainController is in charge of the main page -type MainController struct { - beego.Controller -} - - - -// @Title logout -// @Description Logs out current logged in user session -// @Success 200 {string} logout success -// // @Security mySecurityPathNameApiKey -// @router /logout [get] diff --git a/main.go b/main.go index 49e14ed..fab20ca 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,18 @@ package main import ( "os" + "cloud.o-forge.io/core/oc-catalog/conf" "cloud.o-forge.io/core/oc-catalog/routers" "cloud.o-forge.io/core/oc-catalog/services" + "github.com/beego/beego/logs" beego "github.com/beego/beego/v2/server/web" + "github.com/goraz/onion" ) +const defaultConfigFile = "/etc/oc/catalog.json" +const localConfigFile = "./local_catalog.json" + func main() { // If we have any parameter, we run the beego directly @@ -16,16 +22,54 @@ func main() { beego.Run() } + loadConfig() + routers.Init() services.Init() - + if beego.BConfig.RunMode == "dev" { // beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" } + beego.Run() defer func() { services.MongoDisconnect() }() } + +func loadConfig(){ + log := logs.NewLogger(10000) + log.SetLogger("console") + + configFile := "" + var o *onion.Onion + l3 := onion.NewEnvLayerPrefix("_", "OCCATALOG") + 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().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017/beego-demo") + conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC") + conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demdemo_06042021o") + +} diff --git a/services/init.go b/services/init.go index f82ffbf..fdf63d4 100644 --- a/services/init.go +++ b/services/init.go @@ -13,6 +13,7 @@ func Init() { var DBpoint string var err error + DBpoint = os.Getenv("DOCKER_DBPOINT") if len(DBpoint) == 0 { DBpoint, err = beego.AppConfig.String("DBPOINT") diff --git a/services/mongo.go b/services/mongo.go index d16f92e..3ecf258 100644 --- a/services/mongo.go +++ b/services/mongo.go @@ -2,11 +2,10 @@ package services import ( "context" - "os" "time" + "cloud.o-forge.io/core/oc-catalog/conf" "github.com/beego/beego/v2/core/logs" - beego "github.com/beego/beego/v2/server/web" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -56,40 +55,46 @@ func MongoDisconnect() { func Mongoinit(DBname string) { - var baseConfig string + // var baseConfig string + var err error + + MongoURL := conf.GetConfig().MongoURL + + // if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 { + // baseConfig = "mongodb" + // } else { + // baseConfig = "mongodb_docker" + // } - if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 { - baseConfig = "mongodb" - } else { - baseConfig = "mongodb_docker" - } - mongoURI, err := beego.AppConfig.String(baseConfig + "::url") - if err != nil { - logs.Critical("MongoDB URI error: %v", err) - panic(err) - } - - logs.Info("Connecting to %v", mongoURI) - - clientOptions := options.Client().ApplyURI(mongoURI) - // mngoClient, err = mongo.NewClient(options.Client().ApplyURI(mongoURI)) - // if err = mngoClient.Connect(MngoCtx); err != nil { - // logs.Critical("Mongodb NewClient %v: %v", mongoURI, err) + // MongoURL, err := beego.AppConfig.String(baseConfig + "::url") + // if err != nil { + // logs.Critical("MongoDB URI error: %v", err) // panic(err) // } + + logs.Info("Connecting to %v", MongoURL) + + clientOptions := options.Client().ApplyURI(MongoURL) + + mngoClient, _ = mongo.NewClient(options.Client().ApplyURI(MongoURL)) + + if err = mngoClient.Connect(MngoCtx); err != nil { + logs.Critical("Mongodb NewClient %v: %v", MongoURL, err) + panic(err) + } MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() // Ping the primary if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil { - logs.Critical("Mongodb Connect %v: %v", mongoURI, err) + logs.Critical("Mongodb Connect %v: %v", MongoURL, err) panic(err) } if err = mngoClient.Ping(MngoCtx, nil); err != nil { - logs.Critical("Mongodb Ping %v: %v", mongoURI, err) + logs.Critical("Mongodb Ping %v: %v", MongoURL, err) panic(err) }