aboutsummaryrefslogtreecommitdiff
path: root/backend/db
diff options
context:
space:
mode:
authorRyan Mehri <[email protected]>2020-05-09 21:17:40 -0600
committerGitHub <[email protected]>2020-05-09 21:17:40 -0600
commit14e120bc3fa4da442af0063c1a00852e67fbc6a2 (patch)
tree9258832576f9c1bcad6126cf76ef8f7f6a128e82 /backend/db
parentMerge pull request #3 from jackyzha0/doc-expiry (diff)
parentfolder refactor (diff)
downloadctrl-v-14e120bc3fa4da442af0063c1a00852e67fbc6a2.tar.xz
ctrl-v-14e120bc3fa4da442af0063c1a00852e67fbc6a2.zip
Merge pull request #4 from jackyzha0/folder-refactor
folder refactor
Diffstat (limited to 'backend/db')
-rw-r--r--backend/db/db.go68
-rw-r--r--backend/db/mongo.go67
-rw-r--r--backend/db/schemas.go15
3 files changed, 150 insertions, 0 deletions
diff --git a/backend/db/db.go b/backend/db/db.go
new file mode 100644
index 0000000..053ba87
--- /dev/null
+++ b/backend/db/db.go
@@ -0,0 +1,68 @@
+package db
+
+import (
+ "fmt"
+ "os"
+ "time"
+
+ "github.com/jackyzha0/ctrl-v/hashing"
+ "github.com/joho/godotenv"
+ log "github.com/sirupsen/logrus"
+)
+
+func init() {
+ // load .env file
+ err := godotenv.Load()
+ if err != nil {
+ log.Fatal("Error loading .env file: %s", err.Error())
+ }
+
+ mUser := os.Getenv("MONGO_USER")
+ mPass := os.Getenv("MONGO_PASS")
+ mIP := os.Getenv("MONGO_SHARD_URL")
+
+ initSessions(mUser, mPass, mIP)
+}
+
+// creates a new paste with content and hash
+func New(ip, content, expiry string) error {
+ // generate hash from ip
+ hash := hashing.GenerateURI(ip)
+
+ // create new struct
+ new := Paste{
+ Hash: hash,
+ Content: content,
+ }
+
+ // check if expiry
+ if expiry != "" {
+ t, err := time.Parse(time.RFC3339, expiry)
+
+ // if time format not current
+ if err != nil {
+ return err
+ }
+
+ // time is in the past
+ if t.After(time.Now()) {
+ return fmt.Errorf("err: time %s is in the past", t.String())
+ }
+
+ new.Expiry = t
+
+ } else {
+ // 5 year expiry
+ new.Expiry = time.Now().Add(time.Hour * 43800)
+ }
+
+ // insert struct
+ log.Infof("create new paste with hash %s", hash)
+ insertErr := insert(new)
+ return insertErr
+}
+
+// lookup
+func Lookup(hash string) (Paste, error) {
+ return fetch(hash)
+}
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
+}
diff --git a/backend/db/schemas.go b/backend/db/schemas.go
new file mode 100644
index 0000000..bdfa60c
--- /dev/null
+++ b/backend/db/schemas.go
@@ -0,0 +1,15 @@
+package db
+
+import (
+ "time"
+
+ "github.com/globalsign/mgo/bson"
+)
+
+// Paste represents a single paste
+type Paste struct {
+ ID bson.ObjectId `bson:"_id,omitempty"`
+ Hash string
+ Content string
+ Expiry time.Time `bson:"expiry"`
+}