-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
489 lines (429 loc) · 10.4 KB
/
app.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path"
leveldb "github.com/ipfs/go-ds-leveldb"
logging "github.com/ipfs/go-log/v2"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/storacha/fam/bucket"
"github.com/storacha/fam/store"
"github.com/storacha/go-ucanto/core/delegation"
"github.com/storacha/go-ucanto/did"
)
var log = logging.Logger("app")
type NodeBuilder interface {
ToIPLD() (datamodel.Node, error)
}
// App struct
type App struct {
ctx context.Context
userdata *store.UserDataStore
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalln("getting user home directory: %w", err)
}
dataDir, err := mkdirp(homeDir, ".fam")
if err != nil {
log.Fatalln("creating data directory: %w", err)
}
dstore, err := leveldb.NewDatastore(dataDir, nil)
if err != nil {
log.Fatalln("creating datastore: %w", err)
}
userdata, err := store.NewUserDataStore(ctx, dstore)
if err != nil {
log.Fatalln(err)
}
a.userdata = userdata
}
func (a *App) shutdown(ctx context.Context) {
err := a.userdata.Close()
if err != nil {
log.Errorln(err)
}
}
func (a *App) ID() (string, error) {
signer, err := a.userdata.ID(a.ctx)
if err != nil {
log.Error(err)
return "", err
}
return marshalJSON(Bytes(signer.Encode()))
}
func (a *App) Buckets() (string, error) {
buckets, err := a.userdata.Buckets(a.ctx)
if err != nil {
log.Error(err)
return "", err
}
return marshalJSON(Buckets(buckets))
}
func (a *App) AddBucket(params string) (string, error) {
proof, err := unmarshalAddBucketParams(params)
if err != nil {
log.Error(err)
return "", err
}
id, err := a.userdata.AddBucket(a.ctx, proof)
if err != nil {
log.Error(err)
return "", err
}
return marshalJSON(Bytes(id.Bytes()))
}
func (a *App) RemoveBucket(params string) error {
id, err := unmarshalRemoveBucketParams(params)
if err != nil {
log.Error(err)
return err
}
return a.userdata.RemoveBucket(a.ctx, id)
}
func (a *App) Root(params string) (string, error) {
id, err := unmarshalRootParams(params)
if err != nil {
log.Error(err)
return "", err
}
bk, err := a.userdata.Bucket(a.ctx, id)
if err != nil {
log.Error(err)
return "", err
}
root, err := bk.Root(a.ctx)
if err != nil {
log.Error(err)
return "", err
}
return marshalJSON(Bytes(root.Binary()))
}
func (a *App) Put(params string) (string, error) {
id, key, value, err := unmarshalPutParams(params)
if err != nil {
log.Error(err)
return "", err
}
bk, err := a.userdata.Bucket(a.ctx, id)
if err != nil {
log.Error(err)
return "", err
}
err = bk.Put(a.ctx, key, value)
if err != nil {
log.Error(err)
return "", err
}
root, err := bk.Root(a.ctx)
if err != nil {
log.Error(err)
return "", err
}
return marshalJSON(Bytes(root.Binary()))
}
func (a *App) Del(params string) (string, error) {
return "", nil
}
func (a *App) Entries(params string) (string, error) {
id, options, err := unmarshalEntriesParams(params)
if err != nil {
log.Error(err)
return "", err
}
size := options.Size
if size == 0 {
size = 10
}
bk, err := a.userdata.Bucket(a.ctx, id)
if err != nil {
log.Error(err)
return "", err
}
var opts []bucket.EntriesOption
if options.Prefix != "" {
opts = append(opts, bucket.WithKeyPrefix(options.Prefix))
} else {
if options.GreaterThan != "" {
opts = append(opts, bucket.WithKeyGreaterThan(options.GreaterThan))
} else if options.GreaterThanOrEqual != "" {
opts = append(opts, bucket.WithKeyGreaterThanOrEqual(options.GreaterThanOrEqual))
}
if options.LessThan != "" {
opts = append(opts, bucket.WithKeyLessThan(options.LessThan))
} else if options.LessThanOrEqual != "" {
opts = append(opts, bucket.WithKeyLessThanOrEqual(options.LessThanOrEqual))
}
}
var entries Entries
for e, err := range bk.Entries(a.ctx, opts...) {
if err != nil {
log.Error(err)
return "", err
}
entries = append(entries, Entry(e))
}
if len(entries) == 0 {
return marshalJSON(entries)
}
start := options.Page * size
if start > int64(len(entries)-1) {
return marshalJSON(Entries{})
}
end := start + size
if end > int64(len(entries)) {
end = int64(len(entries))
}
return marshalJSON(entries[start:end])
}
type Bytes []byte
func (a Bytes) ToIPLD() (datamodel.Node, error) {
np := basicnode.Prototype.Any
nb := np.NewBuilder()
err := nb.AssignBytes(a)
if err != nil {
return nil, err
}
return nb.Build(), nil
}
type Buckets map[did.DID]delegation.Delegation
func (bks Buckets) ToIPLD() (datamodel.Node, error) {
np := basicnode.Prototype.Any
nb := np.NewBuilder()
ma, err := nb.BeginMap(int64(len(bks)))
if err != nil {
return nil, err
}
for id, dlg := range bks {
err = ma.AssembleKey().AssignString(id.String())
if err != nil {
return nil, err
}
b, err := io.ReadAll(dlg.Archive())
if err != nil {
return nil, err
}
err = ma.AssembleValue().AssignBytes(b)
if err != nil {
return nil, err
}
}
err = ma.Finish()
if err != nil {
return nil, err
}
return nb.Build(), nil
}
func unmarshalAddBucketParams(input string) (delegation.Delegation, error) {
np := basicnode.Prototype.Bytes
nb := np.NewBuilder()
err := dagjson.Decode(nb, bytes.NewReader([]byte(input)))
if err != nil {
return nil, fmt.Errorf("decoding params: %w", err)
}
n := nb.Build()
b, err := n.AsBytes()
if err != nil {
return nil, err
}
return delegation.Extract(b)
}
func unmarshalRemoveBucketParams(input string) (did.DID, error) {
np := basicnode.Prototype.Bytes
nb := np.NewBuilder()
err := dagjson.Decode(nb, bytes.NewReader([]byte(input)))
if err != nil {
return did.Undef, fmt.Errorf("decoding params: %w", err)
}
n := nb.Build()
b, err := n.AsBytes()
if err != nil {
return did.Undef, err
}
return did.Decode(b)
}
func unmarshalRootParams(input string) (did.DID, error) {
np := basicnode.Prototype.Bytes
nb := np.NewBuilder()
err := dagjson.Decode(nb, bytes.NewReader([]byte(input)))
if err != nil {
return did.Undef, fmt.Errorf("decoding params: %w", err)
}
n := nb.Build()
b, err := n.AsBytes()
if err != nil {
return did.Undef, err
}
return did.Decode(b)
}
func unmarshalPutParams(input string) (did.DID, string, ipld.Link, error) {
np := basicnode.Prototype.Any
nb := np.NewBuilder()
err := dagjson.Decode(nb, bytes.NewReader([]byte(input)))
if err != nil {
return did.Undef, "", nil, fmt.Errorf("decoding params: %w", err)
}
n := nb.Build()
idn, err := n.LookupByString("id")
if err != nil {
return did.Undef, "", nil, fmt.Errorf("looking up id: %w", err)
}
idBytes, err := idn.AsBytes()
if err != nil {
return did.Undef, "", nil, fmt.Errorf("decoding id as bytes: %w", err)
}
id, err := did.Decode(idBytes)
if err != nil {
return did.Undef, "", nil, fmt.Errorf("decoding id as DID: %w", err)
}
kn, err := n.LookupByString("key")
if err != nil {
return did.Undef, "", nil, fmt.Errorf("looking up key: %w", err)
}
key, err := kn.AsString()
if err != nil {
return did.Undef, "", nil, fmt.Errorf("decoding key as string: %w", err)
}
vn, err := n.LookupByString("value")
if err != nil {
return did.Undef, "", nil, fmt.Errorf("looking up value: %w", err)
}
value, err := vn.AsLink()
if err != nil {
return did.Undef, "", nil, fmt.Errorf("decoding value as link: %w", err)
}
return id, key, value, nil
}
type EntriesOptions struct {
Size int64
Page int64
Prefix string
GreaterThan string
GreaterThanOrEqual string
LessThan string
LessThanOrEqual string
}
type Entries []Entry
func (e Entries) ToIPLD() (datamodel.Node, error) {
np := basicnode.Prototype.Any
nb := np.NewBuilder()
la, err := nb.BeginList(int64(len(e)))
if err != nil {
return nil, err
}
for _, ent := range e {
n, err := ent.ToIPLD()
if err != nil {
return nil, err
}
la.AssembleValue().AssignNode(n)
}
err = la.Finish()
if err != nil {
return nil, err
}
return nb.Build(), nil
}
type Entry struct {
Key string
Value ipld.Link
}
func (e Entry) ToIPLD() (datamodel.Node, error) {
np := basicnode.Prototype.Any
nb := np.NewBuilder()
la, err := nb.BeginList(2)
if err != nil {
return nil, err
}
err = la.AssembleValue().AssignString(e.Key)
if err != nil {
return nil, err
}
err = la.AssembleValue().AssignLink(e.Value)
if err != nil {
return nil, err
}
err = la.Finish()
if err != nil {
return nil, err
}
return nb.Build(), nil
}
func unmarshalEntriesParams(input string) (did.DID, EntriesOptions, error) {
np := basicnode.Prototype.Map
nb := np.NewBuilder()
err := dagjson.Decode(nb, bytes.NewReader([]byte(input)))
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("decoding params: %w", err)
}
n := nb.Build()
idn, err := n.LookupByString("id")
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("looking up id: %w", err)
}
idBytes, err := idn.AsBytes()
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("decoding id as bytes: %w", err)
}
id, err := did.Decode(idBytes)
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("decoding id as DID: %w", err)
}
options := EntriesOptions{}
sn, err := n.LookupByString("size")
if err == nil {
options.Size, err = sn.AsInt()
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("decoding size as int: %w", err)
}
}
pgn, err := n.LookupByString("page")
if err == nil {
options.Page, err = pgn.AsInt()
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("decoding page as int: %w", err)
}
}
pn, err := n.LookupByString("prefix")
if err == nil {
options.Prefix, err = pn.AsString()
if err != nil {
return did.Undef, EntriesOptions{}, fmt.Errorf("decoding prefix as string: %w", err)
}
}
return id, options, nil
}
func mkdirp(dirpath ...string) (string, error) {
dir := path.Join(dirpath...)
err := os.MkdirAll(dir, 0755)
if err != nil {
return "", fmt.Errorf("creating directory: %s: %w", dir, err)
}
return dir, nil
}
func marshalJSON(data NodeBuilder) (string, error) {
n, err := data.ToIPLD()
if err != nil {
return "", err
}
buf := bytes.NewBuffer([]byte{})
err = dagjson.Encode(n, buf)
if err != nil {
return "", err
}
return buf.String(), nil
}