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 { type Config struct {
MongoURL string MongoUrl string
DCNAME string MongoDatabase string
DBPOINT string
} }
var instance *Config var instance *Config
@ -26,9 +25,8 @@ func GetConfig() *Config {
func SetConfig(url string, name string, point string) *Config { func SetConfig(url string, name string, point string) *Config {
instance = &Config{ instance = &Config{
MongoURL: url, MongoUrl: url,
DCNAME: name, MongoDatabase: name,
DBPOINT: point,
} }
return instance return instance
} }

View File

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