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 { type Config struct {
MongoUrl string mongoUrl string
MongoDatabase string mongoDatabase string
}
func (c Config) GetUrl() string {
return c.mongoUrl
}
func (c Config) GetDatabase() string {
return c.mongoDatabase
} }
var instance *Config var instance *Config
@ -25,8 +33,8 @@ func GetConfig() *Config {
func SetConfig(url string, database string) *Config { func SetConfig(url string, database string) *Config {
instance = &Config{ instance = &Config{
MongoUrl: url, mongoUrl: url,
MongoDatabase: database, mongoDatabase: database,
} }
return instance return instance
} }

View File

@ -7,7 +7,6 @@ import (
"os" "os"
"time" "time"
lib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/logs"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -31,11 +30,16 @@ var (
var MONGOService = MongoDB{} var MONGOService = MongoDB{}
type MongoConf interface {
GetUrl() string
GetDatabase() string
}
type MongoDB struct { type MongoDB struct {
Logger zerolog.Logger Logger zerolog.Logger
} }
func (m *MongoDB) Init(collections []string) { func (m *MongoDB) Init(collections []string, config MongoConf) {
// var baseConfig string // var baseConfig string
var err error var err error
var conf map[string]string var conf map[string]string
@ -50,15 +54,15 @@ func (m *MongoDB) Init(collections []string) {
} }
json.Unmarshal(db_conf, &conf) 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) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
m.createClient(lib.GetConfig().MongoUrl) m.createClient(config.GetUrl())
logger.Info().Msg("Connecting mongo client to db " + lib.GetConfig().MongoDatabase) logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase())
m.prepareDB(collections) m.prepareDB(collections, config)
logger.Info().Msg("Database is READY") 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 var err error
mngoDB = mngoClient.Database(lib.GetConfig().MongoDatabase) mngoDB = mngoClient.Database(config.GetDatabase())
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())

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 package models
import ( import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/logs"
r "cloud.o-forge.io/core/oc-lib/models/resources" r "cloud.o-forge.io/core/oc-lib/models/resources"
d "cloud.o-forge.io/core/oc-lib/models/resources/data" d "cloud.o-forge.io/core/oc-lib/models/resources/data"
@ -29,10 +28,10 @@ func Model(model string) utils.DBObject {
return nil return nil
} }
func Init() { func GetModelsNames() []string {
dbs := []string{} names := []string{}
for db := range models { for name := range models {
dbs = append(dbs, db) names = append(names, name)
} }
mongo.MONGOService.Init(dbs) return names
} }