-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-handlers.go
122 lines (106 loc) · 2.87 KB
/
http-handlers.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package generic_http_handlers
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
type TODO struct {
Id string `json:"id"`
Name string `json:"name"`
}
type Encoder interface {
Encode(v any) error
}
type Decoder interface {
Decode(v any) error
}
type Handler[Req any, Req2 interface{ *Req }, Res any] func(ctx context.Context, req Req2) (Res, error)
type ErrorHandler[E Encoder] func(w http.ResponseWriter, err error, status int, encoderFactory func(w io.Writer) E)
func GET[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res]) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, http.MethodGet)
}
func POST[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res]) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, http.MethodPost)
}
func PUT[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res]) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, http.MethodPut)
}
func PATCH[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res]) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, http.MethodPatch)
}
func DELETE[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res]) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, http.MethodDelete)
}
func HandleJson[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res],
methods ...string) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, methods...)
}
func Handle[
E Encoder,
D Decoder,
Req any,
Req2 interface{ *Req },
Res any,
](
newEncoder func(w io.Writer) E,
newDecoder func(r io.Reader) D,
handleError ErrorHandler[E],
handler Handler[Req, Req2, Res],
methods ...string,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, method := range methods {
if r.Method == method {
break
}
handleError(w, fmt.Errorf("method not allowed"), http.StatusMethodNotAllowed, newEncoder)
return
}
var req = Req2(new(Req))
if err := newDecoder(r.Body).Decode(req); err != nil {
handleError(w, err, http.StatusInternalServerError, newEncoder)
return
}
res, err := handler(r.Context(), req)
if err != nil {
handleError(w, err, http.StatusInternalServerError, newEncoder)
return
}
if err := newEncoder(w).Encode(res); err != nil {
handleError(w, err, http.StatusInternalServerError, newEncoder)
return
}
})
}
func BasicJsonErrorHandler(w http.ResponseWriter, err error, status int, newEncoder func(w io.Writer) *json.Encoder) {
_ = newEncoder(w).Encode(struct {
Error string `json:"error"`
}{err.Error()})
w.WriteHeader(status)
}