From ddbf4935b9b9adffa5f2939e56cb49cbd5139dd1 Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Sun, 10 May 2020 17:00:46 -0600 Subject: Update post to return hash and password --- backend/api/routes.go | 13 ++++++++++++- backend/db/db.go | 17 +++++++++-------- backend/db/schemas.go | 11 ++++++----- 3 files changed, 27 insertions(+), 14 deletions(-) (limited to 'backend') diff --git a/backend/api/routes.go b/backend/api/routes.go index a65c886..3b08781 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -23,6 +23,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { expiry := r.FormValue("expiry") content := r.FormValue("content") title := r.FormValue("title") + password := r.FormValue("password") // get ip ip := getIP(r) @@ -30,11 +31,21 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { log.Infof("got content '%s' and ip '%s'", content, ip) // insert content - err := db.New(ip, content, expiry, title) + paste, err := db.New(ip, content, expiry, title, password) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "got err: %s", err.Error()) } + + // if successful return paste hash + w.Header().Set("Content-Type", "application/json") + pasteMap := map[string]interface{}{ + "hash": paste.Hash, + "password": paste.Password, + } + + jsonData, _ := json.Marshal(pasteMap) + fmt.Fprintf(w, "%+v", string(jsonData)) } func getHashFunc(w http.ResponseWriter, r *http.Request) { diff --git a/backend/db/db.go b/backend/db/db.go index 4451f34..d40de87 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -28,7 +28,7 @@ const TitleLimit = 100 const ContentLimit = 100000 // creates a new paste with title, content and hash -func New(ip, content, expiry, title string) error { +func New(ip, content, expiry, title, password string) (Paste, error) { // generate hash from ip hash := hashing.GenerateURI(ip) @@ -42,14 +42,15 @@ func New(ip, content, expiry, title string) error { } // if any errors were found if errs != "" { - return fmt.Errorf(errs) + return Paste{}, fmt.Errorf(errs) } // create new struct new := Paste{ - Hash: hash, - Content: content, - Title: title, + Hash: hash, + Content: content, + Title: title, + Password: password, } // check if expiry @@ -58,12 +59,12 @@ func New(ip, content, expiry, title string) error { // if time format not current if err != nil { - return err + return Paste{}, err } // time is in the past if time.Now().After(t) { - return fmt.Errorf("time %s is in the past", t.String()) + return Paste{}, fmt.Errorf("time %s is in the past", t.String()) } new.Expiry = t @@ -76,7 +77,7 @@ func New(ip, content, expiry, title string) error { // insert struct log.Infof("create new paste with hash %s", hash) insertErr := insert(new) - return insertErr + return new, insertErr } // lookup diff --git a/backend/db/schemas.go b/backend/db/schemas.go index 62c5a11..4c73f82 100644 --- a/backend/db/schemas.go +++ b/backend/db/schemas.go @@ -8,9 +8,10 @@ import ( // Paste represents a single paste type Paste struct { - ID bson.ObjectId `bson:"_id,omitempty"` - Hash string - Content string - Expiry time.Time `bson:"expiry"` - Title string + ID bson.ObjectId `bson:"_id,omitempty"` + Hash string + Content string + Expiry time.Time `bson:"expiry"` + Title string + Password string } -- cgit v1.2.3