Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
aac1b97
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 Jan 28, 2025
76271bb
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 Jan 28, 2025
d6b57b5
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 Jan 29, 2025
7dc7728
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 Jan 30, 2025
b695df6
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 Feb 3, 2025
fadbb0a
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 Feb 3, 2025
67965e6
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 Feb 3, 2025
c39df10
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 Feb 25, 2025
334612f
Merge remote-tracking branch 'refs/remotes/origin/main' into generic_…
ReneWerner87 Mar 26, 2025
b48acd9
fresh with main branch
ReneWerner87 Mar 26, 2025
1070d75
fresh with main branch
ReneWerner87 Mar 26, 2025
1bd1186
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 Apr 24, 2025
900167c
repair readme benchmarks
ReneWerner87 Apr 24, 2025
c3a532c
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 May 18, 2025
b7be67f
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 May 18, 2025
ccea753
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 May 18, 2025
291dc7b
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 May 18, 2025
992cfa9
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
ReneWerner87 May 18, 2025
651f3d2
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 May 18, 2025
a505fbe
Merge remote-tracking branch 'origin/main' into generic_app_ctx_3221
ReneWerner87 May 20, 2025
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
201 changes: 112 additions & 89 deletions app.go

Large diffs are not rendered by default.

39 changes: 19 additions & 20 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ const userContextKey contextKey = 0 // __local_user_context__
// generation tool `go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c`
// https://github.com/vburenin/ifacemaker/blob/975a95966976eeb2d4365a7fb236e274c54da64c/ifacemaker.go#L14-L30
//
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface Ctx --pkg fiber --output ctx_interface_gen.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface CtxGeneric --pkg fiber --output ctx_interface.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
//go:generate go run ctx_interface_gen.go
type DefaultCtx struct {
app *App // Reference to *App
route *Route // Reference to *Route
app *App[*DefaultCtx] // Reference to *App
route *Route[*DefaultCtx] // Reference to *Route
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
bind *Bind // Default bind reference
redirect *Redirect // Default redirect reference
Expand All @@ -73,6 +74,13 @@ type DefaultCtx struct {
matched bool // Non use route matched
}

// Ctx is the default context interface used by Fiber when no custom context is
// provided. It exposes the methods defined by CtxGeneric specialized with
// *DefaultCtx.
type Ctx interface {
CtxGeneric[*DefaultCtx]
}

// SendFile defines configuration options when to transfer file with SendFile.
type SendFile struct {
// FS is the file system to serve the static files from.
Expand Down Expand Up @@ -224,7 +232,7 @@ func (c *DefaultCtx) AcceptsLanguages(offers ...string) string {
}

// App returns the *App reference to the instance of the Fiber application
func (c *DefaultCtx) App() *App {
func (c *DefaultCtx) App() *App[*DefaultCtx] {
return c.app
}

Expand Down Expand Up @@ -1045,11 +1053,6 @@ func (c *DefaultCtx) Next() error {
}

// Continue handler stack
if c.app.newCtxFunc != nil {
_, err := c.app.nextCustom(c)
return err
}

_, err := c.app.next(c)
return err
}
Expand All @@ -1060,11 +1063,7 @@ func (c *DefaultCtx) RestartRouting() error {
var err error

c.indexRoute = -1
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}
_, err = c.app.next(c)
return err
}

Expand Down Expand Up @@ -1466,7 +1465,7 @@ func (c *DefaultCtx) renderExtensions(bind any) {

// Req returns a convenience type whose API is limited to operations
// on the incoming request.
func (c *DefaultCtx) Req() Req {
func (c *DefaultCtx) Req() Req[*DefaultCtx] {
return c.req
}

Expand All @@ -1477,10 +1476,10 @@ func (c *DefaultCtx) Res() Res {
}

// Route returns the matched Route struct.
func (c *DefaultCtx) Route() *Route {
func (c *DefaultCtx) Route() *Route[*DefaultCtx] {
if c.route == nil {
// Fallback for fasthttp error handler
return &Route{
return &Route[*DefaultCtx]{
path: c.pathOriginal,
Path: c.pathOriginal,
Method: c.Method(),
Expand Down Expand Up @@ -1744,7 +1743,7 @@ func (c *DefaultCtx) Stale() bool {

// Status sets the HTTP status for the response.
// This method is chainable.
func (c *DefaultCtx) Status(status int) Ctx {
func (c *DefaultCtx) Status(status int) *DefaultCtx {
c.fasthttp.Response.SetStatusCode(status)
return c
}
Expand Down Expand Up @@ -1789,7 +1788,7 @@ func (c *DefaultCtx) String() string {
}

// Type sets the Content-Type HTTP header to the MIME type specified by the file extension.
func (c *DefaultCtx) Type(extension string, charset ...string) Ctx {
func (c *DefaultCtx) Type(extension string, charset ...string) *DefaultCtx {
if len(charset) > 0 {
c.fasthttp.Response.Header.SetContentType(utils.GetMIME(extension) + "; charset=" + charset[0])
} else {
Expand Down Expand Up @@ -1987,7 +1986,7 @@ func (c *DefaultCtx) setMatched(matched bool) {
c.matched = matched
}

func (c *DefaultCtx) setRoute(route *Route) {
func (c *DefaultCtx) setRoute(route *Route[*DefaultCtx]) {
c.route = route
}

Expand Down
74 changes: 74 additions & 0 deletions ctx_custom_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io

package fiber

import (
"errors"

"github.com/valyala/fasthttp"
)

type CustomCtx[T any] interface {
CtxGeneric[T]

// Reset is a method to reset context fields by given request when to use server handlers.
Reset(fctx *fasthttp.RequestCtx)

// Methods to use with next stack.
getMethodInt() int
getIndexRoute() int
getTreePathHash() int
getDetectionPath() string
getPathOriginal() string
getValues() *[maxParams]string
getMatched() bool
setIndexHandler(handler int)
setIndexRoute(route int)
setMatched(matched bool)
setRoute(route *Route[T])
}

func NewDefaultCtx[TCtx *DefaultCtx](app *App[*DefaultCtx]) TCtx {
// return ctx
ctx := &DefaultCtx{
// Set app reference
app: app,
}
ctx.req = &DefaultReq{ctx: ctx}
ctx.res = &DefaultRes{ctx: ctx}

return ctx
}

func (app *App[TCtx]) newCtx() TCtx {
var c TCtx

// TODO: fix this with generics ?
if app.newCtxFunc != nil {
c = app.newCtxFunc(app)
} else {
c = any(NewDefaultCtx[*DefaultCtx](app)).(TCtx)
}

return c
}

// AcquireCtx retrieves a new Ctx from the pool.
func (app *App[TCtx]) AcquireCtx(fctx *fasthttp.RequestCtx) TCtx {
ctx, ok := app.pool.Get().(TCtx)

if !ok {
panic(errors.New("failed to type-assert to Ctx"))
}
ctx.Reset(fctx)

return ctx
}

// ReleaseCtx releases the ctx back into the pool.
func (app *App[TCtx]) ReleaseCtx(c TCtx) {
c.release()
app.pool.Put(c)
}
Loading
Loading