aboutsummaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2020-05-09 20:15:59 -0700
committerjackyzha0 <[email protected]>2020-05-09 20:15:59 -0700
commitdedabf41a18820527aed9e77b75564e69c9030ce (patch)
tree9258832576f9c1bcad6126cf76ef8f7f6a128e82 /db
parentMerge pull request #3 from jackyzha0/doc-expiry (diff)
downloadctrl-v-dedabf41a18820527aed9e77b75564e69c9030ce.tar.xz
ctrl-v-dedabf41a18820527aed9e77b75564e69c9030ce.zip
folder refactor
Diffstat (limited to 'db')
-rw-r--r--db/db.go68
-rw-r--r--db/mongo.go67
-rw-r--r--db/schemas.go15
3 files changed, 0 insertions, 150 deletions
diff --git a/db/db.go b/db/db.go
deleted file mode 100644
index 053ba87..0000000
--- a/db/db.go
+++ /dev/null
@@ -1,68 +0,0 @@
-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/db/mongo.go b/db/mongo.go
deleted file mode 100644
index 4c8a739..0000000
--- a/db/mongo.go
+++ /dev/null
@@ -1,67 +0,0 @@
-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/db/schemas.go b/db/schemas.go
deleted file mode 100644
index bdfa60c..0000000
--- a/db/schemas.go
+++ /dev/null
@@ -1,15 +0,0 @@
-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"`
-}