Skip to content

Commit

Permalink
feat: add corshandler to IPFS gateway (#1589)
Browse files Browse the repository at this point in the history
* add corshandler to IPFS gateway

* use cors lib

* go mod tidy

* refactor test, remove extra cors
  • Loading branch information
LexLuthr authored and willscott committed Sep 12, 2023
1 parent 41ee07f commit 64bd9c3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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 @@ -83,8 +84,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 @@ -106,7 +116,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

0 comments on commit 64bd9c3

Please sign in to comment.