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

feat: bump fiber to v3 #95

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import "github.com/goravel/fiber"
import (
fiberfacades "github.com/goravel/fiber/facades"
"github.com/gofiber/template/html/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
)

"default": "fiber",
Expand Down
41 changes: 30 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,58 @@

import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
"github.com/goravel/framework/contracts/http"
"github.com/valyala/fasthttp"
)

func Background() http.Context {
app := fiber.New()
httpCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})

return NewContext(httpCtx)
return &Context{instance: ctx}
}

var contextPool = sync.Pool{New: func() any {
return &Context{}
}}

type Context struct {
instance *fiber.Ctx
instance fiber.Ctx
request http.ContextRequest
}

func NewContext(ctx *fiber.Ctx) http.Context {
return &Context{instance: ctx}
response http.ContextResponse
}

func (c *Context) Request() http.ContextRequest {
if c.request == nil {
c.request = NewContextRequest(c, LogFacade, ValidationFacade)
request := contextRequestPool.Get().(*ContextRequest)
httpBody, err := getHttpBody(c)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))

Check failure on line 37 in context.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:

Check failure on line 37 in context.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:
}
request.ctx = c
request.instance = c.instance
request.httpBody = httpBody
c.request = request
}

return c.request
}

func (c *Context) Response() http.ContextResponse {
return NewContextResponse(c.instance, &ResponseOrigin{Ctx: c.instance})
if c.response == nil {
response := contextResponsePool.Get().(*ContextResponse)
response.instance = c.instance
response.origin = &ResponseOrigin{Ctx: c.instance}
c.response = response
}

return c.response
}

func (c *Context) WithValue(key any, value any) {
Expand Down Expand Up @@ -66,6 +85,6 @@
return c.instance.UserContext().Value(key)
}

func (c *Context) Instance() *fiber.Ctx {
func (c *Context) Instance() fiber.Ctx {
return c.instance
}
34 changes: 16 additions & 18 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"regexp"
"strconv"
"strings"
"sync"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/utils/v2"
"github.com/gookit/validate"
contractsfilesystem "github.com/goravel/framework/contracts/filesystem"
contractshttp "github.com/goravel/framework/contracts/http"
Expand All @@ -24,23 +25,21 @@ import (
"github.com/valyala/fasthttp/fasthttpadaptor"
)

var contextRequestPool = sync.Pool{New: func() any {
return &ContextRequest{
log: LogFacade,
validation: ValidationFacade,
}
}}

type ContextRequest struct {
ctx *Context
instance *fiber.Ctx
instance fiber.Ctx
httpBody map[string]any
log log.Log
validation contractsvalidate.Validation
}

func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.Validation) contractshttp.ContextRequest {
httpBody, err := getHttpBody(ctx)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))
}

return &ContextRequest{ctx: ctx, instance: ctx.instance, httpBody: httpBody, log: log, validation: validation}
}

func (r *ContextRequest) AbortWithStatus(code int) {
if err := r.instance.SendStatus(code); err != nil {
panic(err)
Expand All @@ -56,9 +55,7 @@ func (r *ContextRequest) AbortWithStatusJson(code int, jsonObj any) {
func (r *ContextRequest) All() map[string]any {
data := make(map[string]any)

for k, v := range r.instance.AllParams() {
data[k] = v
}
_ = r.instance.Bind().URI(data)
for k, v := range r.instance.Queries() {
data[k] = v
}
Expand All @@ -70,11 +67,11 @@ func (r *ContextRequest) All() map[string]any {
}

func (r *ContextRequest) Bind(obj any) error {
return r.instance.BodyParser(obj)
return r.instance.Bind().Body(obj)
}

func (r *ContextRequest) BindQuery(obj any) error {
return r.instance.QueryParser(obj)
return r.instance.Bind().Query(obj)
}

func (r *ContextRequest) Cookie(key string, defaultValue ...string) string {
Expand Down Expand Up @@ -403,7 +400,8 @@ func (r *ContextRequest) Validate(rules map[string]string, options ...contractsv
}
}

for key, param := range r.instance.AllParams() {
for _, key := range r.instance.Route().Params {
param := r.instance.Params(key)
if _, exist := dataFace.Get(key); !exist {
if _, err := dataFace.Set(key, param); err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestContextRequestSuite(t *testing.T) {

func (s *ContextRequestSuite) SetupTest() {
s.mockConfig = &mocksconfig.Config{}
s.mockConfig.EXPECT().GetBool("http.drivers.fiber.prefork", false).Return(false).Once()
s.mockConfig.EXPECT().GetInt("http.drivers.fiber.body_limit", 4096).Return(4096).Once()
s.mockConfig.EXPECT().GetInt("http.drivers.fiber.header_limit", 4096).Return(4096).Once()
ValidationFacade = validation.NewValidation()
Expand Down
20 changes: 10 additions & 10 deletions context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
"net/http"
"sync"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/utils/v2"
contractshttp "github.com/goravel/framework/contracts/http"
"github.com/valyala/fasthttp"
)

var contextResponsePool = sync.Pool{New: func() any {
return &ContextResponse{}
}}

type ContextResponse struct {
instance *fiber.Ctx
instance fiber.Ctx
origin contractshttp.ResponseOrigin
}

func NewContextResponse(instance *fiber.Ctx, origin contractshttp.ResponseOrigin) *ContextResponse {
return &ContextResponse{instance, origin}
}

func (r *ContextResponse) Cookie(cookie contractshttp.Cookie) contractshttp.ContextResponse {
r.instance.Cookie(&fiber.Cookie{
Name: cookie.Name,
Expand Down Expand Up @@ -190,11 +190,11 @@ func (w *netHTTPResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
}

type Status struct {
instance *fiber.Ctx
instance fiber.Ctx
status int
}

func NewStatus(instance *fiber.Ctx, code int) contractshttp.ResponseStatus {
func NewStatus(instance fiber.Ctx, code int) contractshttp.ResponseStatus {
return &Status{instance, code}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ func ResponseMiddleware() contractshttp.Middleware {
}

type ResponseOrigin struct {
*fiber.Ctx
fiber.Ctx
}

func (w *ResponseOrigin) Body() *bytes.Buffer {
Expand Down
6 changes: 1 addition & 5 deletions context_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"testing"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
contractshttp "github.com/goravel/framework/contracts/http"
configmocks "github.com/goravel/framework/mocks/config"
"github.com/goravel/framework/support/json"
Expand All @@ -24,7 +24,6 @@ func TestResponse(t *testing.T) {
)
beforeEach := func() {
mockConfig = &configmocks.Config{}
mockConfig.On("GetBool", "http.drivers.fiber.prefork", false).Return(false).Once()
mockConfig.On("GetInt", "http.drivers.fiber.body_limit", 4096).Return(4096).Once()
mockConfig.On("GetInt", "http.drivers.fiber.header_limit", 4096).Return(4096).Once()
ConfigFacade = mockConfig
Expand Down Expand Up @@ -446,7 +445,6 @@ func TestResponse_Success(t *testing.T) {
)
beforeEach := func() {
mockConfig = &configmocks.Config{}
mockConfig.On("GetBool", "http.drivers.fiber.prefork", false).Return(false).Once()
mockConfig.On("GetInt", "http.drivers.fiber.body_limit", 4096).Return(4096).Once()
mockConfig.On("GetInt", "http.drivers.fiber.header_limit", 4096).Return(4096).Once()
ConfigFacade = mockConfig
Expand Down Expand Up @@ -575,7 +573,6 @@ func TestResponse_Status(t *testing.T) {
)
beforeEach := func() {
mockConfig = &configmocks.Config{}
mockConfig.On("GetBool", "http.drivers.fiber.prefork", false).Return(false).Once()
mockConfig.On("GetInt", "http.drivers.fiber.body_limit", 4096).Return(4096).Once()
mockConfig.On("GetInt", "http.drivers.fiber.header_limit", 4096).Return(4096).Once()
ConfigFacade = mockConfig
Expand Down Expand Up @@ -697,7 +694,6 @@ func TestResponse_Status(t *testing.T) {

func TestResponse_Stream(t *testing.T) {
mockConfig := &configmocks.Config{}
mockConfig.EXPECT().GetBool("http.drivers.fiber.prefork", false).Return(false).Once()
mockConfig.EXPECT().GetInt("http.drivers.fiber.body_limit", 4096).Return(4096).Once()
mockConfig.EXPECT().GetInt("http.drivers.fiber.header_limit", 4096).Return(4096).Once()

Expand Down
1 change: 0 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ func TestContext(t *testing.T) {
assert.Equal(t, "one", ctx.Value(1))
assert.Equal(t, "two point two", ctx.Value(2.2))
}

Loading
Loading