From 6abd70061de3e586223709fc6c1db3a54edc06d3 Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Sat, 11 Jun 2016 11:22:19 +1000 Subject: [PATCH] Add global constants --- main.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 69cd22f..59066df 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,18 @@ package main import ( "fmt" "github.com/dchest/uniuri" + "io" "io/ioutil" "net/http" "os" ) -var directory = "/tmp/" -var address = "http://localhost:8080/p/" +const ( + directory = "/tmp/" + address = "http://localhost:8080/p/" + length = 4 + text = "$ | curl -F 'paste=<-'" + address + "\n" +) func check(e error) { if e != nil { @@ -28,7 +33,7 @@ func exists(location string) bool { } func generateName() string { - s := uniuri.NewLen(4) + s := uniuri.NewLen(length) file := exists(directory + s) if file == true { generateName() @@ -41,23 +46,22 @@ func save(buf []byte) string { paste := buf[92 : len(buf)-46] s := generateName() - loc := directory + s + location := directory + s - err := ioutil.WriteFile(loc, paste, 0644) + err := ioutil.WriteFile(location, paste, 0644) check(err) - url := address + s - return url + return s } 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)) + io.WriteString(w, address+save(buf)+"\n") + case "DELETE": // Remove the record. } @@ -65,7 +69,7 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) { func main() { http.HandleFunc("/", pasteHandler) - http.Handle("/p/", http.StripPrefix("/p/", http.FileServer(http.Dir("/tmp")))) + http.Handle("/p/", http.StripPrefix("/p/", http.FileServer(http.Dir(directory)))) http.ListenAndServe(":8080", nil)