2024-03-31 18:23:50 +00:00
|
|
|
package initialize
|
2024-03-31 20:04:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
mg "penahub.gitlab.yandexcloud.net/backend/penahub_common/mongo"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MongoInit(ctx context.Context, config Config) (*mongo.Database, error) {
|
|
|
|
newCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cfg := mg.Configuration{
|
|
|
|
Host: config.MongoHost,
|
|
|
|
Port: config.MongoPort,
|
|
|
|
User: config.MongoUser,
|
|
|
|
Password: config.MongoPassword,
|
|
|
|
Auth: config.MongoAuth,
|
|
|
|
DatabaseName: config.MongoDatabase,
|
|
|
|
}
|
|
|
|
|
|
|
|
deps := mg.ConnectDeps{
|
|
|
|
Configuration: &cfg,
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb, err := mg.Connect(newCtx, &deps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = mdb.Client().Ping(newCtx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return mdb, nil
|
|
|
|
}
|