package services import ( "context" "os" "time" "github.com/beego/beego/v2/core/logs" beego "github.com/beego/beego/v2/server/web" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/x/bsonx" ) type MongoCollectionNames struct { DATA string COMPUTING string STORAGE string DATACENTER string WORKSPACE string SCHEDULE string } var ( mngoClient *mongo.Client mngoDB *mongo.Database MngoCtx context.Context MngoNamesCollection = MongoCollectionNames{ DATA: "datas", COMPUTING: "computings", STORAGE: "storages", DATACENTER: "datacenters", WORKSPACE: "WORKSPACE", SCHEDULE: "SCHEDULE", } MngoCollData *mongo.Collection MngoCollComputing *mongo.Collection MngoCollStorage *mongo.Collection MngoCollDatacenter *mongo.Collection MngoCollWorkspace *mongo.Collection MngoCollSchedule *mongo.Collection ) // func GetMongoDBclient() *mongo.Client { // return mongoDBclient // } func MongoDisconnect() { if err := mngoClient.Disconnect(MngoCtx); err != nil { panic(err) } } func Mongoinit(DBname string) { var baseConfig string if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 { baseConfig = "mongodb" } else { baseConfig = "mongodb_docker" } mongoURI, err := beego.AppConfig.String(baseConfig + "::url") if err != nil { logs.Critical("MongoDB URI error: %v", err) panic(err) } logs.Info("Connecting to %v", mongoURI) clientOptions := options.Client().ApplyURI(mongoURI) // mngoClient, err = mongo.NewClient(options.Client().ApplyURI(mongoURI)) // if err = mngoClient.Connect(MngoCtx); err != nil { // logs.Critical("Mongodb NewClient %v: %v", mongoURI, err) // panic(err) // } MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() // Ping the primary if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil { logs.Critical("Mongodb Connect %v: %v", mongoURI, err) panic(err) } if err = mngoClient.Ping(MngoCtx, nil); err != nil { logs.Critical("Mongodb Ping %v: %v", mongoURI, err) panic(err) } mngoDB = mngoClient.Database(DBname) MngoCollData = mngoDB.Collection(MngoNamesCollection.DATA) MngoCollComputing = mngoDB.Collection(MngoNamesCollection.COMPUTING) MngoCollStorage = mngoDB.Collection(MngoNamesCollection.STORAGE) MngoCollDatacenter = mngoDB.Collection(MngoNamesCollection.DATACENTER) MngoCollWorkspace = mngoDB.Collection(MngoNamesCollection.WORKSPACE) MngoCollSchedule = mngoDB.Collection(MngoNamesCollection.SCHEDULE) if _, err = MngoCollData.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{ { Keys: bsonx.Doc{ {Key: "description", Value: bsonx.String("text")}, {Key: "example", Value: bsonx.String("text")}, }, }, }); err != nil && err.(mongo.CommandError).Code != 85 { logs.Critical(err) panic(err) } if _, err = MngoCollComputing.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{ { Keys: bsonx.Doc{ {Key: "description", Value: bsonx.String("text")}, {Key: "owner", Value: bsonx.String("text")}, {Key: "license", Value: bsonx.String("text")}, }, }, }); err != nil && err.(mongo.CommandError).Code != 85 { logs.Critical(err) panic(err) } if _, err = MngoCollStorage.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{ { Keys: bsonx.Doc{ {Key: "name", Value: bsonx.String("text")}, {Key: "description", Value: bsonx.String("text")}, }, }, }); err != nil && err.(mongo.CommandError).Code != 85 { logs.Critical(err) panic(err) } if _, err = MngoCollDatacenter.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{ { Keys: bsonx.Doc{ {Key: "name", Value: bsonx.String("text")}, {Key: "description", Value: bsonx.String("text")}, {Key: "owner", Value: bsonx.String("text")}, }, }, }); err != nil && err.(mongo.CommandError).Code != 85 { logs.Critical(err) panic(err) } logs.Info("Database is READY") }