-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
45 lines (37 loc) · 912 Bytes
/
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
package http
import (
"github.com/hopeio/cherry/utils/types"
"net/http"
)
type Handlers []http.Handler
func (hs Handlers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, handler := range hs {
handler.ServeHTTP(w, r)
}
}
type HandlerFuncs []http.HandlerFunc
func (hs HandlerFuncs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, handler := range hs {
handler(w, r)
}
}
func (hs HandlerFuncs) HandlerFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
for _, handler := range hs {
handler(w, r)
}
}
}
func (hs *HandlerFuncs) Add(handler http.HandlerFunc) {
*hs = append(*hs, handler)
}
// TODO
func commonHandler[REQ, RES any](method types.GrpcServiceMethod[*REQ, *RES]) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := new(REQ)
err := Bind(r, req)
if err != nil {
return
}
})
}