Modified the conf loading process, now with onion in conf/conf.go
This commit is contained in:
parent
d0870083f7
commit
afae00fe62
5
conf_template.json
Normal file
5
conf_template.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"MongoURL" : "",
|
||||
"DCNAME" : "",
|
||||
"DBPOINT" : ""
|
||||
}
|
@ -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]
|
44
main.go
44
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,6 +22,8 @@ func main() {
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
loadConfig()
|
||||
|
||||
routers.Init()
|
||||
services.Init()
|
||||
|
||||
@ -23,9 +31,45 @@ func main() {
|
||||
// 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")
|
||||
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
||||
if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
|
||||
baseConfig = "mongodb"
|
||||
} else {
|
||||
baseConfig = "mongodb_docker"
|
||||
}
|
||||
MongoURL := conf.GetConfig().MongoURL
|
||||
|
||||
mongoURI, err := beego.AppConfig.String(baseConfig + "::url")
|
||||
if err != nil {
|
||||
logs.Critical("MongoDB URI error: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
// if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
|
||||
// baseConfig = "mongodb"
|
||||
// } else {
|
||||
// baseConfig = "mongodb_docker"
|
||||
// }
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user