aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorJacky Zhao <[email protected]>2020-05-09 16:02:10 -0700
committerGitHub <[email protected]>2020-05-09 16:02:10 -0700
commit6e69cfa1794d20e3dd41ea9074a7c8a79d51c099 (patch)
treefe078d33c11e21b684af8f400d1667b2411c5415 /api
parentfix port bug (diff)
parentLowercase json response (diff)
downloadctrl-v-6e69cfa1794d20e3dd41ea9074a7c8a79d51c099.tar.xz
ctrl-v-6e69cfa1794d20e3dd41ea9074a7c8a79d51c099.zip
Merge pull request #2 from jackyzha0/api
Add get hash endpoint
Diffstat (limited to 'api')
-rw-r--r--api/api.go1
-rw-r--r--api/routes.go25
2 files changed, 26 insertions, 0 deletions
diff --git a/api/api.go b/api/api.go
index 81ee0a0..c197774 100644
--- a/api/api.go
+++ b/api/api.go
@@ -31,6 +31,7 @@ func Serve(port int) {
r := mux.NewRouter()
r.HandleFunc("/health", healthCheckFunc)
r.HandleFunc("/api", insertFunc).Methods("POST")
+ r.HandleFunc("/api/{hash}", getHashFunc).Methods("GET")
http.Handle("/", r)
diff --git a/api/routes.go b/api/routes.go
index 103de04..ff65e89 100644
--- a/api/routes.go
+++ b/api/routes.go
@@ -1,8 +1,12 @@
package api
import (
+ "encoding/json"
"fmt"
+ "github.com/gorilla/mux"
+ "github.com/jackyzha0/ctrl-v/cache"
"net/http"
+ "time"
"github.com/jackyzha0/ctrl-v/db"
@@ -29,3 +33,24 @@ func insertFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "got err: %s", err.Error())
}
}
+
+func getHashFunc(w http.ResponseWriter, r *http.Request) {
+ hash := mux.Vars(r)["hash"]
+ paste, err := cache.C.Get(hash)
+
+ // if hash was not found
+ if err != nil {
+ w.WriteHeader(http.StatusNotFound)
+ fmt.Fprintf(w, "got err: %s", err.Error())
+ return
+ }
+
+ // otherwise, return paste content and current time
+ w.Header().Set("Content-Type", "application/json")
+ pasteMap := map[string]interface{} {
+ "timestamp": time.Now(),
+ "content": paste.Content,
+ }
+ jsonData, _ := json.Marshal(pasteMap)
+ fmt.Fprintf(w, "%+v", string(jsonData))
+}