2016-06-10 13:56:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-06-11 03:33:29 +00:00
|
|
|
"fmt"
|
2016-06-10 14:17:27 +00:00
|
|
|
"github.com/dchest/uniuri"
|
2016-06-11 03:09:28 +00:00
|
|
|
"github.com/ewhal/pygments"
|
2016-06-11 01:22:19 +00:00
|
|
|
"io"
|
2016-06-10 13:56:42 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-06-10 14:17:27 +00:00
|
|
|
"os"
|
2016-06-10 13:56:42 +00:00
|
|
|
)
|
|
|
|
|
2016-06-11 01:22:19 +00:00
|
|
|
const (
|
2016-06-11 02:31:03 +00:00
|
|
|
DIRECTORY = "/tmp/"
|
2016-06-11 03:09:28 +00:00
|
|
|
ADDRESS = "http://localhost:8080/"
|
2016-06-11 02:31:03 +00:00
|
|
|
LENGTH = 4
|
2016-06-11 03:09:28 +00:00
|
|
|
TEXT = "$ <command> | curl -F 'p=<-' lang='python'" + ADDRESS + "\n"
|
2016-06-11 02:31:03 +00:00
|
|
|
PORT = ":8080"
|
2016-06-11 01:22:19 +00:00
|
|
|
)
|
2016-06-10 14:29:22 +00:00
|
|
|
|
2016-06-11 03:33:29 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2016-06-10 13:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:27 +00:00
|
|
|
func exists(location string) bool {
|
|
|
|
if _, err := os.Stat(location); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateName() string {
|
2016-06-11 02:31:03 +00:00
|
|
|
s := uniuri.NewLen(LENGTH)
|
|
|
|
file := exists(DIRECTORY + s)
|
2016-06-10 14:29:22 +00:00
|
|
|
if file == true {
|
|
|
|
generateName()
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:27 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
}
|
2016-06-11 02:31:03 +00:00
|
|
|
func save(raw []byte) string {
|
|
|
|
paste := raw[92 : len(raw)-46]
|
2016-06-10 14:17:27 +00:00
|
|
|
|
|
|
|
s := generateName()
|
2016-06-11 02:31:03 +00:00
|
|
|
location := DIRECTORY + s
|
2016-06-10 14:17:27 +00:00
|
|
|
|
2016-06-11 01:22:19 +00:00
|
|
|
err := ioutil.WriteFile(location, paste, 0644)
|
2016-06-10 14:17:27 +00:00
|
|
|
check(err)
|
|
|
|
|
2016-06-11 01:22:19 +00:00
|
|
|
return s
|
2016-06-10 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:29:22 +00:00
|
|
|
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
2016-06-11 03:09:28 +00:00
|
|
|
param1 := r.URL.Query().Get("p")
|
|
|
|
param2 := r.URL.Query().Get("lang")
|
|
|
|
if param1 != "" {
|
|
|
|
d := DIRECTORY + param1
|
2016-06-11 02:13:37 +00:00
|
|
|
s, err := ioutil.ReadFile(d)
|
2016-06-11 03:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
2016-06-11 03:09:28 +00:00
|
|
|
|
|
|
|
if param2 != "" {
|
2016-06-11 03:16:31 +00:00
|
|
|
highlight := pygments.Highlight(string(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
|
2016-06-11 03:09:28 +00:00
|
|
|
io.WriteString(w, string(highlight))
|
2016-06-11 03:33:29 +00:00
|
|
|
|
2016-06-11 03:09:28 +00:00
|
|
|
} else {
|
|
|
|
io.WriteString(w, string(s))
|
|
|
|
}
|
2016-06-11 02:13:37 +00:00
|
|
|
} else {
|
2016-06-11 02:31:03 +00:00
|
|
|
io.WriteString(w, TEXT)
|
2016-06-11 02:13:37 +00:00
|
|
|
}
|
2016-06-10 14:29:22 +00:00
|
|
|
case "POST":
|
2016-06-11 02:22:22 +00:00
|
|
|
buf, err := ioutil.ReadAll(r.Body)
|
2016-06-11 03:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
2016-06-11 03:09:28 +00:00
|
|
|
io.WriteString(w, ADDRESS+"?p="+save(buf)+"\n")
|
2016-06-10 14:29:22 +00:00
|
|
|
case "DELETE":
|
|
|
|
// Remove the record.
|
|
|
|
}
|
2016-06-10 13:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", pasteHandler)
|
2016-06-11 02:31:03 +00:00
|
|
|
err := http.ListenAndServe(PORT, nil)
|
2016-06-11 03:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-06-10 13:56:42 +00:00
|
|
|
|
|
|
|
}
|