timetable-mgr/src/mongo/client.go

30 lines
765 B
Go
Raw Normal View History

2023-07-15 23:00:48 +01:00
package database
2023-07-15 22:32:46 +01:00
import (
2023-07-15 23:00:48 +01:00
"git.fjla.uk/owlboard/mq-client/helpers"
2023-07-15 22:32:46 +01:00
"context"
"time"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
// "go.mongodb.org/mongo-driver/mongo/readpref"
)
2023-07-15 23:00:48 +01:00
// Provide the DB Connection to other functions
var MongoClient (*mongo.Client) = initDataAccess()
2023-07-15 22:32:46 +01:00
2023-07-15 23:00:48 +01:00
// Initialise the DB Connection
2023-07-15 22:32:46 +01:00
func initDataAccess() (*mongo.Client) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUri))
if err != nil {
fmt.Println(err)
2023-07-15 23:00:48 +01:00
helpers.Log("Error connecting to database", 4)
2023-07-15 22:32:46 +01:00
} else {
2023-07-15 23:00:48 +01:00
helpers.Log("Connected to Database", 1)
2023-07-15 22:32:46 +01:00
}
return client
}