-
Notifications
You must be signed in to change notification settings - Fork 0
/
pst.go
137 lines (93 loc) · 2.21 KB
/
pst.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
"github.com/zidhuss/pst/db"
)
const programURL = "pst.zidhuss.tech"
type app struct {
db *db.PasteDatabase
}
func main() {
pst := &app{db.CreatePasteDatabase("pst.db")}
err := http.ListenAndServe("127.0.0.1:8081", handler(pst))
if err != nil {
log.Fatal(err)
}
}
func handler(app *app) http.Handler {
r := mux.NewRouter()
r.HandleFunc("/", app.home).
Methods("GET")
r.HandleFunc("/", app.postPaste).
Methods("POST")
r.HandleFunc("/{pasteID}", app.retrievePaste)
return r
}
func (pst *app) retrievePaste(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pasteID := vars["pasteID"]
paste, err := pst.db.RetrievePaste(pasteID)
if err != nil {
// TODO: 404?
log.Printf("%s\n", err)
return
}
w.Write(paste.Data)
}
func (pst *app) postPaste(w http.ResponseWriter, r *http.Request) {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
r.Body = http.MaxBytesReader(w, r.Body, 2*1024*1024) // 2 Mb
for i := 1; ; i++ {
key := fmt.Sprintf("f:%d", i)
contents := []byte(r.FormValue(key))
if len(contents) == 0 {
f, _, err := r.FormFile(key)
if err != nil {
break
}
contents, err = ioutil.ReadAll(f)
if err != nil {
break
}
}
paste, err := pst.db.StorePaste(contents)
if err != nil {
fmt.Fprintf(w, "%s", err)
return
}
fmt.Fprintf(w, "https://%s/%s\n", programURL, paste.ID)
log.Printf("Storing paste %s from %s\n", paste.ID, ip)
}
}
func (pst *app) home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, Help)
}
var Help = fmt.Sprintf(`
pst(1) PST pst(1)
NAME
pst: command line pastebin.
TL;DR
~$ echo Hello world. | curl -F 'f:1=<-' %[1]s
https://%[1]s/fpW
GET
%[1]s/ID
raw
POST
%[1]s/
f:N contents or attached file.
where N is a unique number within request. (This allows you to post
multiple files at once.)
returns: https://%[1]s/id for N in request
EXAMPLES
Anonymous, unnamed paste, two ways:
cat file.ext | curl -F 'f:1=<-' %[1]s
curl -F 'f:1=@file.ext' %[1]s
SEE ALSO
https://github.com/zidhuss/pst
`, programURL)