aboutsummaryrefslogtreecommitdiff
path: root/db
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
parenthealthcheck + basic api (diff)
downloadctrl-v-bc6947f41facc3ef62fe8f2611551f48a7e3788b.tar.xz
ctrl-v-bc6947f41facc3ef62fe8f2611551f48a7e3788b.zip
working con to mongo
Diffstat (limited to 'db')
-rw-r--r--db/db.go3
-rw-r--r--db/mongo.go39
-rw-r--r--db/schemas.go12
3 files changed, 53 insertions, 1 deletions
diff --git a/db/db.go b/db/db.go
index 9ff1a5e..c170af3 100644
--- a/db/db.go
+++ b/db/db.go
@@ -16,6 +16,7 @@ func init() {
mUser := os.Getenv("MONGO_USER")
mPass := os.Getenv("MONGO_PASS")
+ mIP := os.Getenv("MONGO_SHARD_URL")
- log.Infof("got %s and %s", mUser, mPass)
+ initSessions(mUser, mPass, mIP)
}
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")
+}
diff --git a/db/schemas.go b/db/schemas.go
new file mode 100644
index 0000000..bad1e53
--- /dev/null
+++ b/db/schemas.go
@@ -0,0 +1,12 @@
+package db
+
+import "github.com/globalsign/mgo/bson"
+
+// Paste represents a single paste
+type Paste struct {
+ ID bson.ObjectId `bson:"_id,omitempty"`
+ Hash string
+ Content string
+}
+
+// add timestamp and expiry later