configuration

This commit is contained in:
mr 2024-07-18 13:55:38 +02:00
parent 9bc77b6699
commit c41eeb969a
2 changed files with 10 additions and 16 deletions

10
conf.go
View File

@ -9,9 +9,8 @@ import "sync"
// ===================================================
type Config struct {
MongoURL string
DCNAME string
DBPOINT string
MongoUrl string
MongoDatabase string
}
var instance *Config
@ -26,9 +25,8 @@ func GetConfig() *Config {
func SetConfig(url string, name string, point string) *Config {
instance = &Config{
MongoURL: url,
DCNAME: name,
DBPOINT: point,
MongoUrl: url,
MongoDatabase: name,
}
return instance
}

View File

@ -39,7 +39,6 @@ func (m *MongoDB) Init(collections []string) {
// var baseConfig string
var err error
var conf map[string]string
var DBname string
m.Logger = logs.CreateLogger("oc-lib", "")
ResourceMap = make(map[string]interface{})
@ -51,17 +50,15 @@ func (m *MongoDB) Init(collections []string) {
}
json.Unmarshal(db_conf, &conf)
DBname = lib.GetConfig().DCNAME + "-" + lib.GetConfig().DBPOINT
logger.Info().Msg("Connecting to" + lib.GetConfig().MongoURL)
logger.Info().Msg("Connecting to" + lib.GetConfig().MongoUrl)
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
m.createClient(lib.GetConfig().MongoURL)
m.createClient(lib.GetConfig().MongoUrl)
logger.Info().Msg("Connecting mongo client to db " + DBname)
m.prepareDB(collections, lib.GetConfig().DCNAME, lib.GetConfig().DBPOINT)
logger.Info().Msg("Connecting mongo client to db " + lib.GetConfig().MongoDatabase)
m.prepareDB(collections)
logger.Info().Msg("Database is READY")
@ -97,10 +94,9 @@ func (m *MongoDB) createClient(MongoURL string) {
}
func (m *MongoDB) prepareDB(list_collection []string, dc_name string, db_point string) {
func (m *MongoDB) prepareDB(list_collection []string) {
var err error
DBname := dc_name + "-" + db_point
mngoDB = mngoClient.Database(DBname)
mngoDB = mngoClient.Database(lib.GetConfig().MongoDatabase)
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
if err != nil {
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())