Skip to content

Commit

Permalink
👔 Add filter middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Oct 3, 2024
1 parent 395ade1 commit 2ac7992
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
110 changes: 110 additions & 0 deletions rest/filter_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package rest

import (
"bytes"
"encoding/json"

"github.com/gin-gonic/gin"
"github.com/likecoin/likecoin-chain-tx-indexer/logger"
)

type copyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}

func (cw copyWriter) Write(b []byte) (int, error) {
return cw.body.Write(b)
}

func (cw *copyWriter) WriteHeader(statusCode int) {
// Remove content-length header written by proxy
cw.Header().Del("Content-length")
cw.Header().Set("Transfer-Encoding", "chunked")
cw.ResponseWriter.WriteHeader(statusCode)
}

func filterContentFingerprints() gin.HandlerFunc {
return func(c *gin.Context) {
isBrowser := false
switch c.NegotiateFormat(gin.MIMEJSON, gin.MIMEHTML) {
case gin.MIMEHTML:
isBrowser = true
case gin.MIMEJSON:
isBrowser = false
}

if !isBrowser {
c.Next()
return
}

wb := &copyWriter{
body: &bytes.Buffer{},
ResponseWriter: c.Writer,
}
c.Writer = wb
c.Next()

originBody := wb.body
originBodyBytes := originBody.Bytes()

var jsonObject map[string]any
err := json.Unmarshal(originBodyBytes, &jsonObject)
if err != nil {
_, e := wb.ResponseWriter.Write(originBodyBytes)
if e != nil {
logger.L.Error(e)
}
return
}

records, ok := jsonObject["records"].([]interface{})
if !ok {
_, e := wb.ResponseWriter.Write(originBodyBytes)
if e != nil {
logger.L.Error(e)
}
return
}

for index, record := range records {
obj, ok := record.(map[string]any)
if !ok {
continue
}
data, ok := obj["data"].(map[string]any)
if !ok {
continue
}
if data["contentFingerprints"] != nil {
delete(data, "contentFingerprints")
}
if data["contentMetadata"] != nil {
contentMetadata, ok := data["contentMetadata"].(map[string]any)
if ok {
if contentMetadata["sameAs"] != nil {
delete(contentMetadata, "sameAs")
}
data["contentMetadata"] = contentMetadata
}
}
obj["data"] = data
records[index] = obj
}
jsonObject["records"] = records

newBody, err := json.Marshal(jsonObject)
if err != nil {
_, e := wb.ResponseWriter.Write(originBodyBytes)
if e != nil {
logger.L.Error(e)
}
return
}
_, e := wb.ResponseWriter.Write(newBody)
if e != nil {
logger.L.Error(e)
}
}
}
1 change: 1 addition & 0 deletions rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func GetRouter(pool *pgxpool.Pool, defaultApiAddresses []string) *gin.Engine {
analysis.GET("/nft/owner-count", handleNftOwnerCount)
analysis.GET("/nft/owners", handleNftOwnerList)
}
router.Use(filterContentFingerprints())
router.GET(ISCN_ENDPOINT, handleIscn)
router.GET(STARGATE_ENDPOINT, handleStargateTxsSearch)
router.GET(LATEST_HEIGHT_ENDPOINT, handleLatestHeight)
Expand Down

0 comments on commit 2ac7992

Please sign in to comment.