Skip to content

Commit d746c95

Browse files
Merge pull request #2 from vincent99/main
CORS support
2 parents 8bd960d + 199a582 commit d746c95

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
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

+6-2
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 {
@@ -37,7 +38,7 @@ func complete(opts []Options) (runnerOpts []runner.Options, result Options) {
3738
})
3839
}
3940
if result.ListenAddress == "" {
40-
result.ListenAddress = "127.0.0.1:89090"
41+
result.ListenAddress = "127.0.0.1:9090"
4142
}
4243
return
4344
}
@@ -149,6 +150,7 @@ func (s *Server) run(rw http.ResponseWriter, req *http.Request) {
149150
go func() {
150151
_, _ = s.runner.Run(ctx, prg, os.Environ(), string(body))
151152
}()
153+
rw.Header().Set("Content-Type", "application/json")
152154
err := json.NewEncoder(rw).Encode(map[string]any{
153155
"id": id,
154156
})
@@ -169,12 +171,14 @@ func (s *Server) Start(ctx context.Context) error {
169171
s.melody.HandleConnect(s.Connect)
170172
go s.events.Start(ctx)
171173
log.Infof("Listening on http://%s", s.listenAddress)
172-
server := &http.Server{Addr: s.listenAddress, Handler: s}
174+
handler := cors.Default().Handler(s)
175+
server := &http.Server{Addr: s.listenAddress, Handler: handler}
173176
context.AfterFunc(ctx, func() {
174177
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
175178
defer cancel()
176179
_ = server.Shutdown(ctx)
177180
})
181+
178182
return server.ListenAndServe()
179183
}
180184

0 commit comments

Comments
 (0)