This commit is contained in:
mr 2024-07-18 14:11:13 +02:00
parent 2aa9054148
commit 7f1f85c00e
4 changed files with 44 additions and 18 deletions

16
conf.go
View File

@ -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
}

View File

@ -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())

15
entrypoint.go Normal file
View File

@ -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)
}

View File

@ -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
}