Skip to content

Commit 41529bb

Browse files
authored
ISSUE-16: Enhancements in Pulse logic (#17)
* use net/http instead of fasthttp, cleanup code * delete fasthttp * use http package * use http package, cleanup code * use http methods constants * cleanup route file
1 parent 29aa224 commit 41529bb

File tree

7 files changed

+121
-192
lines changed

7 files changed

+121
-192
lines changed

app.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package pulse
33
import (
44
"fmt"
55
"github.com/common-nighthawk/go-figure"
6-
"github.com/valyala/fasthttp"
76
"net"
7+
"net/http"
88
)
99

1010
type (
1111
Pulse struct {
1212
config *Config
13-
server *fasthttp.Server
13+
server *http.Server
1414
Router *Router
1515
}
1616

@@ -34,7 +34,7 @@ const (
3434
func New(config ...Config) *Pulse {
3535
app := &Pulse{
3636
config: &Config{},
37-
server: &fasthttp.Server{},
37+
server: &http.Server{},
3838
}
3939

4040
if len(config) > 0 {
@@ -73,7 +73,7 @@ func (f *Pulse) Run(address string) {
7373
}
7474

7575
func (f *Pulse) Stop() {
76-
err := f.server.Shutdown()
76+
err := f.server.Shutdown(nil)
7777
if err != nil {
7878
return
7979
}

context.go

+62-80
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
11
package pulse
22

33
import (
4-
"bytes"
5-
"github.com/gopulse/pulse/utils"
6-
"github.com/valyala/fasthttp"
4+
"encoding/json"
5+
"net/http"
76
"strings"
87
"time"
98
)
109

1110
type handlerFunc func(ctx *Context) error
1211

1312
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)
2024
}
2125

2226
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
3336
}
3437

3538
// 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 {
3740
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,
4347
}
4448
}
4549

46-
// Context returns the fasthttp.RequestCtx
47-
func (c *Context) Context() *fasthttp.RequestCtx {
48-
return c.RequestCtx
49-
}
50-
5150
// WithParams sets the params for the context.
5251
func (c *Context) WithParams(params map[string]string) *Context {
5352
c.Params = params
@@ -61,17 +60,14 @@ func (c *Context) Param(key string) string {
6160

6261
// Query returns the query value for the given key.
6362
func (c *Context) Query(key string) string {
64-
return string(c.RequestCtx.QueryArgs().Peek(key))
63+
return c.Request.URL.Query().Get(key)
6564
}
6665

6766
// String sets the response body to the given string.
6867
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
7571
}
7672
}
7773

@@ -82,7 +78,7 @@ func (c *Context) SetData(key string, value interface{}) {
8278

8379
// GetData returns the http header value for the given key.
8480
func (c *Context) GetData(key string) string {
85-
return string(c.RequestCtx.Response.Header.Peek(key))
81+
return string(c.Request.Header.Get(key))
8682
}
8783

8884
// Next calls the next handler in the chain.
@@ -106,42 +102,26 @@ func (c *Context) Abort() {
106102

107103
// SetCookie sets a cookie with the given name, value, and options.
108104
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+
})
136116
}
137117

138118
// GetCookie returns the value of the cookie with the given name.
139119
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 {
142122
return ""
143123
}
144-
return string(cookie)
124+
return cookie.Value
145125
}
146126

147127
// ClearCookie deletes the cookie with the given name.
@@ -158,27 +138,27 @@ func (c *Context) ClearCookie(name string) {
158138

159139
// SetResponseHeader sets the http header value to the given key.
160140
func (c *Context) SetResponseHeader(key, value string) {
161-
c.RequestCtx.Response.Header.Set(key, value)
141+
c.ResponseWriter.Header().Set(key, value)
162142
}
163143

164144
// GetResponseHeader returns the http header value for the given key.
165145
func (c *Context) GetResponseHeader(key string) string {
166-
return string(c.RequestCtx.Request.Header.Peek(key))
146+
return c.ResponseWriter.Header().Get(key)
167147
}
168148

169149
// SetRequestHeader SetResponseHeader sets the http header value to the given key.
170150
func (c *Context) SetRequestHeader(key, value string) {
171-
c.RequestCtx.Request.Header.Set(key, value)
151+
c.Request.Header.Set(key, value)
172152
}
173153

174154
// GetRequestHeader GetResponseHeader returns the http header value for the given key.
175155
func (c *Context) GetRequestHeader(key string) string {
176-
return string(c.RequestCtx.Request.Header.Peek(key))
156+
return c.Request.Header.Get(key)
177157
}
178158

179159
// SetContentType sets the Content-Type header in the response to the given value.
180160
func (c *Context) SetContentType(value string) {
181-
c.RequestCtx.Response.Header.SetContentType(value)
161+
c.ResponseWriter.Header().Set("Content-Type", value)
182162
}
183163

184164
// Accepts checks if the specified content types are acceptable.
@@ -204,22 +184,24 @@ func (c *Context) Accepts(types ...string) string {
204184

205185
// Status sets the response status code.
206186
func (c *Context) Status(code int) {
207-
c.RequestCtx.Response.SetStatusCode(code)
187+
c.ResponseWriter.WriteHeader(code)
208188
}
209189

210190
// JSON sets the response body to the given JSON representation.
211191
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)
215195
if err != nil {
216196
return nil, err
217197
}
218-
c.RequestCtx.Response.SetBodyString(jsonBody)
198+
if _, err := c.ResponseWriter.Write(jsonBody); err != nil {
199+
return nil, err
200+
}
219201

220-
return []byte(jsonBody), nil
202+
return jsonBody, nil
221203
}
222204

223205
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)
225207
}

context_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pulse
22

33
import (
4+
"net/http"
45
"testing"
56
"time"
67
)
@@ -56,16 +57,15 @@ func TestContext_SetCookie(t *testing.T) {
5657

5758
router.Get("/", func(ctx *Context) error {
5859
cookie := Cookie{
59-
Name: "Test Cookie 1",
60-
Value: "Test Cookie 1",
61-
Path: "/",
62-
Domain: "localhost",
63-
MaxAge: 0,
64-
Expires: time.Now().Add(24 * time.Hour),
65-
Secure: false,
66-
HTTPOnly: false,
67-
SameSite: "Lax",
68-
SessionOnly: false,
60+
Name: "Test Cookie 1",
61+
Value: "Test Cookie 1",
62+
Path: "/",
63+
Domain: "localhost",
64+
MaxAge: 0,
65+
Expires: time.Now().Add(24 * time.Hour),
66+
Secure: false,
67+
HTTPOnly: false,
68+
SameSite: http.SameSiteLaxMode,
6969
}
7070
ctx.SetCookie(&cookie)
7171
return nil

go.mod

-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ module github.com/gopulse/pulse
22

33
go 1.19
44

5-
require github.com/valyala/fasthttp v1.45.0
65

76
require (
8-
github.com/andybalholm/brotli v1.0.5 // indirect
97
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
10-
github.com/klauspost/compress v1.16.3 // indirect
11-
github.com/valyala/bytebufferpool v1.0.0 // indirect
128
)

0 commit comments

Comments
 (0)