From 7f1f85c00e33e6ecc7789b9209354f00413efe52 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 18 Jul 2024 14:11:13 +0200 Subject: [PATCH] accessor --- conf.go | 16 ++++++++++++---- dbs/mongo/mongo.go | 20 ++++++++++++-------- entrypoint.go | 15 +++++++++++++++ models/models.go | 11 +++++------ 4 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 entrypoint.go diff --git a/conf.go b/conf.go index dc50b50..462c556 100644 --- a/conf.go +++ b/conf.go @@ -9,8 +9,16 @@ import "sync" // =================================================== type Config struct { - MongoUrl string - MongoDatabase string + mongoUrl string + mongoDatabase string +} + +func (c Config) GetUrl() string { + return c.mongoUrl +} + +func (c Config) GetDatabase() string { + return c.mongoDatabase } var instance *Config @@ -25,8 +33,8 @@ func GetConfig() *Config { func SetConfig(url string, database string) *Config { instance = &Config{ - MongoUrl: url, - MongoDatabase: database, + mongoUrl: url, + mongoDatabase: database, } return instance } diff --git a/dbs/mongo/mongo.go b/dbs/mongo/mongo.go index 6bacb93..2c4b3bb 100644 --- a/dbs/mongo/mongo.go +++ b/dbs/mongo/mongo.go @@ -7,7 +7,6 @@ import ( "os" "time" - lib "cloud.o-forge.io/core/oc-lib" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/logs" "github.com/rs/zerolog" @@ -31,11 +30,16 @@ var ( var MONGOService = MongoDB{} +type MongoConf interface { + GetUrl() string + GetDatabase() string +} + type MongoDB struct { Logger zerolog.Logger } -func (m *MongoDB) Init(collections []string) { +func (m *MongoDB) Init(collections []string, config MongoConf) { // var baseConfig string var err error var conf map[string]string @@ -50,15 +54,15 @@ func (m *MongoDB) Init(collections []string) { } json.Unmarshal(db_conf, &conf) - logger.Info().Msg("Connecting to" + lib.GetConfig().MongoUrl) + logger.Info().Msg("Connecting to" + config.GetUrl()) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - m.createClient(lib.GetConfig().MongoUrl) + m.createClient(config.GetUrl()) - logger.Info().Msg("Connecting mongo client to db " + lib.GetConfig().MongoDatabase) - m.prepareDB(collections) + logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase()) + m.prepareDB(collections, config) logger.Info().Msg("Database is READY") @@ -94,9 +98,9 @@ func (m *MongoDB) createClient(MongoURL string) { } -func (m *MongoDB) prepareDB(list_collection []string) { +func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) { var err error - mngoDB = mngoClient.Database(lib.GetConfig().MongoDatabase) + mngoDB = mngoClient.Database(config.GetDatabase()) existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{}) if err != nil { m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error()) diff --git a/entrypoint.go b/entrypoint.go new file mode 100644 index 0000000..ce3be1e --- /dev/null +++ b/entrypoint.go @@ -0,0 +1,15 @@ +package oclib + +import ( + "cloud.o-forge.io/core/oc-lib/dbs/mongo" + "cloud.o-forge.io/core/oc-lib/models" + "cloud.o-forge.io/core/oc-lib/models/utils" +) + +func Init() { + mongo.MONGOService.Init(models.GetModelsNames(), GetConfig()) +} + +func Model(name string) utils.DBObject { + return models.Model(name) +} diff --git a/models/models.go b/models/models.go index 86e4d3c..ddcecfa 100644 --- a/models/models.go +++ b/models/models.go @@ -1,7 +1,6 @@ package models import ( - "cloud.o-forge.io/core/oc-lib/dbs/mongo" "cloud.o-forge.io/core/oc-lib/logs" r "cloud.o-forge.io/core/oc-lib/models/resources" d "cloud.o-forge.io/core/oc-lib/models/resources/data" @@ -29,10 +28,10 @@ func Model(model string) utils.DBObject { return nil } -func Init() { - dbs := []string{} - for db := range models { - dbs = append(dbs, db) +func GetModelsNames() []string { + names := []string{} + for name := range models { + names = append(names, name) } - mongo.MONGOService.Init(dbs) + return names }