aboutsummaryrefslogtreecommitdiff
path: root/db/mongo.go
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2020-05-09 12:47:39 -0700
committerjackyzha0 <[email protected]>2020-05-09 12:47:39 -0700
commitbc6947f41facc3ef62fe8f2611551f48a7e3788b (patch)
tree5b8820f3184e64be3d3ba58547028ec8f599a0d8 /db/mongo.go
parenthealthcheck + basic api (diff)
downloadctrl-v-bc6947f41facc3ef62fe8f2611551f48a7e3788b.tar.xz
ctrl-v-bc6947f41facc3ef62fe8f2611551f48a7e3788b.zip
working con to mongo
Diffstat (limited to 'db/mongo.go')
-rw-r--r--db/mongo.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/db/mongo.go b/db/mongo.go
new file mode 100644
index 0000000..4c3b88a
--- /dev/null
+++ b/db/mongo.go
@@ -0,0 +1,39 @@
+package db
+
+import (
+ "crypto/tls"
+ "fmt"
+ "net"
+
+ "github.com/globalsign/mgo"
+ log "github.com/sirupsen/logrus"
+)
+
+var Session *mgo.Session
+var TextDB *mgo.Collection
+
+func initSessions(user, pass, ip string) {
+ log.Infof("attempting connection to %s", ip)
+
+ // build uri string
+ URIfmt := "mongodb://%s:%s@%s:27017"
+ mongoURI := fmt.Sprintf(URIfmt, user, pass, ip)
+ dialInfo, err := mgo.ParseURL(mongoURI)
+ if err != nil {
+ log.Fatalf("error parsing uri: %s", err.Error())
+ }
+
+ tlsConfig := &tls.Config{}
+ dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
+ conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
+ return conn, err
+ }
+
+ Session, err = mgo.DialWithInfo(dialInfo)
+ if err != nil {
+ log.Fatalf("error establishing connection to mongo: %s", err.Error())
+ }
+
+ // Define connection to Databases
+ TextDB = Session.DB("main").C("pastes")
+}