-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
50 lines (43 loc) · 958 Bytes
/
handler.go
File metadata and controls
50 lines (43 loc) · 958 Bytes
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
package connecthttp
import (
"context"
"net/http"
)
type Handler struct {
impl func(w http.ResponseWriter, r *http.Request)
}
func NewHandler[Req, Res any](
_ string,
unary func(context.Context, *Request[Req]) (*Response[Res], error),
options ...HandlerOption,
) *Handler {
config := newHandlerConfig(options...)
impl := func(w http.ResponseWriter, r *http.Request) {
var req Req
if err := config.drf(r, &req); err != nil {
config.eef(w, r, err)
return
}
response, err := unary(r.Context(), NewRequest(&req))
if err != nil {
config.eef(w, r, err)
return
}
if err := config.erf(w, r, response.Any()); err != nil {
config.eef(w, r, err)
return
}
}
return &Handler{
impl: impl,
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tr := &Transport{
request: r,
response: w,
}
tr.request = r.WithContext(NewTransportContext(ctx, tr))
h.impl(w, tr.request)
}