Last active
February 23, 2019 12:29
-
-
Save wemgl/4472587915df63b3640fade31f3d6327 to your computer and use it in GitHub Desktop.
Configuration of the MongoDB Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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