Modify pastehandler to allow for shorter query urls

This commit is contained in:
Eliot Whalan 2016-06-11 12:13:37 +10:00
parent e197051c6e
commit e7c0922d7f
1 changed files with 10 additions and 4 deletions

14
main.go
View File

@ -10,7 +10,7 @@ import (
const ( const (
directory = "/tmp/" directory = "/tmp/"
address = "http://localhost:8080/p/" address = "http://localhost:8080/?"
length = 4 length = 4
text = "$ <command> | curl -F 'paste=<-'" + address + "\n" text = "$ <command> | curl -F 'paste=<-'" + address + "\n"
) )
@ -56,7 +56,15 @@ func save(buf []byte) string {
func pasteHandler(w http.ResponseWriter, r *http.Request) { func pasteHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "GET": 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": case "POST":
buf, _ := ioutil.ReadAll(r.Body) buf, _ := ioutil.ReadAll(r.Body)
io.WriteString(w, address+save(buf)+"\n") io.WriteString(w, address+save(buf)+"\n")
@ -67,8 +75,6 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/", pasteHandler) http.HandleFunc("/", pasteHandler)
http.Handle("/p/", http.StripPrefix("/p/", http.FileServer(http.Dir(directory))))
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }