-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzhttp.go
128 lines (106 loc) · 2.66 KB
/
zhttp.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
123
124
125
126
127
128
package zhttp
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
)
// ContextKeyType for set context.Value
type ContextKeyType int
const (
// RequestContextKey for http.Request
RequestContextKey ContextKeyType = iota
)
// HandlerFunc is type signtature for handler function
type HandlerFunc[T1, T2 any] func(context.Context, *T1) (*T2, error)
// Handler convert zhttp.HandlerFunc to http.HandlerFunc
func Handler[T1, T2 any](f func(context.Context, *T1) (*T2, error)) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var in T1
if input, ok := any(&in).(Boundable); ok {
err := input.Bind(r)
if err != nil {
handlerErr(w, err)
return
}
} else {
err := bind(r, &in)
if err != nil {
handlerErr(w, err)
return
}
}
ctx := context.WithValue(r.Context(), RequestContextKey, r)
out, err := f(ctx, &in)
if err != nil {
handlerErr(w, err)
return
}
if m, ok := any(out).(Marshalable); ok {
data, err := m.Marshal(ctx)
if err != nil {
handlerErr(w, err)
}
fmt.Fprintf(w, "%s", data)
return
}
if err = json.NewEncoder(w).Encode(out); err != nil {
handlerErr(w, err)
}
})
}
func handlerErr(w http.ResponseWriter, err error) {
if e, ok := err.(Error); ok {
statusCode, body := e.HTTPError()
w.WriteHeader(statusCode)
fmt.Fprintf(w, "%s", body)
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err.Error())
}
func bind(r *http.Request, in any) error {
v := reflect.ValueOf(in).Elem()
t := reflect.TypeOf(in).Elem()
for i := 0; i < v.NumField(); i++ {
fv := v.Field(i)
ft := t.Field(i)
var param string
if urlTagValue, ok := ft.Tag.Lookup("url"); !ok {
param = r.URL.Query().Get(ft.Name)
} else {
param = r.URL.Query().Get(urlTagValue)
}
if len(param) == 0 {
continue
}
switch fv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
paramValue, _ := strconv.ParseInt(param, 10, 64)
fv.SetInt(paramValue)
case reflect.Bool:
paramValue, _ := strconv.ParseBool(param)
fv.SetBool(paramValue)
case reflect.Float32, reflect.Float64:
paramValue, _ := strconv.ParseFloat(param, 64)
fv.SetFloat(paramValue)
case reflect.String:
fv.SetString(param)
}
}
if r.Method == http.MethodGet {
return nil
}
return json.NewDecoder(r.Body).Decode(in)
}
// RequestFromContext get the *http.Request from wrapped context
// this func use for some general propose, eg: upload file, handle form...
func RequestFromContext(ctx context.Context) *http.Request {
r, ok := ctx.Value(RequestContextKey).(*http.Request)
if ok {
return r
}
return nil
}