From 66727df2c1aa369b9250e7596fea6c8ae8385eaa Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Sat, 9 May 2020 17:40:41 -0700 Subject: add document ttl --- db/db.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'db/db.go') 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) -- cgit v1.2.3