forked from fioprotocol/fiostore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
77 lines (70 loc) · 1.9 KB
/
handler.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
package fiostore
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
)
// ReqHandler is responsible for servicing the incoming request.
func ReqHandler(resp http.ResponseWriter, req *http.Request) {
clientAddr := req.RemoteAddr
if xff {
xh := strings.Split(req.Header.Get("X-Forwarded-For"), ",")
if len(xh) > 0 && xh[0] != "" {
clientAddr = xh[0]
}
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("%s: unable to read body, sending blank response. %v\n", clientAddr, err)
resp.WriteHeader(400)
resp.Write(nil)
return
}
defer req.Body.Close()
fioRequest, status, err := parseRequest(body)
if err != nil {
log.Printf("%s: %v\n", clientAddr, err)
if status == nil {
log.Printf("%s: parse request returned nil status! sending blank response\n", clientAddr)
resp.WriteHeader(500)
resp.Write(nil)
return
}
j, err := json.MarshalIndent(status, "", " ")
if err != nil {
log.Printf("%s: parse request returned invalid status struct! sending blank response %v\n", clientAddr, err)
resp.WriteHeader(500)
resp.Write(nil)
return
}
resp.WriteHeader(status.Code)
resp.Write(j)
return
}
result, err := sendFioRequest(fioRequest)
if err != nil {
log.Printf("%s: %v\n", clientAddr, err)
}
if result == nil {
log.Printf("%s: request to %s, send request provided nil result! sending empty response\n", clientAddr, fioRequest.FioAddress)
resp.WriteHeader(500)
resp.Write(nil)
return
}
j, err := json.MarshalIndent(result, "", " ")
if err != nil {
log.Printf("%s: %v\n", clientAddr, err)
resp.WriteHeader(500)
resp.Write(nil)
return
}
if result.Code == 200 {
log.Printf("%s: SUCCESS: sent request to %s with txid %s\n", clientAddr, fioRequest.FioAddress, result.Txid)
} else {
log.Printf("%s: request to %s failed %s\n", clientAddr, fioRequest.FioAddress, result.Message)
}
resp.WriteHeader(result.Code)
resp.Write(j)
}