Skip to content

Commit 01273d5

Browse files
Fix missing context
1 parent 912d1b4 commit 01273d5

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pkg/server/server.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gptscript-ai/gptscript/pkg/loader"
2020
"github.com/gptscript-ai/gptscript/pkg/runner"
2121
"github.com/gptscript-ai/gptscript/pkg/types"
22+
"github.com/gptscript-ai/gptscript/static"
2223
"github.com/olahol/melody"
2324
"github.com/rs/cors"
2425
)
@@ -169,10 +170,10 @@ func (s *Server) run(rw http.ResponseWriter, req *http.Request) {
169170
}
170171

171172
id := fmt.Sprint(atomic.AddInt64(&execID, 1))
172-
ctx := context.WithValue(req.Context(), execKey{}, id)
173173
if req.URL.Query().Has("async") {
174+
ctx := context.WithValue(s.ctx, execKey{}, id)
174175
go func() {
175-
_, _ = runner.Run(s.ctx, prg, os.Environ(), string(body))
176+
_, _ = runner.Run(ctx, prg, os.Environ(), string(body))
176177
}()
177178
rw.Header().Set("Content-Type", "application/json")
178179
err := json.NewEncoder(rw).Encode(map[string]any{
@@ -182,6 +183,7 @@ func (s *Server) run(rw http.ResponseWriter, req *http.Request) {
182183
http.Error(rw, err.Error(), http.StatusInternalServerError)
183184
}
184185
} else {
186+
ctx := context.WithValue(req.Context(), execKey{}, id)
185187
out, err := runner.Run(ctx, prg, os.Environ(), string(body))
186188
if err == nil {
187189
_, _ = rw.Write([]byte(out))
@@ -228,6 +230,18 @@ func (s *Server) Connect(session *melody.Session) {
228230
}
229231

230232
func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
233+
if strings.HasPrefix(req.URL.Path, "/ui") {
234+
path := req.URL.Path
235+
if path == "/ui" || path == "/ui/" {
236+
path = "/ui/index.html"
237+
}
238+
if _, err := fs.Stat(static.UI, path[1:]); errors.Is(err, fs.ErrNotExist) {
239+
path = "/ui/index.html"
240+
}
241+
http.ServeFileFS(rw, req, static.UI, path)
242+
return
243+
}
244+
231245
switch req.Method {
232246
case http.MethodPost:
233247
s.run(rw, req)

static/fs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package static
2+
3+
import (
4+
"embed"
5+
_ "embed"
6+
)
7+
8+
// /go:embed ui/** ui/**/_*
9+
var UI embed.FS

0 commit comments

Comments
 (0)