Skip to content

Commit 469a855

Browse files
Add docs from gofiber/fiber@16f9056
1 parent fabefc1 commit 469a855

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

docs/core/api/ctx.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,20 @@ app.Get("/hello", func(c fiber.Ctx) error {
354354

355355
## Context
356356

357-
Returns [\*fasthttp.RequestCtx](https://godoc.org/github.com/valyala/fasthttp#RequestCtx) that is compatible with the context.Context interface that requires a deadline, a cancellation signal, and other values across API boundaries.
357+
Context returns a context implementation that was set by user earlier or returns a non-nil, empty context, if it was not set earlier.
358358

359359
```go title="Signature"
360-
func (c Ctx) Context() *fasthttp.RequestCtx
360+
func (c Ctx) Context() context.Context
361361
```
362362

363-
:::info
364-
Please read the [Fasthttp Documentation](https://pkg.go.dev/github.com/valyala/fasthttp?tab=doc) for more information.
365-
:::
363+
```go title="Example"
364+
app.Get("/", func(c fiber.Ctx) error {
365+
ctx := c.Context()
366+
// ctx is context implementation set by user
367+
368+
// ...
369+
})
370+
```
366371

367372
## Cookie
368373

@@ -1489,6 +1494,18 @@ app.Get("/", func(c fiber.Ctx) error {
14891494
})
14901495
```
14911496
1497+
## RequestCtx
1498+
1499+
Returns [\*fasthttp.RequestCtx](https://godoc.org/github.com/valyala/fasthttp#RequestCtx) that is compatible with the context.Context interface that requires a deadline, a cancellation signal, and other values across API boundaries.
1500+
1501+
```go title="Signature"
1502+
func (c Ctx) RequestCtx() *fasthttp.RequestCtx
1503+
```
1504+
1505+
:::info
1506+
Please read the [Fasthttp Documentation](https://pkg.go.dev/github.com/valyala/fasthttp?tab=doc) for more information.
1507+
:::
1508+
14921509
## Response
14931510
14941511
Response return the [\*fasthttp.Response](https://godoc.org/github.com/valyala/fasthttp#Response) pointer
@@ -1891,18 +1908,18 @@ app.Get("/", func(c fiber.Ctx) error {
18911908
})
18921909
```
18931910

1894-
## SetUserContext
1911+
## SetContext
18951912

1896-
Sets the user specified implementation for context interface.
1913+
Sets the user specified implementation for context.Context interface.
18971914

18981915
```go title="Signature"
1899-
func (c Ctx) SetUserContext(ctx context.Context)
1916+
func (c Ctx) SetContext(ctx context.Context)
19001917
```
19011918

19021919
```go title="Example"
19031920
app.Get("/", func(c fiber.Ctx) error {
19041921
ctx := context.Background()
1905-
c.SetUserContext(ctx)
1922+
c.SetContext(ctx)
19061923
// Here ctx could be any context implementation
19071924
19081925
// ...
@@ -2005,24 +2022,6 @@ app.Get("/", func(c fiber.Ctx) error {
20052022
})
20062023
```
20072024

2008-
## UserContext
2009-
2010-
UserContext returns a context implementation that was set by user earlier
2011-
or returns a non-nil, empty context, if it was not set earlier.
2012-
2013-
```go title="Signature"
2014-
func (c Ctx) UserContext() context.Context
2015-
```
2016-
2017-
```go title="Example"
2018-
app.Get("/", func(c fiber.Ctx) error {
2019-
ctx := c.UserContext()
2020-
// ctx is context implementation set by user
2021-
2022-
// ...
2023-
})
2024-
```
2025-
20262025
## Vary
20272026

20282027
Adds the given header field to the [Vary](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary) response header. This will append the header, if not already listed, otherwise leaves it listed in the current location.

docs/core/middleware/timeout.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ There exist two distinct implementations of timeout middleware [Fiber](https://g
88

99
## New
1010

11-
As a `fiber.Handler` wrapper, it creates a context with `context.WithTimeout` and pass it in `UserContext`.
11+
As a `fiber.Handler` wrapper, it creates a context with `context.WithTimeout` which is then used with `c.Context()`.
1212

1313
If the context passed executions (eg. DB ops, Http calls) takes longer than the given duration to return, the timeout error is set and forwarded to the centralized `ErrorHandler`.
1414

@@ -38,7 +38,7 @@ func main() {
3838
app := fiber.New()
3939
h := func(c fiber.Ctx) error {
4040
sleepTime, _ := time.ParseDuration(c.Params("sleepTime") + "ms")
41-
if err := sleepWithContext(c.UserContext(), sleepTime); err != nil {
41+
if err := sleepWithContext(c.Context(), sleepTime); err != nil {
4242
return fmt.Errorf("%w: execution error", err)
4343
}
4444
return nil
@@ -84,7 +84,7 @@ func main() {
8484
app := fiber.New()
8585
h := func(c fiber.Ctx) error {
8686
sleepTime, _ := time.ParseDuration(c.Params("sleepTime") + "ms")
87-
if err := sleepWithContextWithCustomError(c.UserContext(), sleepTime); err != nil {
87+
if err := sleepWithContextWithCustomError(c.Context(), sleepTime); err != nil {
8888
return fmt.Errorf("%w: execution error", err)
8989
}
9090
return nil
@@ -116,7 +116,7 @@ func main() {
116116
db, _ := gorm.Open(postgres.Open("postgres://localhost/foodb"), &gorm.Config{})
117117

118118
handler := func(ctx fiber.Ctx) error {
119-
tran := db.WithContext(ctx.UserContext()).Begin()
119+
tran := db.WithContext(ctx.Context()).Begin()
120120

121121
if tran = tran.Exec("SELECT pg_sleep(50)"); tran.Error != nil {
122122
return tran.Error

docs/core/whats_new.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ DRAFT section
229229
- Format -> Param: body interface{} -> handlers ...ResFmt
230230
- Redirect -> c.Redirect().To()
231231
- SendFile now supports different configurations using the config parameter.
232+
- Context has been renamed to RequestCtx which corresponds to the FastHTTP Request Context.
233+
- UserContext has been renamed to Context which returns a context.Context object.
234+
- SetUserContext has been renamed to SetContext.
232235

233236
---
234237

0 commit comments

Comments
 (0)