Add global constants

This commit is contained in:
Eliot Whalan 2016-06-11 11:22:19 +10:00
parent a79e880d73
commit 6abd70061d
1 changed files with 14 additions and 10 deletions

24
main.go
View File

@ -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 = "$ <command> | 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 := "$ <command> | 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)