-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk.go
327 lines (308 loc) · 8.79 KB
/
bulk.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package internal
import (
"bytes"
"context"
"encoding/json"
"io"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/invopop/gobl/data"
"github.com/invopop/gobl/dsig"
"github.com/invopop/gobl/schema"
)
// BulkRequest represents a single request in the stream of bulk requests.
type BulkRequest struct {
// Action is the action to perform on the payload.
Action string `json:"action"`
// ReqID is an opaque request ID, which is returned with the associated
// response.
ReqID string `json:"req_id"`
// Payload is the payload upon which to perform the action.
Payload json.RawMessage `json:"payload"`
// When true, responses are indented for easier human consumption
Indent bool `json:"indent"`
}
// BulkResponse represents a single response in the stream of bulk responses.
type BulkResponse struct {
// ReqID is an exact copy of the value provided in the request, if any.
ReqID string `json:"req_id,omitempty"`
// SeqID is the sequence ID of the request this response correspond to,
// starting at 1.
SeqID int64 `json:"seq_id"`
// Payload is the response payload.
Payload json.RawMessage `json:"payload,omitempty"`
// Error represents an error processing a request item.
Error *Error `json:"error"`
// IsFinal will be true once the end of the request input stream has been
// reached, or an unrecoverable error has occurred.
IsFinal bool `json:"is_final"`
}
// BulkOptions are the options used for processing a stream of bulk requests.
type BulkOptions struct {
// In is the input stream of requests
In io.Reader
// DefaultPrivateKey is the default private key to use with sign requests
DefaultPrivateKey *dsig.PrivateKey
}
// VerifyRequest is the payload for a verification request.
type VerifyRequest struct {
Data []byte `json:"data"`
PublicKey *dsig.PublicKey `json:"publickey"`
}
// VerifyResponse is the response to a verification request.
type VerifyResponse struct {
OK bool `json:"ok"`
}
// ValidateResponse is the response to a validate request.
type ValidateResponse struct {
OK bool `json:"ok"`
}
// BuildRequest is the payload for a build request.
type BuildRequest struct {
Template []byte `json:"template"`
Data []byte `json:"data"`
DocType string `json:"type"`
Envelop bool `json:"envelop"`
}
// SignRequest is the payload for a sign request.
type SignRequest struct {
Template []byte `json:"template"`
Data []byte `json:"data"`
PrivateKey *dsig.PrivateKey `json:"privatekey"`
DocType string `json:"type"`
Envelop bool `json:"envelop"`
}
type ValidateRequest struct {
Data []byte `json:"data"`
}
// KeygenResponse is the payload for a key generation response.
type KeygenResponse struct {
Private *dsig.PrivateKey `json:"private"`
Public *dsig.PublicKey `json:"public"`
}
// CorrectRequest is the payload used to generate a corrected document.
// If the schema option is true, the options data is ignored.
type CorrectRequest struct {
Data []byte `json:"data"`
Options []byte `json:"options"`
Schema bool `json:"schema"`
}
// SchemaRequest defines a body used to request a specific JSON schema
type SchemaRequest struct {
Path string `json:"path"`
}
type RegimeRequest struct {
Code string `json:"code"`
}
// Bulk processes a stream of bulk requests.
func Bulk(ctx context.Context, opts *BulkOptions) <-chan *BulkResponse {
dec := json.NewDecoder(opts.In)
resCh := make(chan *BulkResponse, 1)
wg := &sync.WaitGroup{}
go func() {
var seq int64
defer close(resCh)
for {
seq := atomic.AddInt64(&seq, 1)
var req BulkRequest
err := dec.Decode(&req)
if err != nil {
wg.Wait()
res := &BulkResponse{
ReqID: req.ReqID,
SeqID: seq,
IsFinal: true,
}
if err != io.EOF {
res.Error = wrapError(StatusUnprocessableEntity, err)
}
resCh <- res
return
}
wg.Add(1)
go func() {
resCh <- processRequest(ctx, req, seq, opts)
wg.Done()
}()
}
}()
return resCh
}
func processRequest(ctx context.Context, req BulkRequest, seq int64, bulkOpts *BulkOptions) *BulkResponse {
marshal := json.Marshal
if req.Indent {
marshal = func(i interface{}) ([]byte, error) {
return json.MarshalIndent(i, "", "\t")
}
}
res := &BulkResponse{
ReqID: req.ReqID,
SeqID: seq,
}
switch req.Action {
case "verify":
vrfy := &VerifyRequest{}
if err := json.Unmarshal(req.Payload, vrfy); err != nil {
res.Error = wrapError(StatusUnprocessableEntity, err)
return res
}
err := Verify(ctx, bytes.NewReader(vrfy.Data), vrfy.PublicKey)
if err != nil {
res.Error = wrapError(StatusUnprocessableEntity, err)
return res
}
res.Payload, _ = marshal(VerifyResponse{OK: true})
case "validate":
valReq := &ValidateRequest{}
if err := json.Unmarshal(req.Payload, valReq); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
data := bytes.NewReader(valReq.Data)
if err := Validate(ctx, data); err != nil {
res.Error = wrapError(StatusUnprocessableEntity, err)
return res
}
res.Payload, _ = marshal(ValidateResponse{OK: true})
case "build":
bld := &BuildRequest{}
if err := json.Unmarshal(req.Payload, bld); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
opts := &BuildOptions{
ParseOptions: &ParseOptions{
DocType: bld.DocType,
Input: bytes.NewReader(bld.Data),
Envelop: bld.Envelop,
},
}
if len(bld.Template) > 0 {
opts.Template = bytes.NewReader(bld.Template)
}
env, err := Build(ctx, opts)
if err != nil {
res.Error = wrapError(StatusUnprocessableEntity, err)
return res
}
res.Payload, _ = marshal(env)
case "sign":
bld := &SignRequest{}
if err := json.Unmarshal(req.Payload, bld); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
opts := &SignOptions{
ParseOptions: &ParseOptions{
DocType: bld.DocType,
Input: bytes.NewReader(bld.Data),
},
PrivateKey: bld.PrivateKey,
}
if len(bld.Template) > 0 {
opts.Template = bytes.NewReader(bld.Template)
}
if opts.PrivateKey == nil {
opts.PrivateKey = bulkOpts.DefaultPrivateKey
}
env, err := Sign(ctx, opts)
if err != nil {
res.Error = wrapError(StatusUnprocessableEntity, err)
return res
}
res.Payload, _ = marshal(env)
case "correct":
bld := &CorrectRequest{}
if err := json.Unmarshal(req.Payload, bld); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
opts := &CorrectOptions{
ParseOptions: &ParseOptions{
Input: bytes.NewReader(bld.Data),
},
OptionsSchema: bld.Schema,
Data: bld.Options,
}
env, err := Correct(ctx, opts)
if err != nil {
res.Error = wrapError(StatusUnprocessableEntity, err)
return res
}
res.Payload, _ = marshal(env)
case "keygen":
key := dsig.NewES256Key()
res.Payload, _ = marshal(KeygenResponse{
Private: key,
Public: key.Public(),
})
case "ping":
res.Payload, _ = marshal(map[string]interface{}{
"pong": true,
})
case "sleep":
var delay string
if err := json.Unmarshal(req.Payload, &delay); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
dur, err := time.ParseDuration(delay)
if err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid duration: %w", err)
return res
}
time.Sleep(dur)
res.Payload, _ = marshal(map[string]interface{}{
"sleep": "done",
})
case "schemas":
list := schema.List()
items := make([]string, len(list))
for i, v := range list {
items[i] = v.String()
}
// sorting makes comparisons easier
sort.Strings(items)
res.Payload, _ = marshal(map[string]interface{}{
"list": items,
})
case "schema":
sch := new(SchemaRequest)
if err := json.Unmarshal(req.Payload, sch); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
ext := filepath.Ext(sch.Path)
if ext == "" {
sch.Path = sch.Path + ".json"
}
sch.Path = path.Join("schemas", sch.Path)
data, err := data.Content.ReadFile(sch.Path)
if err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid schema: %w", err)
return res
}
res.Payload = data
case "regime":
reg := new(RegimeRequest)
if err := json.Unmarshal(req.Payload, reg); err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid payload: %w", err)
return res
}
p := path.Join("regimes", strings.ToLower(reg.Code)+".json")
data, err := data.Content.ReadFile(p)
if err != nil {
res.Error = wrapErrorf(StatusUnprocessableEntity, "invalid regime: %w", err)
return res
}
res.Payload = data
default:
res.Error = wrapErrorf(StatusBadRequest, "unrecognized action: '%s'", req.Action)
}
return res
}