23 lines
542 B
Go
23 lines
542 B
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func New(ctx context.Context, dsn string) (*mongo.Client, error) {
|
|
client, err := mongo.NewClient(options.Client().ApplyURI(dsn))
|
|
if err != nil {
|
|
return nil, fmt.Errorf(`initializing MongoDB connection: %w`, err)
|
|
}
|
|
toCtx, _ := context.WithTimeout(ctx, 10*time.Second)
|
|
err = client.Connect(toCtx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(`connecting to MongoDB: %w`, err)
|
|
}
|
|
return client, err
|
|
}
|