-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
225 lines (186 loc) · 4.96 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
cid "github.com/ipfs/go-cid"
"github.com/julienschmidt/httprouter"
mc "github.com/multiformats/go-multicodec"
mh "github.com/multiformats/go-multihash"
"github.com/rs/cors"
)
const AUTH_TOKEN = "development-token"
var (
publicPath string
ipfsPath string
port int
logger = log.New(os.Stderr, "", log.Ltime)
)
type PinJSONRequestBody struct {
PinataContent interface{} `json:"pinataContent"`
}
type PinJSONResponseBody struct {
IpfsHash string `json:"IpfsHash"`
PinSize int `json:"PinSize"`
Timestamp string `json:"Timestamp"`
}
type WrappedResponseWriter struct {
http.ResponseWriter
lastStatusCode int
}
func (w *WrappedResponseWriter) WriteHeader(statusCode int) {
w.lastStatusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
type LogHandler struct {
handler http.Handler
}
func (l *LogHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ww := &WrappedResponseWriter{w, 200}
l.handler.ServeHTTP(ww, r)
logger.Printf("[%s] %s (%d)", r.Method, r.URL.Path, ww.lastStatusCode)
}
func init() {
flag.IntVar(&port, "port", 0, "http server port")
flag.StringVar(&publicPath, "public", "", "public path")
}
func handleError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
logger.Printf("error: %+v", err)
fmt.Fprint(w, "server error")
}
func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Hello World")
}
func bytesToCID(b []byte) (string, error) {
pref := cid.Prefix{
Version: 1,
Codec: uint64(mc.Raw),
MhType: mh.SHA2_256,
MhLength: -1, // default length
}
hash, err := pref.Sum(b)
if err != nil {
return "", err
}
return hash.String(), nil
}
func handleUpload(w http.ResponseWriter, r *http.Request, content []byte) error {
// generate CID
ipfsHash, err := bytesToCID(content)
if err != nil {
return err
}
// create file using CID as file name
filePath := filepath.Join(ipfsPath, ipfsHash)
logger.Printf("writing to file %s", filePath)
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// write uploadded file content to file
_, err = f.Write(content)
if err != nil {
return err
}
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusCreated)
// encode response body
err = json.NewEncoder(w).Encode(&PinJSONResponseBody{
IpfsHash: ipfsHash,
PinSize: 10,
Timestamp: time.Now().UTC().Format(time.RFC3339),
})
return err
}
func pinJSONHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var body PinJSONRequestBody
// parse request body
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
handleError(w, err)
return
}
// encode JSON file content
content := bytes.NewBuffer([]byte{})
err = json.NewEncoder(content).Encode(body.PinataContent)
if err != nil {
handleError(w, err)
return
}
err = handleUpload(w, r, content.Bytes())
if err != nil {
handleError(w, err)
}
}
func pinFileHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
r.ParseMultipartForm(10 << 20) // max 10MB
// parse uploaded file
file, _, err := r.FormFile("file")
if err != nil {
handleError(w, err)
return
}
defer file.Close()
// read file content
content, err := ioutil.ReadAll(file)
if err != nil {
handleError(w, err)
return
}
err = handleUpload(w, r, content)
if err != nil {
handleError(w, err)
}
}
func authWrapper(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
authorization := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.Replace(authorization, "Bearer", "", 1))
if token != AUTH_TOKEN {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, fmt.Sprintf("access denied, you are not using the development auth token: %s", AUTH_TOKEN))
return
}
next(w, r, params)
}
}
func newRouter(publicPath string) (*httprouter.Router, error) {
ipfsPath = filepath.Join(publicPath, "ipfs")
err := os.MkdirAll(ipfsPath, 0755)
if err != nil {
return nil, err
}
router := httprouter.New()
router.GET("/", indexHandler)
router.POST("/pinning/pinJSONToIPFS", authWrapper(pinJSONHandler))
router.POST("/pinning/pinFileToIPFS", authWrapper(pinFileHandler))
router.NotFound = http.FileServer(http.Dir(publicPath))
return router, nil
}
func main() {
flag.Parse()
if port == 0 || publicPath == "" {
fmt.Println("port and public path flags are mandatory")
flag.Usage()
os.Exit(1)
}
router, err := newRouter(publicPath)
if err != nil {
log.Fatal(err)
}
binding := fmt.Sprintf(":%d", port)
logger.Printf("listening: %s\n", binding)
logger.Printf("public path: %s\n", publicPath)
handler := cors.New(cors.Options{Logger: logger, AllowedHeaders: []string{"*"}}).Handler(router)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), &LogHandler{handler}))
}