diff options
| author | Ryan Mehri <[email protected]> | 2020-05-10 17:00:46 -0600 |
|---|---|---|
| committer | Ryan Mehri <[email protected]> | 2020-05-10 17:00:46 -0600 |
| commit | ddbf4935b9b9adffa5f2939e56cb49cbd5139dd1 (patch) | |
| tree | 1d80075cd661b978ebeb681043b3ad110cdbed7a /backend | |
| parent | Merge pull request #11 from jackyzha0/pass_and_expiry (diff) | |
| download | ctrl-v-ddbf4935b9b9adffa5f2939e56cb49cbd5139dd1.tar.xz ctrl-v-ddbf4935b9b9adffa5f2939e56cb49cbd5139dd1.zip | |
Update post to return hash and password
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/api/routes.go | 13 | ||||
| -rw-r--r-- | backend/db/db.go | 17 | ||||
| -rw-r--r-- | backend/db/schemas.go | 11 |
3 files changed, 27 insertions, 14 deletions
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 } |