From e7c0922d7fec38b9441af2952b8aaec040c60ecc Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Sat, 11 Jun 2016 12:13:37 +1000 Subject: [PATCH] Modify pastehandler to allow for shorter query urls --- main.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 492f456..8208be1 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( const ( directory = "/tmp/" - address = "http://localhost:8080/p/" + address = "http://localhost:8080/?" length = 4 text = "$ | curl -F 'paste=<-'" + address + "\n" ) @@ -56,7 +56,15 @@ func save(buf []byte) string { func pasteHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": - io.WriteString(w, text) + param := r.URL.RawQuery + if param != "" { + d := directory + param + s, err := ioutil.ReadFile(d) + check(err) + io.WriteString(w, string(s)) + } else { + io.WriteString(w, text) + } case "POST": buf, _ := ioutil.ReadAll(r.Body) io.WriteString(w, address+save(buf)+"\n") @@ -67,8 +75,6 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) { func main() { http.HandleFunc("/", pasteHandler) - http.Handle("/p/", http.StripPrefix("/p/", http.FileServer(http.Dir(directory)))) - http.ListenAndServe(":8080", nil) }