-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
93 lines (69 loc) · 1.76 KB
/
handler.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
83
84
85
86
87
88
89
90
91
92
93
package rpc
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"github.com/alexfalkowski/go-service/marshaller"
"github.com/alexfalkowski/go-service/net/http/content"
)
var (
// ErrReadAll for HTTP.
ErrReadAll = errors.New("invalid body read")
// ErrUnmarshal for HTTP.
ErrUnmarshal = errors.New("invalid unmarshal")
// ErrHandle for HTTP.
ErrHandle = errors.New("invalid handle")
// ErrMarshal for HTTP.
ErrMarshal = errors.New("invalid marshal")
)
// Handler for HTTP.
type Handler[Req any, Res any] interface {
// Handle func for request/response.
Handle(ctx Context, req *Req) (*Res, error)
}
var (
mux *http.ServeMux
mar *marshaller.Map
)
// Register for HTTP.
func Register(mu *http.ServeMux, ma *marshaller.Map) {
mux, mar = mu, ma
}
// Handler for HTTP.
func Handle[Req any, Res any](path string, handler Handler[Req, Res]) {
h := func(res http.ResponseWriter, req *http.Request) {
ctx := newContext(req.Context(), req, res)
ct := content.NewFromRequest(req)
m := ct.Marshaller(mar)
res.Header().Add(content.TypeKey, ct.Media)
body, err := io.ReadAll(req.Body)
if err != nil {
writeError(ctx, fmt.Errorf("%w: %w", ErrReadAll, err))
return
}
req.Body = io.NopCloser(bytes.NewBuffer(body))
var rq Req
ptr := &rq
if err := m.Unmarshal(body, ptr); err != nil {
writeError(ctx, fmt.Errorf("%w: %w", ErrUnmarshal, err))
return
}
rs, err := handler.Handle(ctx, ptr)
if err != nil {
writeError(ctx, fmt.Errorf("%w: %w", ErrHandle, err))
return
}
d, err := m.Marshal(rs)
if err != nil {
writeError(ctx, fmt.Errorf("%w: %w", ErrMarshal, err))
return
}
res.Write(d)
}
mux.HandleFunc("POST "+path, h)
}
func writeError(ctx Context, err error) {
http.Error(ctx.Response(), err.Error(), Code(err))
}