24 lines
654 B
Go
24 lines
654 B
Go
package initialization
|
|
|
|
import (
|
|
"context"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
"testData/global"
|
|
)
|
|
|
|
func InitMongoDB() {
|
|
//host := viper.GetString("mongodb.host")
|
|
//port := viper.GetString("mongodb.port")
|
|
//client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://"+host+":"+port))
|
|
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if err = client.Ping(context.TODO(), readpref.Primary()); err != nil {
|
|
panic(err)
|
|
}
|
|
global.MongoDB = client
|
|
}
|