aboutsummaryrefslogtreecommitdiff
path: root/db/db.go
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2020-05-09 17:40:41 -0700
committerjackyzha0 <[email protected]>2020-05-09 17:40:41 -0700
commit66727df2c1aa369b9250e7596fea6c8ae8385eaa (patch)
tree540dc43adfe6f346490993d5d81b42563aeaeada /db/db.go
parentMerge pull request #2 from jackyzha0/api (diff)
downloadctrl-v-66727df2c1aa369b9250e7596fea6c8ae8385eaa.tar.xz
ctrl-v-66727df2c1aa369b9250e7596fea6c8ae8385eaa.zip
add document ttl
Diffstat (limited to 'db/db.go')
-rw-r--r--db/db.go25
1 files changed, 24 insertions, 1 deletions
diff --git a/db/db.go b/db/db.go
index 4a932f5..053ba87 100644
--- a/db/db.go
+++ b/db/db.go
@@ -1,7 +1,9 @@
package db
import (
+ "fmt"
"os"
+ "time"
"github.com/jackyzha0/ctrl-v/hashing"
"github.com/joho/godotenv"
@@ -23,7 +25,7 @@ func init() {
}
// creates a new paste with content and hash
-func New(ip, content string) error {
+func New(ip, content, expiry string) error {
// generate hash from ip
hash := hashing.GenerateURI(ip)
@@ -33,6 +35,27 @@ func New(ip, content string) error {
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)