|
|
@ -0,0 +1,27 @@ |
|
|
|
package db |
|
|
|
|
|
|
|
import ( |
|
|
|
"LAPP_ACURA_MOM_BACKEND/conf" |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"go.mongodb.org/mongo-driver/mongo" |
|
|
|
"go.mongodb.org/mongo-driver/mongo/options" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
var AppMongo *mongo.Client |
|
|
|
|
|
|
|
func InitMongo() error { |
|
|
|
//1.建立链接
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) |
|
|
|
dsn := fmt.Sprintf("mongodb://%s:%d", conf.DbConfig.Mongdbip, conf.DbConfig.Mongdbport) |
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(dsn)) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if err = client.Ping(ctx, nil); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
AppMongo = client |
|
|
|
return err |
|
|
|
} |