aboutsummaryrefslogtreecommitdiff
path: root/backend/api
diff options
context:
space:
mode:
authorJacky Zhao <[email protected]>2020-05-10 17:12:23 -0700
committerGitHub <[email protected]>2020-05-10 17:12:23 -0700
commit9c73e3ea82e9a00eecc49db8ee8f10ac030200bd (patch)
tree78f9e2b49ef9ce266c58dad2d26b8fdce2db7ff9 /backend/api
parentMerge pull request #14 from jackyzha0/readme (diff)
parentAdd auth check to get (diff)
downloadctrl-v-9c73e3ea82e9a00eecc49db8ee8f10ac030200bd.tar.xz
ctrl-v-9c73e3ea82e9a00eecc49db8ee8f10ac030200bd.zip
Merge pull request #12 from jackyzha0/backend
Update post to return hash and password
Diffstat (limited to 'backend/api')
-rw-r--r--backend/api/routes.go27
1 files changed, 23 insertions, 4 deletions
diff --git a/backend/api/routes.go b/backend/api/routes.go
index 07bea5a..7fb2114 100644
--- a/backend/api/routes.go
+++ b/backend/api/routes.go
@@ -27,6 +27,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)
@@ -34,11 +35,20 @@ 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)
+ hash, 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": hash,
+ }
+
+ jsonData, _ := json.Marshal(pasteMap)
+ fmt.Fprintf(w, "%+v", string(jsonData))
}
func getHashFunc(w http.ResponseWriter, r *http.Request) {
@@ -50,17 +60,26 @@ func getHashFunc(w http.ResponseWriter, r *http.Request) {
paste, err := cache.C.Get(hash)
// if hash was not found
- if err != nil {
+ if err == cache.PasteNotFound {
w.WriteHeader(http.StatusNotFound)
- fmt.Fprintf(w, "got err: %s", err.Error())
+ fmt.Fprintf(w, "got err: %s", err)
+ return
+ }
+
+ // if paste is password protected
+ if err == cache.UserUnauthorized {
+ w.WriteHeader(http.StatusUnauthorized)
+ fmt.Fprintf(w, "got err: %s", err)
return
}
- // otherwise, return paste content and current time
+ // otherwise, return paste content, title, and current time
w.Header().Set("Content-Type", "application/json")
pasteMap := map[string]interface{}{
"timestamp": time.Now(),
+ "title": paste.Title,
"content": paste.Content,
+ "expiry": paste.Expiry,
}
jsonData, _ := json.Marshal(pasteMap)