Skip to content

Instantly share code, notes, and snippets.

@wemgl
Last active February 23, 2019 12:29
Show Gist options
  • Save wemgl/4472587915df63b3640fade31f3d6327 to your computer and use it in GitHub Desktop.
Save wemgl/4472587915df63b3640fade31f3d6327 to your computer and use it in GitHub Desktop.
Configuration of the MongoDB Connection
func configDB(ctx context.Context) (*mongo.Database, error) {
uri := fmt.Sprintf(`mongodb://%s:%s@%s/%s`,
ctx.Value(usernameKey).(string),
ctx.Value(passwordKey).(string),
ctx.Value(hostKey).(string),
ctx.Value(databaseKey).(string),
)
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
return nil, fmt.Errorf("todo: couldn't connect to mongo: %v", err)
}
err = client.Connect(ctx)
if err != nil {
return nil, fmt.Errorf("todo: mongo client couldn't connect with background context: %v", err)
}
todoDB := client.Database("todo")
return todoDB, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment