Skip to content

Commit

Permalink
api: cache responses (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: luke miles <[email protected]>
  • Loading branch information
malonehedges and worm-emoji authored Sep 26, 2022
1 parent d48e6d7 commit 013c72c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 4 additions & 0 deletions api/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ func (s *Server) GetProof(w http.ResponseWriter, r *http.Request) {
)
if errors.Is(err, pgx.ErrNoRows) {
s.sendJSONError(r, w, nil, http.StatusNotFound, "tree not found")
w.Header().Set("Cache-Control", "public, max-age=60")
return
} else if err != nil {
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting proof")
return
}

// cache for 1 year if we're returning an unhashed leaf proof
// or 60 seconds for an address proof
if leaf != "" {
w.Header().Set("Cache-Control", "public, max-age=31536000")
} else {
w.Header().Set("Cache-Control", "public, max-age=60")
}
s.sendJSON(r, w, resp)
}
7 changes: 1 addition & 6 deletions api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/jackc/pgx/v4"
)

func proofURLToDBQuery(param string) string {
Expand Down Expand Up @@ -54,15 +53,11 @@ func (s *Server) GetRoot(w http.ResponseWriter, r *http.Request) {
roots := make([]hexutil.Bytes, 0)
rb := make(hexutil.Bytes, 0)

_, err := s.db.QueryFunc(ctx, q, []interface{}{dbQuery}, []interface{}{&rb}, func(qfr pgx.QueryFuncRow) error {
roots = append(roots, rb)
return nil
})

if err != nil {
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting root")
return
} else if len(roots) == 0 { // db.QueryFunc doesn't return pgx.ErrNoRows
w.Header().Set("Cache-Control", "public, max-age=60")
s.sendJSONError(r, w, nil, http.StatusNotFound, "root not found for proofs")
return
}
Expand Down
1 change: 1 addition & 0 deletions api/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
)
if errors.Is(err, pgx.ErrNoRows) {
s.sendJSONError(r, w, nil, http.StatusNotFound, "tree not found for root")
w.Header().Set("Cache-Control", "public, max-age=60")
return
} else if err != nil {
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting tree")
Expand Down

1 comment on commit 013c72c

@vercel
Copy link

@vercel vercel bot commented on 013c72c Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lanyard – ./

allowlist.context.wtf
lanyard.context.wtf
lanyard-git-main.context.wtf

Please sign in to comment.