35 lines
617 B
Go
35 lines
617 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
mongoOpt "go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func main() {
|
|
uri := os.Getenv("MONGO_URI")
|
|
|
|
opts := mongoOpt.Client().ApplyURI(uri)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
|
|
dbClient, err := mongo.Connect(ctx, opts)
|
|
if err != nil {
|
|
panic(fmt.Errorf("connect: %e", err))
|
|
}
|
|
|
|
for {
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
fmt.Printf("ping returned: %e", dbClient.Ping(ctx, nil))
|
|
cancel()
|
|
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|