Skip to content

Commit

Permalink
ft: add caller mw
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul0tripathi committed Jun 4, 2024
1 parent c954cc9 commit f78fdd3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/server/mw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package server

import (
"context"

"github.com/ethereum/go-ethereum/common"
"github.com/labstack/echo/v4"
)

const (
_headerCaller = "X-Caller"
)

type Caller struct{}

func SetResponseHeaderMw(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("Content-Type", "application/json")
return next(c)
}
}

func SetCallerContextMw(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
caller := c.Request().Header.Get(_headerCaller)
if common.IsHexAddress(caller) {
ctx := context.WithValue(c.Request().Context(), Caller{}, common.HexToAddress(caller))
c.SetRequest(c.Request().WithContext(ctx))
}

return next(c)
}
}

0 comments on commit f78fdd3

Please sign in to comment.