-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
129 lines (101 loc) · 2.38 KB
/
request.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
129
package gohf
import (
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"time"
)
type Request struct {
req *http.Request
body RequestBody
timestamp time.Time
ctx context.Context
rootContext context.Context
}
func newRequest(req *http.Request) *Request {
ctx := req.Context()
return &Request{
req: req,
body: newRequestBody(req.Body),
timestamp: time.Now(),
ctx: ctx,
rootContext: ctx,
}
}
func (req *Request) GetTimestamp() time.Time {
return req.timestamp
}
func (req *Request) Method() string {
return req.req.Method
}
func (req *Request) RemoteAddr() string {
return req.req.RemoteAddr
}
func (req *Request) Host() string {
return req.req.Host
}
func (req *Request) RequestURI() string {
return req.req.RequestURI
}
func (req *Request) RootContext() context.Context {
return req.rootContext
}
func (req *Request) Context() context.Context {
return req.ctx
}
func (req *Request) SetContext(ctx context.Context) {
req.ctx = ctx
}
func (req *Request) GetHeader(key string) string {
return req.req.Header.Get(key)
}
func (req *Request) SetHeader(key string, value string) {
req.req.Header.Set(key, value)
}
func (req *Request) PathValue(name string) string {
return req.req.PathValue(name)
}
func (req *Request) GetQuery(key string) string {
return req.req.URL.Query().Get(key)
}
func (req *Request) GetBody() RequestBody {
return req.body
}
func (req *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
return req.req.FormFile(key)
}
func (req *Request) FormValue(key string) string {
return req.req.FormValue(key)
}
func (req *Request) Cookies() []*http.Cookie {
return req.req.Cookies()
}
func (req *Request) Cookie(name string) (*http.Cookie, error) {
return req.req.Cookie(name)
}
func (req *Request) AddCookie(c *http.Cookie) {
req.req.AddCookie(c)
}
func (req *Request) SetHttpRequest(r *http.Request) {
req.req = r
}
func (req *Request) GetHttpRequest() *http.Request {
return req.req
}
type RequestBody struct {
body io.ReadCloser
}
func newRequestBody(body io.ReadCloser) RequestBody {
return RequestBody{body: body}
}
func (body RequestBody) Close() error {
return body.body.Close()
}
func (body RequestBody) Read(p []byte) (n int, err error) {
return body.body.Read(p)
}
func (body RequestBody) JsonDecode(v any) error {
return json.NewDecoder(body).Decode(v)
}