1
1
package pulse
2
2
3
3
import (
4
- "bytes"
5
- "github.com/gopulse/pulse/utils"
6
- "github.com/valyala/fasthttp"
4
+ "encoding/json"
5
+ "net/http"
7
6
"strings"
8
7
"time"
9
8
)
10
9
11
10
type handlerFunc func (ctx * Context ) error
12
11
13
12
type Context struct {
14
- RequestCtx * fasthttp.RequestCtx
15
- Params map [string ]string
16
- paramValues []string
17
- handlers []handlerFunc
18
- handlerIdx int
19
- Cookies * fasthttp.Cookie
13
+ ResponseWriter http.ResponseWriter
14
+ Request * http.Request
15
+ Params map [string ]string
16
+ paramValues []string
17
+ handlers []handlerFunc
18
+ handlerIdx int
19
+ Cookies []* http.Cookie
20
+ }
21
+
22
+ func (c * Context ) Write (p []byte ) (n int , err error ) {
23
+ return c .ResponseWriter .Write (p )
20
24
}
21
25
22
26
type Cookie struct {
23
- Name string `json:"name"`
24
- Value string `json:"value"`
25
- Path string `json:"path"`
26
- Domain string `json:"domain"`
27
- MaxAge int `json:"max_age"`
28
- Expires time.Time `json:"expires"`
29
- Secure bool `json:"secure"`
30
- HTTPOnly bool `json:"http_only"`
31
- SameSite string `json:"same_site"`
32
- SessionOnly bool `json:"session_only"`
27
+ Name string
28
+ Value string
29
+ Path string
30
+ Domain string
31
+ MaxAge int
32
+ Expires time.Time
33
+ Secure bool
34
+ HTTPOnly bool
35
+ SameSite http.SameSite
33
36
}
34
37
35
38
// NewContext returns a new Context.
36
- func NewContext (ctx * fasthttp. RequestCtx , params map [ string ] string ) * Context {
39
+ func NewContext (w http. ResponseWriter , req * http. Request ) * Context {
37
40
return & Context {
38
- RequestCtx : ctx ,
39
- Params : params ,
40
- paramValues : make ([]string , 0 , 10 ),
41
- handlers : nil ,
42
- handlerIdx : - 1 ,
41
+ ResponseWriter : w ,
42
+ Request : req ,
43
+ Params : make (map [string ]string ),
44
+ paramValues : make ([]string , 0 , 10 ),
45
+ handlers : nil ,
46
+ handlerIdx : - 1 ,
43
47
}
44
48
}
45
49
46
- // Context returns the fasthttp.RequestCtx
47
- func (c * Context ) Context () * fasthttp.RequestCtx {
48
- return c .RequestCtx
49
- }
50
-
51
50
// WithParams sets the params for the context.
52
51
func (c * Context ) WithParams (params map [string ]string ) * Context {
53
52
c .Params = params
@@ -61,17 +60,14 @@ func (c *Context) Param(key string) string {
61
60
62
61
// Query returns the query value for the given key.
63
62
func (c * Context ) Query (key string ) string {
64
- return string ( c . RequestCtx . QueryArgs ().Peek (key ) )
63
+ return c . Request . URL . Query ().Get (key )
65
64
}
66
65
67
66
// String sets the response body to the given string.
68
67
func (c * Context ) String (value string ) {
69
- if c .RequestCtx .Response .Body () == nil {
70
- c .RequestCtx .Response .SetBodyString (value )
71
- } else {
72
- buf := bytes .NewBuffer (c .RequestCtx .Response .Body ())
73
- buf .WriteString (value )
74
- c .RequestCtx .Response .SetBody (buf .Bytes ())
68
+ _ , err := c .ResponseWriter .Write ([]byte (value ))
69
+ if err != nil {
70
+ return
75
71
}
76
72
}
77
73
@@ -82,7 +78,7 @@ func (c *Context) SetData(key string, value interface{}) {
82
78
83
79
// GetData returns the http header value for the given key.
84
80
func (c * Context ) GetData (key string ) string {
85
- return string (c .RequestCtx . Response . Header .Peek (key ))
81
+ return string (c .Request . Header .Get (key ))
86
82
}
87
83
88
84
// Next calls the next handler in the chain.
@@ -106,42 +102,26 @@ func (c *Context) Abort() {
106
102
107
103
// SetCookie sets a cookie with the given name, value, and options.
108
104
func (c * Context ) SetCookie (cookie * Cookie ) {
109
- acCookie := fasthttp .AcquireCookie ()
110
-
111
- acCookie .SetKey (cookie .Name )
112
- acCookie .SetValue (cookie .Value )
113
- acCookie .SetPath (cookie .Path )
114
- acCookie .SetDomain (cookie .Domain )
115
- acCookie .SetSecure (cookie .Secure )
116
- acCookie .SetHTTPOnly (cookie .HTTPOnly )
117
- acCookie .SetSecure (cookie .Secure )
118
- if ! cookie .SessionOnly {
119
- acCookie .SetMaxAge (cookie .MaxAge )
120
- acCookie .SetExpire (cookie .Expires )
121
- }
122
-
123
- switch strings .ToLower (cookie .SameSite ) {
124
- case string (rune (fasthttp .CookieSameSiteStrictMode )):
125
- acCookie .SetSameSite (fasthttp .CookieSameSiteStrictMode )
126
- case string (rune (fasthttp .CookieSameSiteNoneMode )):
127
- acCookie .SetSameSite (fasthttp .CookieSameSiteNoneMode )
128
- case string (rune (fasthttp .CookieSameSiteDisabled )):
129
- acCookie .SetSameSite (fasthttp .CookieSameSiteDisabled )
130
- default :
131
- acCookie .SetSameSite (fasthttp .CookieSameSiteDefaultMode )
132
- }
133
-
134
- c .RequestCtx .Response .Header .SetCookie (acCookie )
135
- fasthttp .ReleaseCookie (acCookie )
105
+ http .SetCookie (c .ResponseWriter , & http.Cookie {
106
+ Name : cookie .Name ,
107
+ Value : cookie .Value ,
108
+ Path : cookie .Path ,
109
+ Domain : cookie .Domain ,
110
+ Expires : cookie .Expires ,
111
+ MaxAge : cookie .MaxAge ,
112
+ Secure : cookie .Secure ,
113
+ HttpOnly : cookie .HTTPOnly ,
114
+ SameSite : cookie .SameSite ,
115
+ })
136
116
}
137
117
138
118
// GetCookie returns the value of the cookie with the given name.
139
119
func (c * Context ) GetCookie (name string ) string {
140
- cookie := c .RequestCtx . Request . Header .Cookie (name )
141
- if cookie = = nil {
120
+ cookie , err := c .Request .Cookie (name )
121
+ if err ! = nil {
142
122
return ""
143
123
}
144
- return string ( cookie )
124
+ return cookie . Value
145
125
}
146
126
147
127
// ClearCookie deletes the cookie with the given name.
@@ -158,27 +138,27 @@ func (c *Context) ClearCookie(name string) {
158
138
159
139
// SetResponseHeader sets the http header value to the given key.
160
140
func (c * Context ) SetResponseHeader (key , value string ) {
161
- c .RequestCtx . Response . Header .Set (key , value )
141
+ c .ResponseWriter . Header () .Set (key , value )
162
142
}
163
143
164
144
// GetResponseHeader returns the http header value for the given key.
165
145
func (c * Context ) GetResponseHeader (key string ) string {
166
- return string ( c . RequestCtx . Request . Header . Peek (key ) )
146
+ return c . ResponseWriter . Header (). Get (key )
167
147
}
168
148
169
149
// SetRequestHeader SetResponseHeader sets the http header value to the given key.
170
150
func (c * Context ) SetRequestHeader (key , value string ) {
171
- c .RequestCtx . Request .Header .Set (key , value )
151
+ c .Request .Header .Set (key , value )
172
152
}
173
153
174
154
// GetRequestHeader GetResponseHeader returns the http header value for the given key.
175
155
func (c * Context ) GetRequestHeader (key string ) string {
176
- return string ( c . RequestCtx . Request .Header .Peek (key ) )
156
+ return c . Request .Header .Get (key )
177
157
}
178
158
179
159
// SetContentType sets the Content-Type header in the response to the given value.
180
160
func (c * Context ) SetContentType (value string ) {
181
- c .RequestCtx . Response . Header . SetContentType ( value )
161
+ c .ResponseWriter . Header (). Set ( "Content-Type" , value )
182
162
}
183
163
184
164
// Accepts checks if the specified content types are acceptable.
@@ -204,22 +184,24 @@ func (c *Context) Accepts(types ...string) string {
204
184
205
185
// Status sets the response status code.
206
186
func (c * Context ) Status (code int ) {
207
- c .RequestCtx . Response . SetStatusCode (code )
187
+ c .ResponseWriter . WriteHeader (code )
208
188
}
209
189
210
190
// JSON sets the response body to the given JSON representation.
211
191
func (c * Context ) JSON (code int , obj interface {}) ([]byte , error ) {
212
- c .RequestCtx . Response . Header . SetContentType ( "application/json" )
213
- c .RequestCtx . Response . SetStatusCode (code )
214
- jsonBody , err := utils . ToJSON (obj )
192
+ c .ResponseWriter . Header (). Set ( "Content-Type" , "application/json" )
193
+ c .Status (code )
194
+ jsonBody , err := json . Marshal (obj )
215
195
if err != nil {
216
196
return nil , err
217
197
}
218
- c .RequestCtx .Response .SetBodyString (jsonBody )
198
+ if _ , err := c .ResponseWriter .Write (jsonBody ); err != nil {
199
+ return nil , err
200
+ }
219
201
220
- return [] byte ( jsonBody ) , nil
202
+ return jsonBody , nil
221
203
}
222
204
223
205
func (c * Context ) BodyParser (v interface {}) error {
224
- return utils . FromJSON (c .RequestCtx . Request .Body (), v )
206
+ return json . NewDecoder (c .Request .Body ). Decode ( v )
225
207
}
0 commit comments