package global

import (
	"context"
	"file_upload/simple_zap"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
)

var MongoCon *mongo.Collection

func SetupMongo() {
	clientOptions := options.Client().ApplyURI(MongoSetting.Url)
	connect, err := mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "mongo 出现异常")
		panic(err)
	}
	err = connect.Ping(context.TODO(), readpref.Primary())
	if err != nil {
		simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "mongo 连接失败")
		panic(err)
	}
	database := connect.Database("bzd_file")
	MongoCon = database.Collection("file")
}
