aboutsummaryrefslogtreecommitdiff
path: root/backend/db
diff options
context:
space:
mode:
authorJacky Zhao <[email protected]>2023-07-09 15:05:12 -0700
committerJacky Zhao <[email protected]>2023-07-09 15:05:12 -0700
commit9853ff01573b9495da848e09a62cd11add19e4bf (patch)
treecbb663bcea9c731b5ffd02c71488682a1dab083e /backend/db
parentadd tls config (diff)
downloadctrl-v-master.tar.xz
ctrl-v-master.zip
move disconnection to cleanupHEADmaster
Diffstat (limited to 'backend/db')
-rw-r--r--backend/db/mongo.go14
1 files changed, 5 insertions, 9 deletions
diff --git a/backend/db/mongo.go b/backend/db/mongo.go
index dc1dd25..f7870ac 100644
--- a/backend/db/mongo.go
+++ b/backend/db/mongo.go
@@ -13,6 +13,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/readpref"
)
+var Client *mongo.Client
var Session *mongo.Session
var pastes *mongo.Collection
@@ -25,21 +26,16 @@ func initSessions(user, pass, ip string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
- client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI).SetTLSConfig(&tls.Config{}))
+ c, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI).SetTLSConfig(&tls.Config{}))
+ Client = c
if err != nil {
log.Fatalf("error establishing connection to mongo: %s", err.Error())
}
- err = client.Ping(ctx, readpref.Primary())
+ err = Client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatalf("error pinging mongo: %s", err.Error())
}
- defer func() {
- if err = client.Disconnect(ctx); err != nil {
- panic(err)
- }
- }()
-
// ensure expiry check
expiryIndex := options.Index().SetExpireAfterSeconds(0)
sessionTTL := mongo.IndexModel{
@@ -55,7 +51,7 @@ func initSessions(user, pass, ip string) {
}
// Define connection to Databases
- pastes = client.Database("main").Collection("pastes")
+ pastes = Client.Database("main").Collection("pastes")
_, _ = pastes.Indexes().CreateOne(ctx, sessionTTL)
_, _ = pastes.Indexes().CreateOne(ctx, uniqueHashes)
}