Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add corshandler to IPFS gateway #1589

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/booster-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func TestNewHttpServer(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)

// Create a request with Cors header
LexLuthr marked this conversation as resolved.
Show resolved Hide resolved
req, err := http.NewRequest("GET", "http://localhost:7777/", nil)
require.NoError(t, err)
req.Header.Add("Origin", "test")
client := new(http.Client)
response, err := client.Do(req)
require.NoError(t, err)

// Check for Cors header
require.Equal(t, "*", response.Header.Get("Access-Control-Allow-Origin"))

// Stop the server
err = httpServer.Stop()
require.NoError(t, err)
Expand Down
16 changes: 13 additions & 3 deletions cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/hashicorp/go-multierror"
"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/rs/cors"
"go.opencensus.io/stats"
)

Expand Down Expand Up @@ -82,8 +83,17 @@ func (s *HttpServer) ipfsBasePath() string {
return s.path + "/ipfs/"
}

func newCors() *cors.Cors {
options := cors.Options{
AllowedHeaders: []string{"*"},
}
c := cors.New(options)
return c
}

func (s *HttpServer) Start(ctx context.Context) error {
s.ctx, s.cancel = context.WithCancel(ctx)
c := newCors()
handler := http.NewServeMux()

if s.opts.ServePieces {
Expand All @@ -105,7 +115,7 @@ func (s *HttpServer) Start(ctx context.Context) error {
handler.Handle("/metrics", metrics.Exporter("booster_http")) // metrics
s.server = &http.Server{
Addr: fmt.Sprintf("%s:%d", s.listenAddr, s.port),
Handler: handler,
Handler: c.Handler(handler),
// This context will be the parent of the context associated with all
// incoming requests
BaseContext: func(listener net.Listener) context.Context {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ require (
github.com/prometheus/procfs v0.9.0 // indirect
github.com/prometheus/statsd_exporter v0.22.7 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/rs/cors v1.7.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
Expand Down
20 changes: 0 additions & 20 deletions gql/cors.go

This file was deleted.

18 changes: 13 additions & 5 deletions gql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/graph-gophers/graphql-go/relay"
"github.com/graph-gophers/graphql-transport-ws/graphqlws"
logging "github.com/ipfs/go-log/v2"
"github.com/rs/cors"
)

var log = logging.Logger("gql")
Expand All @@ -31,13 +32,13 @@ type Server struct {
}

func NewServer(cfg *config.Boost, resolver *resolver, bstore BlockGetter) *Server {
webCfg := &corsHandler{sub: &webConfigServer{
webCfg := &webConfigServer{
cfg: webConfig{
Ipni: webConfigIpni{
IndexerHost: cfg.IndexProvider.WebHost,
},
},
}}
}
return &Server{resolver: resolver, bstore: bstore, cfgHandler: webCfg}
}

Expand All @@ -49,6 +50,13 @@ func (s *Server) Start(ctx context.Context) error {

// Serve React app
mux := http.NewServeMux()

// Get new cors
options := cors.Options{
AllowedHeaders: []string{"*"},
}
c := cors.New(options)

LexLuthr marked this conversation as resolved.
Show resolved Hide resolved
err := s.serveReactApp(mux)
if err != nil {
return err
Expand Down Expand Up @@ -89,10 +97,10 @@ func (s *Server) Start(ctx context.Context) error {
wsHandler := graphqlws.NewHandlerFunc(schema, queryHandler, wsOpts...)

listenAddr := fmt.Sprintf("%s:%d", bindAddress, port)
s.srv = &http.Server{Addr: listenAddr, Handler: mux}
s.srv = &http.Server{Addr: listenAddr, Handler: c.Handler(mux)}
fmt.Printf("Graphql server listening on %s\n", listenAddr)
mux.Handle("/graphql/subscription", &corsHandler{wsHandler})
mux.Handle("/graphql/query", &corsHandler{queryHandler})
mux.Handle("/graphql/subscription", wsHandler)
mux.Handle("/graphql/query", queryHandler)

s.wg.Add(1)
go func() {
Expand Down