Skip to content

Commit 4295edb

Browse files
go mongo pkg
1 parent 5fe4f4c commit 4295edb

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"time"
1010

11+
"github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
1112
"go.mongodb.org/mongo-driver/mongo"
1213
"go.mongodb.org/mongo-driver/mongo/options"
1314
"go.mongodb.org/mongo-driver/mongo/readpref"
@@ -17,6 +18,7 @@ import (
1718
swaggerFiles "github.com/swaggo/files"
1819
ginSwagger "github.com/swaggo/gin-swagger"
1920
_ "github.com/vinodnextcoder/golang-mongo-server/docs"
21+
2022
)
2123

2224
// @title Swagger Example API
@@ -42,18 +44,17 @@ func main() {
4244

4345
// Get Client, Context, CancelFunc and
4446
// err from connect method.
45-
client, ctx, cancel, err := connect("mongodb://localhost:27017/test")
47+
client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
4648
if err != nil {
4749
panic(err)
4850
}
4951

5052
// Release resource when the main
5153
// function is returned.
52-
defer close(client, ctx, cancel)
54+
defer mongoconnect.Close(client, ctx, cancel)
5355

54-
5556
// Ping mongoDB with Ping method
56-
ping(client, ctx)
57+
mongoconnect.Ping(client, ctx)
5758

5859
port := os.Getenv("PORT")
5960

mongoconnect/config.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package mongoconnect
2+
3+
import (
4+
"fmt"
5+
"context"
6+
"time"
7+
"go.mongodb.org/mongo-driver/mongo"
8+
"go.mongodb.org/mongo-driver/mongo/options"
9+
"go.mongodb.org/mongo-driver/mongo/readpref"
10+
)
11+
12+
13+
// This is a user defined method to close resources.
14+
// This method closes mongoDB connection and cancel context.
15+
func Close(client *mongo.Client, ctx context.Context,
16+
cancel context.CancelFunc){
17+
18+
// CancelFunc to cancel to context
19+
defer cancel()
20+
21+
// client provides a method to close
22+
// a mongoDB connection.
23+
defer func(){
24+
25+
// client.Disconnect method also has deadline.
26+
// returns error if any,
27+
if err := client.Disconnect(ctx); err != nil{
28+
panic(err)
29+
}
30+
}()
31+
}
32+
33+
// This is a user defined method that returns mongo.Client,
34+
// context.Context, context.CancelFunc and error.
35+
// mongo.Client will be used for further database operation.
36+
// context.Context will be used set deadlines for process.
37+
// context.CancelFunc will be used to cancel context and
38+
// resource associated with it.
39+
40+
func Connect(uri string)(*mongo.Client, context.Context,
41+
context.CancelFunc, error) {
42+
43+
// ctx will be used to set deadline for process, here
44+
// deadline will of 30 seconds.
45+
ctx, cancel := context.WithTimeout(context.Background(),
46+
30 * time.Second)
47+
48+
// mongo.Connect return mongo.Client method
49+
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
50+
return client, ctx, cancel, err
51+
}
52+
53+
// This is a user defined method that accepts
54+
// mongo.Client and context.Context
55+
// This method used to ping the mongoDB, return error if any.
56+
func Ping(client *mongo.Client, ctx context.Context) error{
57+
58+
// mongo.Client has Ping to ping mongoDB, deadline of
59+
// the Ping method will be determined by cxt
60+
// Ping method return error if any occurred, then
61+
// the error can be handled.
62+
if err := client.Ping(ctx, readpref.Primary()); err != nil {
63+
return err
64+
}
65+
fmt.Println("connected successfully")
66+
return nil
67+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy