Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal v3] 🔥feat!: add Req and Res objects to mirror Express #2764

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func DefaultErrorHandler(c Ctx, err error) error {
if errors.As(err, &e) {
code = e.Code
}
c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
c.Res().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
return c.Status(code).SendString(err.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func Test_App_Static_Custom_CacheControl(t *testing.T) {

app.Static("/", "./.github", Static{ModifyResponse: func(c Ctx) error {
if strings.Contains(c.GetRespHeader("Content-Type"), "text/html") {
c.Response().Header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
c.Res().Set("Cache-Control", "no-cache, no-store, must-revalidate")
}
return nil
}})
Expand Down Expand Up @@ -956,7 +956,7 @@ func Test_App_Static_Group(t *testing.T) {
app := New()

grp := app.Group("/v1", func(c Ctx) error {
c.Set("Test-Header", "123")
c.Res().Set("Test-Header", "123")
return c.Next()
})

Expand Down Expand Up @@ -1147,7 +1147,7 @@ func Test_App_Mixed_Routes_WithSameLen(t *testing.T) {

// middleware
app.Use(func(c Ctx) error {
c.Set("TestHeader", "TestValue")
c.Res().Set("TestHeader", "TestValue")
return c.Next()
})
// routes with the same length
Expand Down
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (b *Bind) Header(out any) error {

// RespHeader binds the response header strings into the struct, map[string]string and map[string][]string.
func (b *Bind) RespHeader(out any) error {
if err := b.returnErr(binder.RespHeaderBinder.Bind(b.ctx.Response(), out)); err != nil {
if err := b.returnErr(binder.RespHeaderBinder.Bind(b.ctx.Res().FastHTTP(), out)); err != nil {
return err
}

Expand Down
54 changes: 27 additions & 27 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,21 +540,21 @@ func Test_Bind_RespHeader(t *testing.T) {
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Res().Append("id", "1")
c.Res().Append("Name", "John Doe")
c.Res().Append("Hobby", "golang,fiber")
q := new(Header)
require.Nil(t, c.Bind().RespHeader(q))
require.Equal(t, 2, len(q.Hobby))

c.Response().Header.Del("hobby")
c.Response().Header.Add("Hobby", "golang,fiber,go")
c.Res().FastHTTP().Header.Del("hobby")
c.Res().Append("Hobby", "golang,fiber,go")
q = new(Header)
require.Nil(t, c.Bind().RespHeader(q))
require.Equal(t, 3, len(q.Hobby))

empty := new(Header)
c.Response().Header.Del("hobby")
c.Res().FastHTTP().Header.Del("hobby")
require.Nil(t, c.Bind().Query(empty))
require.Equal(t, 0, len(empty.Hobby))

Expand All @@ -569,13 +569,13 @@ func Test_Bind_RespHeader(t *testing.T) {
No []int64
}

c.Response().Header.Add("id", "2")
c.Response().Header.Add("Name", "Jane Doe")
c.Response().Header.Del("hobby")
c.Response().Header.Add("Hobby", "go,fiber")
c.Response().Header.Add("favouriteDrinks", "milo,coke,pepsi")
c.Response().Header.Add("alloc", "")
c.Response().Header.Add("no", "1")
c.Res().Append("id", "2")
c.Res().Append("Name", "Jane Doe")
c.Res().FastHTTP().Header.Del("hobby")
c.Res().Append("Hobby", "go,fiber")
c.Res().Append("favouriteDrinks", "milo,coke,pepsi")
c.Res().Append("alloc", "")
c.Res().Append("no", "1")

h2 := new(Header2)
h2.Bool = true
Expand All @@ -594,7 +594,7 @@ func Test_Bind_RespHeader(t *testing.T) {
Name string `respHeader:"name,required"`
}
rh := new(RequiredHeader)
c.Response().Header.Del("name")
c.Res().FastHTTP().Header.Del("name")
require.Equal(t, "name is empty", c.Bind().RespHeader(rh).Error())
}

Expand All @@ -608,21 +608,21 @@ func Test_Bind_RespHeader_Map(t *testing.T) {
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Res().Append("id", "1")
c.Res().Append("Name", "John Doe")
c.Res().Append("Hobby", "golang,fiber")
q := make(map[string][]string, 0)
require.Nil(t, c.Bind().RespHeader(&q))
require.Equal(t, 2, len(q["Hobby"]))

c.Response().Header.Del("hobby")
c.Response().Header.Add("Hobby", "golang,fiber,go")
c.Res().FastHTTP().Header.Del("hobby")
c.Res().Append("Hobby", "golang,fiber,go")
q = make(map[string][]string, 0)
require.Nil(t, c.Bind().RespHeader(&q))
require.Equal(t, 3, len(q["Hobby"]))

empty := make(map[string][]string, 0)
c.Response().Header.Del("hobby")
c.Res().FastHTTP().Header.Del("hobby")
require.Nil(t, c.Bind().Query(&empty))
require.Equal(t, 0, len(empty["Hobby"]))
}
Expand Down Expand Up @@ -790,9 +790,9 @@ func Benchmark_Bind_RespHeader(b *testing.B) {
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Res().Append("id", "1")
c.Res().Append("Name", "John Doe")
c.Res().Append("Hobby", "golang,fiber")

q := new(ReqHeader)
b.ReportAllocs()
Expand All @@ -812,9 +812,9 @@ func Benchmark_Bind_RespHeader_Map(b *testing.B) {
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")

c.Response().Header.Add("id", "1")
c.Response().Header.Add("Name", "John Doe")
c.Response().Header.Add("Hobby", "golang,fiber")
c.Res().Append("id", "1")
c.Res().Append("Name", "John Doe")
c.Res().Append("Hobby", "golang,fiber")

q := make(map[string][]string)
b.ReportAllocs()
Expand Down Expand Up @@ -1541,7 +1541,7 @@ func Test_Bind_Must(t *testing.T) {
rq := new(RequiredQuery)
c.Request().URI().SetQueryString("")
err := c.Bind().Must().Query(rq)
require.Equal(t, StatusBadRequest, c.Response().StatusCode())
require.Equal(t, StatusBadRequest, c.Res().FastHTTP().StatusCode())
require.Equal(t, "Bad request: name is empty", err.Error())
}

Expand Down
16 changes: 5 additions & 11 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type DefaultCtx struct {
pathOriginal string // Original HTTP path
values [maxParams]string // Route parameter values
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
res *Res // Response object
matched bool // Non use route matched
viewBindMap sync.Map // Default view map to bind template engine
bind *Bind // Default bind reference
Expand Down Expand Up @@ -143,7 +144,7 @@ func (c *DefaultCtx) Append(field string, values ...string) {
}
}
if originalH != h {
c.Set(field, h)
c.Res().Set(field, h)
}
}

Expand Down Expand Up @@ -363,11 +364,9 @@ func (c *DefaultCtx) Request() *fasthttp.Request {
return &c.fasthttp.Request
}

// Response return the *fasthttp.Response object
// This allows you to use all fasthttp response methods
// https://godoc.org/github.com/valyala/fasthttp#Response
func (c *DefaultCtx) Response() *fasthttp.Response {
return &c.fasthttp.Response
// Res returns the response object for the current context
func (c *DefaultCtx) Res() *Res {
return c.res
}

// Format performs content-negotiation on the Accept HTTP header.
Expand Down Expand Up @@ -1393,11 +1392,6 @@ func (c *DefaultCtx) SendStream(stream io.Reader, size ...int) error {
return nil
}

// Set sets the response's HTTP header field to the specified key, value.
func (c *DefaultCtx) Set(key, val string) {
c.fasthttp.Response.Header.Set(key, val)
}

func (c *DefaultCtx) setCanonical(key, val string) {
c.fasthttp.Response.Header.SetCanonical(utils.UnsafeBytes(key), utils.UnsafeBytes(val))
}
Expand Down
18 changes: 12 additions & 6 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ type Ctx interface {
// https://godoc.org/github.com/valyala/fasthttp#Request
Request() *fasthttp.Request

// Response return the *fasthttp.Response object
// Res return the *fasthttp.Res object
// This allows you to use all fasthttp response methods
// https://godoc.org/github.com/valyala/fasthttp#Response
Response() *fasthttp.Response
// https://godoc.org/github.com/valyala/fasthttp#Res
Res() *Res

// Format performs content-negotiation on the Accept HTTP header.
// It uses Accepts to select a proper format.
Expand Down Expand Up @@ -335,9 +335,6 @@ type Ctx interface {
// SendStream sets response body stream and optional body size.
SendStream(stream io.Reader, size ...int) error

// Set sets the response's HTTP header field to the specified key, value.
Set(key, val string)

// Subdomains returns a string slice of subdomains in the domain name of the request.
// The subdomain offset, which defaults to 2, is used for determining the beginning of the subdomain segments.
Subdomains(offset ...int) []string
Expand Down Expand Up @@ -482,6 +479,10 @@ func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) {

// Attach *fasthttp.RequestCtx to ctx
c.fasthttp = fctx
c.res = &Res{
app: c.app,
fasthttp: &fctx.Response,
}

// reset base uri
c.baseURI = ""
Expand All @@ -499,6 +500,7 @@ func (c *DefaultCtx) release() {
c.route = nil
c.fasthttp = nil
c.bind = nil
c.res = nil
c.redirectionMessages = c.redirectionMessages[:0]
c.viewBindMap = sync.Map{}
if c.redirect != nil {
Expand All @@ -514,6 +516,10 @@ func (c *DefaultCtx) setReq(fctx *fasthttp.RequestCtx) {

// Attach *fasthttp.RequestCtx to ctx
c.fasthttp = fctx
c.res = &Res{
app: c.app,
fasthttp: &fctx.Response,
}

// Set method
c.method = c.app.getString(fctx.Request.Header.Method())
Expand Down
Loading
Loading