diff options
| author | jackyzha0 <[email protected]> | 2020-05-09 17:40:41 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2020-05-09 17:40:41 -0700 |
| commit | 66727df2c1aa369b9250e7596fea6c8ae8385eaa (patch) | |
| tree | 540dc43adfe6f346490993d5d81b42563aeaeada /db/db.go | |
| parent | Merge pull request #2 from jackyzha0/api (diff) | |
| download | ctrl-v-66727df2c1aa369b9250e7596fea6c8ae8385eaa.tar.xz ctrl-v-66727df2c1aa369b9250e7596fea6c8ae8385eaa.zip | |
add document ttl
Diffstat (limited to 'db/db.go')
| -rw-r--r-- | db/db.go | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -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) |