-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathmongo.go
34 lines (30 loc) · 1.03 KB
/
mongo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package bindings
import (
"context"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// MongoBinding checks if the CONNECTIONSTRING environment parameter is present and if so, attempts to connect to a MongoDB
//
// instance using the provided URI, returning a BindingStatus indicating success or failure.
func MongoBinding(envParams map[string]string) BindingStatus {
uri := envParams["CONNECTIONSTRING"]
if uri == "" {
log.Println("CONNECTIONSTRING is required")
return BindingStatus{false, "CONNECTIONSTRING is required"}
}
clientOptions := options.Client().ApplyURI(uri)
ctx := context.Background()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Println("mongodb connection failed", err.Error())
return BindingStatus{false, "mongodb connection failed"}
}
err = client.Ping(ctx, nil)
if err != nil {
log.Println("mongodb connection failed", err.Error())
return BindingStatus{false, "mongodb connection failed"}
}
return BindingStatus{true, "connected"}
}