-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardmiddleware.go
82 lines (65 loc) · 2.51 KB
/
standardmiddleware.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package httperror
import (
"context"
"net/http"
)
type contextKey string
var key = contextKey("key")
// StandardMiddleware is a standard http.Handler wrapper.
type StandardMiddleware = func(http.Handler) http.Handler
type standardMiddleware[P any] struct {
params P
err error
}
// XApplyStandardMiddleware applies middleware written for a standard
// [http.Handler] to an [httperror.XHandler], returning an
// [httperror.XHandler]. It is possible to apply standard middleware to
// [httperror.XHandler] without using this function,because
// [httperror.XHandler] implements the standard [http.Handler] interface.
// However, the result would be an [http.Handler], not an
// [httperror.XHandler], and so parameters could not passed to it and it
// could not return an error. This function solves that problem by passing
// errors and parameters through the context.
func XApplyStandardMiddleware[P any](h XHandler[P], ms ...StandardMiddleware) XHandlerFunc[P] {
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sm := ctx.Value(key).(*standardMiddleware[P])
sm.err = h.Serve(w, r, sm.params)
})
for _, m := range ms {
handler = m(handler)
}
return func(w http.ResponseWriter, r *http.Request, p P) error {
sm := &standardMiddleware[P]{p, nil}
c := r.Context()
c = context.WithValue(c, key, sm)
handler.ServeHTTP(w, r.WithContext(c))
return sm.err
}
}
// ApplyStandardMiddleware applies middleware written for a standard
// [http.Handler] to an [httperror.Handler], returning an
// [httperror.Handler]. It is possible to apply standard middleware to
// [httperror.Handler] without using this function,because
// [httperror.Handler] implements the standard [http.Handler] interface.
// However, the result would be an [http.Handler], not an
// [httperror.Handler], and so parameters could not passed to it and it
// could not return an error. This function solves that problem by passing
// errors and parameters through the context.
func ApplyStandardMiddleware(h Handler, ms ...StandardMiddleware) HandlerFunc {
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sm := ctx.Value(key).(*standardMiddleware[any])
sm.err = h.Serve(w, r)
})
for _, m := range ms {
handler = m(handler)
}
return func(w http.ResponseWriter, r *http.Request) error {
sm := &standardMiddleware[any]{}
c := r.Context()
c = context.WithValue(c, key, sm)
handler.ServeHTTP(w, r.WithContext(c))
return sm.err
}
}