diff options
Diffstat (limited to 'backend/db/mongo.go')
| -rw-r--r-- | backend/db/mongo.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/backend/db/mongo.go b/backend/db/mongo.go new file mode 100644 index 0000000..4c8a739 --- /dev/null +++ b/backend/db/mongo.go @@ -0,0 +1,67 @@ +package db + +import ( + "crypto/tls" + "fmt" + "net" + + "github.com/globalsign/mgo" + "github.com/globalsign/mgo/bson" + log "github.com/sirupsen/logrus" +) + +var Session *mgo.Session +var pastes *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()) + } + + // ensure expiry check + sessionTTL := mgo.Index{ + Key: []string{"expiry"}, + ExpireAfter: 0, + } + + // ensure hashes are unique + uniqueHashes := mgo.Index{ + Key: []string{"hash"}, + Unique: true, + } + + _ = Session.DB("main").C("pastes").EnsureIndex(sessionTTL) + _ = Session.DB("main").C("pastes").EnsureIndex(uniqueHashes) + + // Define connection to Databases + pastes = Session.DB("main").C("pastes") +} + +func insert(new Paste) error { + return pastes.Insert(new) +} + +func fetch(hash string) (Paste, error) { + p := Paste{} + + q := bson.M{"hash": hash} + err := pastes.Find(q).One(&p) + return p, err +} |