Skip to content

Commit 199a582

Browse files
committed
CORS support
1 parent 727668c commit 199a582

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/onsi/gomega v1.29.0 // indirect
4343
github.com/pmezard/go-difflib v1.0.0 // indirect
4444
github.com/rogpeppe/go-internal v1.11.0 // indirect
45+
github.com/rs/cors v1.10.1 // indirect
4546
github.com/samber/lo v1.38.1 // indirect
4647
github.com/samber/slog-logrus v1.0.0 // indirect
4748
github.com/spf13/pflag v1.0.5 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
8787
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
8888
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
8989
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
90+
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
91+
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
9092
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9193
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
9294
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=

pkg/server/server.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/acorn-io/gptscript/pkg/runner"
2121
"github.com/acorn-io/gptscript/pkg/types"
2222
"github.com/olahol/melody"
23+
"github.com/rs/cors"
2324
)
2425

2526
type Options struct {
@@ -170,12 +171,14 @@ func (s *Server) Start(ctx context.Context) error {
170171
s.melody.HandleConnect(s.Connect)
171172
go s.events.Start(ctx)
172173
log.Infof("Listening on http://%s", s.listenAddress)
173-
server := &http.Server{Addr: s.listenAddress, Handler: s}
174+
handler := cors.Default().Handler(s)
175+
server := &http.Server{Addr: s.listenAddress, Handler: handler}
174176
context.AfterFunc(ctx, func() {
175177
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
176178
defer cancel()
177179
_ = server.Shutdown(ctx)
178180
})
181+
179182
return server.ListenAndServe()
180183
}
181184

0 commit comments

Comments
 (0)