38 lines
724 B
Go
38 lines
724 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
)
|
|
|
|
var MongoDB *mongo.Client
|
|
var PreUserData *mongo.Collection
|
|
|
|
func InitMongoDriver(mongoUri string) *mongo.Client {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoUri))
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = client.Ping(ctx, readpref.Primary())
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
} else {
|
|
fmt.Println("MongoDB is ready")
|
|
}
|
|
|
|
MongoDB = client
|
|
PreUserData = MongoDB.Database("Totoday-shop").Collection("preusers")
|
|
|
|
return client
|
|
}
|