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]
|
|
46
main.go
46
main.go
@ -3,12 +3,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-catalog/conf"
|
||||||
"cloud.o-forge.io/core/oc-catalog/routers"
|
"cloud.o-forge.io/core/oc-catalog/routers"
|
||||||
"cloud.o-forge.io/core/oc-catalog/services"
|
"cloud.o-forge.io/core/oc-catalog/services"
|
||||||
|
|
||||||
|
"github.com/beego/beego/logs"
|
||||||
beego "github.com/beego/beego/v2/server/web"
|
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() {
|
func main() {
|
||||||
|
|
||||||
// If we have any parameter, we run the beego directly
|
// If we have any parameter, we run the beego directly
|
||||||
@ -16,16 +22,54 @@ func main() {
|
|||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadConfig()
|
||||||
|
|
||||||
routers.Init()
|
routers.Init()
|
||||||
services.Init()
|
services.Init()
|
||||||
|
|
||||||
if beego.BConfig.RunMode == "dev" {
|
if beego.BConfig.RunMode == "dev" {
|
||||||
// beego.BConfig.WebConfig.DirectoryIndex = true
|
// beego.BConfig.WebConfig.DirectoryIndex = true
|
||||||
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
||||||
}
|
}
|
||||||
|
|
||||||
beego.Run()
|
beego.Run()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
services.MongoDisconnect()
|
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 DBpoint string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
||||||
DBpoint = os.Getenv("DOCKER_DBPOINT")
|
DBpoint = os.Getenv("DOCKER_DBPOINT")
|
||||||
if len(DBpoint) == 0 {
|
if len(DBpoint) == 0 {
|
||||||
DBpoint, err = beego.AppConfig.String("DBPOINT")
|
DBpoint, err = beego.AppConfig.String("DBPOINT")
|
||||||
|
@ -2,11 +2,10 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-catalog/conf"
|
||||||
"github.com/beego/beego/v2/core/logs"
|
"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"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
@ -56,40 +55,46 @@ func MongoDisconnect() {
|
|||||||
|
|
||||||
func Mongoinit(DBname string) {
|
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")
|
// MongoURL, err := beego.AppConfig.String(baseConfig + "::url")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
logs.Critical("MongoDB URI error: %v", err)
|
// 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)
|
|
||||||
// panic(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)
|
MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Ping the primary
|
// Ping the primary
|
||||||
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
|
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)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
|
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)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user