forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
65 lines (55 loc) · 1.66 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sdkserver
import (
"net/http"
"runtime/debug"
"github.com/gptscript-ai/gptscript/pkg/context"
"github.com/gptscript-ai/gptscript/pkg/mvl"
)
type middleware func(http.Handler) http.Handler
func apply(h http.Handler, m ...func(http.Handler) http.Handler) http.Handler {
for i := len(m) - 1; i >= 0; i-- {
h = m[i](h)
}
return h
}
func contentType(contentTypes ...string) middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, ct := range contentTypes {
w.Header().Add("Content-Type", ct)
}
h.ServeHTTP(w, r)
})
}
}
func logRequest(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := context.GetLogger(r.Context())
defer func() {
if err := recover(); err != nil {
l.Fields("stack", string(debug.Stack())).Errorf("Panic: %v", err)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"stderr": "encountered an unexpected error"}`))
}
}()
l.Infof("Handling request: method %s, path %s", r.Method, r.URL.Path)
h.ServeHTTP(w, r)
l.Infof("Handled request: method %s, path %s", r.Method, r.URL.Path)
})
}
func addRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithNewRequestID(r.Context())))
})
}
func addLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(
w,
r.WithContext(context.WithLogger(
r.Context(),
mvl.NewWithID(context.GetRequestID(r.Context())),
)),
)
})
}