Add global constants
This commit is contained in:
parent
a79e880d73
commit
6abd70061d
24
main.go
24
main.go
|
@ -3,13 +3,18 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dchest/uniuri"
|
"github.com/dchest/uniuri"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var directory = "/tmp/"
|
const (
|
||||||
var address = "http://localhost:8080/p/"
|
directory = "/tmp/"
|
||||||
|
address = "http://localhost:8080/p/"
|
||||||
|
length = 4
|
||||||
|
text = "$ <command> | curl -F 'paste=<-'" + address + "\n"
|
||||||
|
)
|
||||||
|
|
||||||
func check(e error) {
|
func check(e error) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
@ -28,7 +33,7 @@ func exists(location string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateName() string {
|
func generateName() string {
|
||||||
s := uniuri.NewLen(4)
|
s := uniuri.NewLen(length)
|
||||||
file := exists(directory + s)
|
file := exists(directory + s)
|
||||||
if file == true {
|
if file == true {
|
||||||
generateName()
|
generateName()
|
||||||
|
@ -41,23 +46,22 @@ func save(buf []byte) string {
|
||||||
paste := buf[92 : len(buf)-46]
|
paste := buf[92 : len(buf)-46]
|
||||||
|
|
||||||
s := generateName()
|
s := generateName()
|
||||||
loc := directory + s
|
location := directory + s
|
||||||
|
|
||||||
err := ioutil.WriteFile(loc, paste, 0644)
|
err := ioutil.WriteFile(location, paste, 0644)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
url := address + s
|
return s
|
||||||
return url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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":
|
||||||
text := "$ <command> | curl -F 'paste=<-' " + address
|
|
||||||
fmt.Fprintf(w, text)
|
fmt.Fprintf(w, text)
|
||||||
case "POST":
|
case "POST":
|
||||||
buf, _ := ioutil.ReadAll(r.Body)
|
buf, _ := ioutil.ReadAll(r.Body)
|
||||||
fmt.Fprintf(w, save(buf))
|
io.WriteString(w, address+save(buf)+"\n")
|
||||||
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
// Remove the record.
|
// Remove the record.
|
||||||
}
|
}
|
||||||
|
@ -65,7 +69,7 @@ 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("/tmp"))))
|
http.Handle("/p/", http.StripPrefix("/p/", http.FileServer(http.Dir(directory))))
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue