Modify variable names

This commit is contained in:
Eliot Whalan 2016-06-11 12:31:03 +10:00
parent 1c83f69879
commit 9f9f6665f1
1 changed files with 14 additions and 13 deletions

27
main.go
View File

@ -9,10 +9,11 @@ import (
) )
const ( const (
directory = "/tmp/" DIRECTORY = "/tmp/"
address = "http://localhost:8080/?" ADDRESS = "http://localhost:8080"
length = 4 LENGTH = 4
text = "$ <command> | curl -F 'paste=<-'" + address + "\n" TEXT = "$ <command> | curl -F 'paste=<-'" + ADDRESS + "\n"
PORT = ":8080"
) )
func check(e error) { func check(e error) {
@ -32,8 +33,8 @@ func exists(location string) bool {
} }
func generateName() string { func generateName() string {
s := uniuri.NewLen(length) s := uniuri.NewLen(LENGTH)
file := exists(directory + s) file := exists(DIRECTORY + s)
if file == true { if file == true {
generateName() generateName()
} }
@ -41,11 +42,11 @@ func generateName() string {
return s return s
} }
func save(buf []byte) string { func save(raw []byte) string {
paste := buf[92 : len(buf)-46] paste := raw[92 : len(raw)-46]
s := generateName() s := generateName()
location := directory + s location := DIRECTORY + s
err := ioutil.WriteFile(location, paste, 0644) err := ioutil.WriteFile(location, paste, 0644)
check(err) check(err)
@ -58,17 +59,17 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
case "GET": case "GET":
param := r.URL.RawQuery param := r.URL.RawQuery
if param != "" { if param != "" {
d := directory + param d := DIRECTORY + param
s, err := ioutil.ReadFile(d) s, err := ioutil.ReadFile(d)
check(err) check(err)
io.WriteString(w, string(s)) io.WriteString(w, string(s))
} else { } else {
io.WriteString(w, text) io.WriteString(w, TEXT)
} }
case "POST": case "POST":
buf, err := ioutil.ReadAll(r.Body) buf, err := ioutil.ReadAll(r.Body)
check(err) check(err)
io.WriteString(w, address+save(buf)+"\n") io.WriteString(w, ADDRESS+"?"+save(buf)+"\n")
case "DELETE": case "DELETE":
// Remove the record. // Remove the record.
} }
@ -76,7 +77,7 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/", pasteHandler) http.HandleFunc("/", pasteHandler)
err := http.ListenAndServe(":8080", nil) err := http.ListenAndServe(PORT, nil)
check(err) check(err)
} }