From e3284153435775f998ec5672b48b3f6b91e8245e Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Sat, 11 Jun 2016 00:29:22 +1000 Subject: [PATCH] Add GET request page --- main.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 3c9e686..69cd22f 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,9 @@ import ( "os" ) +var directory = "/tmp/" +var address = "http://localhost:8080/p/" + func check(e error) { if e != nil { panic(e) @@ -26,16 +29,19 @@ func exists(location string) bool { func generateName() string { s := uniuri.NewLen(4) + file := exists(directory + s) + if file == true { + generateName() + } + return s } func save(buf []byte) string { paste := buf[92 : len(buf)-46] - address := "localhost:8080/p/" - dir := "/tmp/" s := generateName() - loc := dir + s + loc := directory + s err := ioutil.WriteFile(loc, paste, 0644) check(err) @@ -44,9 +50,17 @@ func save(buf []byte) string { return url } -func pasteHandler(w http.ResponseWriter, req *http.Request) { - buf, _ := ioutil.ReadAll(req.Body) - fmt.Fprintf(w, save(buf)) +func pasteHandler(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + text := "$ | curl -F 'paste=<-' " + address + fmt.Fprintf(w, text) + case "POST": + buf, _ := ioutil.ReadAll(r.Body) + fmt.Fprintf(w, save(buf)) + case "DELETE": + // Remove the record. + } } func main() {