Skip to content

Commit

Permalink
adding preflight caching and options response. (lavanet#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet authored Jan 2, 2024
1 parent 0228c06 commit c22a18b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
25 changes: 25 additions & 0 deletions protocol/chainlib/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
common "github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/protocol/metrics"
"github.com/lavanet/lava/utils"
Expand Down Expand Up @@ -280,3 +281,27 @@ func GetRelayTimeout(chainMessage ChainMessage, chainParser ChainParser, timeout
// Set relay timout, increase it every time we fail a relay on timeout
return extraRelayTimeout + time.Duration(timeouts+1)*relayTimeAddition + common.AverageWorldLatency
}

// setup a common preflight and cors configuration allowing wild cards and preflight caching.
func createAndSetupBaseAppListener() *fiber.App {
app := fiber.New(fiber.Config{})
app.Use(favicon.New())
app.Use(func(c *fiber.Ctx) error {
// we set up wild card by default.
c.Set("Access-Control-Allow-Origin", "*")
// set up all allowed methods.
c.Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")

// Handle preflight requests directly
if c.Method() == "OPTIONS" {
// Cache preflight request for 24 hours (in seconds)
c.Set("Access-Control-Max-Age", "86400")
return c.SendStatus(fiber.StatusNoContent)
}
if c.Method() == "DELETE" {
return c.SendStatus(fiber.StatusNoContent)
}
return c.Next()
})
return app
}
13 changes: 12 additions & 1 deletion protocol/chainlib/grpcproxy/grpcproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"

"github.com/gofiber/fiber/v2"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/lavanet/lava/utils"
"golang.org/x/net/http2"
Expand All @@ -23,8 +24,18 @@ func NewGRPCProxy(cb ProxyCallBack, healthCheckPath string) (*grpc.Server, *http
// Set CORS headers
resp.Header().Set("Access-Control-Allow-Origin", "*")
resp.Header().Set("Access-Control-Allow-Headers", "Content-Type,x-grpc-web")
resp.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")

if req.Method == http.MethodOptions {
// Cache preflight request for 24 hours (in seconds)
resp.Header().Set("Access-Control-Max-Age", "86400")
resp.WriteHeader(fiber.StatusNoContent)
_, _ = resp.Write(make([]byte, 0))
return
}

if req.URL.Path == healthCheckPath && req.Method == http.MethodGet {
resp.WriteHeader(200)
resp.WriteHeader(fiber.StatusOK)
_, _ = resp.Write(make([]byte, 0))
return
}
Expand Down
5 changes: 1 addition & 4 deletions protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/websocket/v2"
"github.com/lavanet/lava/protocol/chainlib/chainproxy/rpcInterfaceMessages"
"github.com/lavanet/lava/protocol/common"
Expand Down Expand Up @@ -296,9 +295,7 @@ func (apil *JsonRPCChainListener) Serve(ctx context.Context) {
}
test_mode := common.IsTestMode(ctx)
// Setup HTTP Server
app := fiber.New(fiber.Config{})

app.Use(favicon.New())
app := createAndSetupBaseAppListener()

app.Use("/ws", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
Expand Down
5 changes: 1 addition & 4 deletions protocol/chainlib/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
pairingtypes "github.com/lavanet/lava/x/pairing/types"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/protocol/metrics"
spectypes "github.com/lavanet/lava/x/spec/types"
Expand Down Expand Up @@ -255,9 +254,7 @@ func (apil *RestChainListener) Serve(ctx context.Context) {
}

// Setup HTTP Server
app := fiber.New(fiber.Config{})

app.Use(favicon.New())
app := createAndSetupBaseAppListener()

chainID := apil.endpoint.ChainID
apiInterface := apil.endpoint.ApiInterface
Expand Down
5 changes: 1 addition & 4 deletions protocol/chainlib/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/websocket/v2"
"github.com/lavanet/lava/protocol/chainlib/chainproxy"
"github.com/lavanet/lava/protocol/chainlib/chainproxy/rpcInterfaceMessages"
Expand Down Expand Up @@ -326,12 +325,10 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context) {
}

// Setup HTTP Server
app := fiber.New(fiber.Config{})
app := createAndSetupBaseAppListener()
chainID := apil.endpoint.ChainID
apiInterface := apil.endpoint.ApiInterface

app.Use(favicon.New())

app.Use("/ws", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
Expand Down

0 comments on commit c22a18b

Please sign in to comment.