From b8a82b283691306306b5aea8fbbff8b85d56ddd2 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Sat, 29 Jul 2023 21:23:31 -0500 Subject: [PATCH] update mongo methods --- pkg/backy/mongo.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/backy/mongo.go b/pkg/backy/mongo.go index 17f8749..4e8d1eb 100644 --- a/pkg/backy/mongo.go +++ b/pkg/backy/mongo.go @@ -30,7 +30,9 @@ func (opts *ConfigOpts) InitMongo() { host := opts.viper.GetString(getMongoConfigKey("host")) port := opts.viper.GetInt32(getMongoConfigKey("port")) - client, err = mongo.NewClient(options.Client().ApplyURI(fmt.Sprintf("mongo://%s:%d", host, port))) + ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second) + defer ctxCancel() + client, err = mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongo://%s:%d", host, port))) if opts.viper.GetBool(getMongoConfigKey("prod")) { mongoEnvFileSet := opts.viper.IsSet(getMongoConfigKey("env")) if mongoEnvFileSet { @@ -39,15 +41,12 @@ func (opts *ConfigOpts) InitMongo() { auth := options.Credential{} auth.Password = opts.viper.GetString("global.mongo.password") auth.Username = opts.viper.GetString("global.mongo.username") - client, err = mongo.NewClient(options.Client().SetAuth(auth).ApplyURI("mongodb://localhost:27017")) + client, err = mongo.Connect(ctx, options.Client().SetAuth(auth).ApplyURI("mongodb://localhost:27017")) } if err != nil { opts.ConfigFile.Logger.Fatal().Err(err).Send() } - ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second) - defer ctxCancel() - err = client.Connect(ctx) if err != nil { opts.ConfigFile.Logger.Fatal().Err(err).Send() }