An atproto PDS written in Go
at hailey/tidy 1.4 kB view raw
1package server 2 3import ( 4 "bytes" 5 6 "github.com/bluesky-social/indigo/carstore" 7 "github.com/haileyok/cocoon/internal/helpers" 8 "github.com/haileyok/cocoon/models" 9 "github.com/ipfs/go-cid" 10 cbor "github.com/ipfs/go-ipld-cbor" 11 "github.com/ipld/go-car" 12 "github.com/labstack/echo/v4" 13) 14 15func (s *Server) handleSyncGetRecord(e echo.Context) error { 16 ctx := e.Request().Context() 17 18 did := e.QueryParam("did") 19 collection := e.QueryParam("collection") 20 rkey := e.QueryParam("rkey") 21 22 var urepo models.Repo 23 if err := s.db.Raw("SELECT * FROM repos WHERE did = ?", nil, did).Scan(&urepo).Error; err != nil { 24 s.logger.Error("error getting repo", "error", err) 25 return helpers.ServerError(e, nil) 26 } 27 28 root, blocks, err := s.repoman.getRecordProof(ctx, urepo, collection, rkey) 29 if err != nil { 30 return err 31 } 32 33 buf := new(bytes.Buffer) 34 35 hb, err := cbor.DumpObject(&car.CarHeader{ 36 Roots: []cid.Cid{root}, 37 Version: 1, 38 }) 39 40 if _, err := carstore.LdWrite(buf, hb); err != nil { 41 s.logger.Error("error writing to car", "error", err) 42 return helpers.ServerError(e, nil) 43 } 44 45 for _, blk := range blocks { 46 if _, err := carstore.LdWrite(buf, blk.Cid().Bytes(), blk.RawData()); err != nil { 47 s.logger.Error("error writing to car", "error", err) 48 return helpers.ServerError(e, nil) 49 } 50 } 51 52 return e.Stream(200, "application/vnd.ipld.car", bytes.NewReader(buf.Bytes())) 53}