/
handler_operation_build.go
148 lines (115 loc) · 3.63 KB
/
handler_operation_build.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
package digest
import (
"bytes"
"fmt"
"io"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/spikeekips/mitum-currency/currency"
"github.com/spikeekips/mitum/util/hint"
)
var factTypesByHint = map[string]hint.Hinter{
"create-accounts": currency.CreateAccountsHinter,
"key-updater": currency.KeyUpdaterHinter,
"transfers": currency.TransfersHinter,
"currency-register": currency.CurrencyRegisterHinter,
}
func (hd *Handlers) handleOperationBuild(w http.ResponseWriter, r *http.Request) {
if err := LoadFromCache(hd.cache, CacheKeyPath(r), w); err == nil {
return
}
h, err := hd.combineURL(HandlerPathOperationBuild)
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
var hal Hal
hal = NewBaseHal(nil, NewHalLink(h, nil))
for factType := range factTypesByHint {
if h, err := hd.combineURL(HandlerPathOperationBuildFactTemplate, "fact", factType); err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
} else {
hal = hal.AddLink(
fmt.Sprintf("operation-fact:{%s}", factType),
NewHalLink(h, nil).SetTemplated(),
)
}
}
HTTP2WriteHal(hd.enc, w, hal, http.StatusOK)
HTTP2WriteCache(w, CacheKeyPath(r), time.Hour*100*100*100)
}
func (hd *Handlers) handleOperationBuildFactTemplate(w http.ResponseWriter, r *http.Request) {
if err := LoadFromCache(hd.cache, CacheKeyPath(r), w); err == nil {
return
}
factType := mux.Vars(r)["fact"]
hinter, found := factTypesByHint[factType]
if !found {
HTTP2ProblemWithError(w, errors.Errorf("unknown operation, %q", factType), http.StatusNotFound)
return
}
builder := NewBuilder(hd.enc, hd.networkID)
hal, err := builder.FactTemplate(hinter.Hint())
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusBadRequest)
return
}
h, err := hd.combineURL(HandlerPathOperationBuildFactTemplate, "fact", factType)
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
hal = hal.SetSelf(NewHalLink(h, nil))
HTTP2WriteHal(hd.enc, w, hal, http.StatusOK)
HTTP2WriteCache(w, CacheKeyPath(r), time.Hour*100*100*100)
}
func (hd *Handlers) handleOperationBuildFact(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}
if _, err := io.Copy(body, r.Body); err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
builder := NewBuilder(hd.enc, hd.networkID)
hal, err := builder.BuildFact(body.Bytes())
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusBadRequest)
return
}
h, err := hd.combineURL(HandlerPathOperationBuildFact)
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
hal = hal.SetSelf(NewHalLink(h, nil))
HTTP2WriteHal(hd.enc, w, hal, http.StatusOK)
}
func (hd *Handlers) handleOperationBuildSign(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}
if _, err := io.Copy(body, r.Body); err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
builder := NewBuilder(hd.enc, hd.networkID)
hal, err := builder.BuildOperation(body.Bytes())
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusBadRequest)
return
}
if parseBoolQuery(r.URL.Query().Get("send")) {
sl, e := hd.sendSeal(hal.Interface())
if e != nil {
HTTP2ProblemWithError(w, e, http.StatusInternalServerError)
return
}
hal = hal.SetInterface(sl).AddExtras("sent", true)
}
h, err := hd.combineURL(HandlerPathOperationBuildSign)
if err != nil {
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
return
}
hal = hal.SetSelf(NewHalLink(h, nil))
HTTP2WriteHal(hd.enc, w, hal, http.StatusOK)
}