Skip to content

Commit

Permalink
Implement singleton pattern for MongoDB instance (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
greeshiee authored Sep 16, 2024
1 parent d66e72f commit 911f7e2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
49 changes: 31 additions & 18 deletions api/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"strconv"
"sync"
"time"

"github.com/UTDNebula/nebula-api/api/common/log"
Expand All @@ -14,34 +15,46 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

type DBSingleton struct {
client *mongo.Client
}

var dbInstance *DBSingleton
var once sync.Once

func ConnectDB() *mongo.Client {
once.Do(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(GetEnvMongoURI()))
if err != nil {
log.WriteErrorMsg("Unable to create MongoDB client")
os.Exit(1)
}

client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(GetEnvMongoURI()))
if err != nil {
log.WriteErrorMsg("Unable to create MongoDB client")
os.Exit(1)
}
defer cancel()

defer cancel()
//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.WriteErrorMsg("Unable to ping database")
os.Exit(1)
}

//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.WriteErrorMsg("Unable to ping database")
os.Exit(1)
}
log.WriteDebug("Connected to MongoDB")

log.WriteDebug("Connected to MongoDB")
dbInstance = &DBSingleton{
client: client,
}

return client
}
})

var DB *mongo.Client = ConnectDB()
return dbInstance.client
}

// getting database collections
func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
func GetCollection(collectionName string) *mongo.Collection {
client := ConnectDB()
collection := client.Database("combinedDB").Collection(collectionName)
return collection
}
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

var courseCollection *mongo.Collection = configs.GetCollection(configs.DB, "courses")
var courseCollection *mongo.Collection = configs.GetCollection("courses")

func CourseSearch() gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/degree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

var degreeCollection *mongo.Collection = configs.GetCollection(configs.DB, "degrees")
var degreeCollection *mongo.Collection = configs.GetCollection("degrees")

func DegreeSearch() gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

var evaluationCollection *mongo.Collection = configs.GetCollection(configs.DB, "evaluations")
var evaluationCollection *mongo.Collection = configs.GetCollection("evaluations")

func EvalBySectionID(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/exam.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

var examCollection *mongo.Collection = configs.GetCollection(configs.DB, "exams")
var examCollection *mongo.Collection = configs.GetCollection("exams")

type examFilter struct {
Type string `schema:"type"`
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/professor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

var professorCollection *mongo.Collection = configs.GetCollection(configs.DB, "professors")
var professorCollection *mongo.Collection = configs.GetCollection("professors")

func ProfessorSearch() gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

var sectionCollection *mongo.Collection = configs.GetCollection(configs.DB, "sections")
var sectionCollection *mongo.Collection = configs.GetCollection("sections")

func SectionSearch() gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down

0 comments on commit 911f7e2

Please sign in to comment.