mirror of
https://github.com/MedzikUser/go-cdn
synced 2024-08-15 03:19:36 +00:00
c77de68966
- Added github workflows and dependabot - Added goreleaser - The Automatic Update system has been changed - Changed DB connect from init to Connect()
32 lines
687 B
Go
32 lines
687 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitlab.com/gaming0skar123/go/cdn/config"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
)
|
|
|
|
var Client *mongo.Client
|
|
var DB *mongo.Database
|
|
|
|
func Connect() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
Client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.Mongo_URI))
|
|
if err != nil {
|
|
panic("Failed Connected to DB: " + err.Error())
|
|
}
|
|
|
|
err = Client.Ping(ctx, readpref.Primary())
|
|
if err != nil {
|
|
panic("Failed Pinging DB: " + err.Error())
|
|
}
|
|
|
|
DB = Client.Database(config.Mongo_Database)
|
|
}
|