Skip to content

Commit 32dd82b

Browse files
authored
Merge branch 'main' into feat/add-bind-all
2 parents 0c41334 + 85cce3c commit 32dd82b

32 files changed

+2332
-450
lines changed

.github/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ Fiber is optimized for **high-performance**, meaning values returned from **fibe
9191

9292
## 🤖 Benchmarks
9393

94-
These tests are performed by [TechEmpower](https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=plaintext) and [Go Web](https://github.com/smallnest/go-web-framework-benchmark). If you want to see all the results, please visit our [Wiki](https://docs.gofiber.io/extra/benchmarks).
94+
These tests are performed by [TechEmpower](https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=plaintext). If you want to see all the results, please visit our [Wiki](https://docs.gofiber.io/extra/benchmarks).
9595

9696
<p float="left" align="middle">
97-
<img src="https://raw.githubusercontent.com/gofiber/docs/master/static/img/benchmark-pipeline.png" width="49%">
98-
<img src="https://raw.githubusercontent.com/gofiber/docs/master/static/img/benchmark_alloc.png" width="49%">
97+
<img src="https://raw.githubusercontent.com/gofiber/docs/master/static/img/plaintext.png" width="49%">
98+
<img src="https://raw.githubusercontent.com/gofiber/docs/master/static/img/json.png" width="49%">
9999
</p>
100100

101101
## 🎯 Features

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232

3333
- name: Upload coverage reports to Codecov
3434
if: ${{ matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.23.x' }}
35-
uses: codecov/[email protected].0
35+
uses: codecov/[email protected].2
3636
with:
3737
token: ${{ secrets.CODECOV_TOKEN }}
3838
file: ./coverage.txt

app.go

+31-9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ type App struct {
106106
tlsHandler *TLSHandler
107107
// Mount fields
108108
mountFields *mountFields
109+
// state management
110+
state *State
109111
// Route stack divided by HTTP methods
110112
stack [][]*Route
111113
// Route stack divided by HTTP methods and route prefixes
@@ -456,17 +458,29 @@ const (
456458
DefaultWriteBufferSize = 4096
457459
)
458460

461+
const (
462+
methodGet = iota
463+
methodHead
464+
methodPost
465+
methodPut
466+
methodDelete
467+
methodConnect
468+
methodOptions
469+
methodTrace
470+
methodPatch
471+
)
472+
459473
// HTTP methods enabled by default
460474
var DefaultMethods = []string{
461-
MethodGet,
462-
MethodHead,
463-
MethodPost,
464-
MethodPut,
465-
MethodDelete,
466-
MethodConnect,
467-
MethodOptions,
468-
MethodTrace,
469-
MethodPatch,
475+
methodGet: MethodGet,
476+
methodHead: MethodHead,
477+
methodPost: MethodPost,
478+
methodPut: MethodPut,
479+
methodDelete: MethodDelete,
480+
methodConnect: MethodConnect,
481+
methodOptions: MethodOptions,
482+
methodTrace: MethodTrace,
483+
methodPatch: MethodPatch,
470484
}
471485

472486
// DefaultErrorHandler that process return errors from handlers
@@ -515,6 +529,9 @@ func New(config ...Config) *App {
515529
// Define mountFields
516530
app.mountFields = newMountFields(app)
517531

532+
// Define state
533+
app.state = newState()
534+
518535
// Override config if provided
519536
if len(config) > 0 {
520537
app.config = config[0]
@@ -952,6 +969,11 @@ func (app *App) Hooks() *Hooks {
952969
return app.hooks
953970
}
954971

972+
// State returns the state struct to store global data in order to share it between handlers.
973+
func (app *App) State() *State {
974+
return app.state
975+
}
976+
955977
var ErrTestGotEmptyResponse = errors.New("test: got empty response")
956978

957979
// TestConfig is a struct holding Test settings

app_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,16 @@ func Test_Route_Naming_Issue_2671_2685(t *testing.T) {
18901890
require.Equal(t, "/simple-route", sRoute2.Path)
18911891
}
18921892

1893+
func Test_App_State(t *testing.T) {
1894+
t.Parallel()
1895+
app := New()
1896+
1897+
app.State().Set("key", "value")
1898+
str, ok := app.State().GetString("key")
1899+
require.True(t, ok)
1900+
require.Equal(t, "value", str)
1901+
}
1902+
18931903
// go test -v -run=^$ -bench=Benchmark_Communication_Flow -benchmem -count=4
18941904
func Benchmark_Communication_Flow(b *testing.B) {
18951905
app := New()

ctx.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ type DefaultCtx struct {
6161
res *DefaultRes // Default response api reference
6262
values [maxParams]string // Route parameter values
6363
viewBindMap sync.Map // Default view map to bind template engine
64-
method string // HTTP method
6564
baseURI string // HTTP base uri
6665
pathOriginal string // Original HTTP path
6766
flashMessages redirectionMsgs // Flash messages
@@ -70,7 +69,7 @@ type DefaultCtx struct {
7069
treePathHash int // Hash of the path for the search in the tree
7170
indexRoute int // Index of the current route
7271
indexHandler int // Index of the current handler
73-
methodINT int // HTTP method INT equivalent
72+
methodInt int // HTTP method INT equivalent
7473
matched bool // Non use route matched
7574
}
7675

@@ -1006,19 +1005,17 @@ func (c *DefaultCtx) Location(path string) {
10061005
func (c *DefaultCtx) Method(override ...string) string {
10071006
if len(override) == 0 {
10081007
// Nothing to override, just return current method from context
1009-
return c.method
1008+
return c.app.method(c.methodInt)
10101009
}
10111010

10121011
method := utils.ToUpper(override[0])
1013-
mINT := c.app.methodInt(method)
1014-
if mINT == -1 {
1012+
methodInt := c.app.methodInt(method)
1013+
if methodInt == -1 {
10151014
// Provided override does not valid HTTP method, no override, return current method
1016-
return c.method
1015+
return c.app.method(c.methodInt)
10171016
}
1018-
1019-
c.method = method
1020-
c.methodINT = mINT
1021-
return c.method
1017+
c.methodInt = methodInt
1018+
return method
10221019
}
10231020

10241021
// MultipartForm parse form entries from binary.
@@ -1486,7 +1483,7 @@ func (c *DefaultCtx) Route() *Route {
14861483
return &Route{
14871484
path: c.pathOriginal,
14881485
Path: c.pathOriginal,
1489-
Method: c.method,
1486+
Method: c.Method(),
14901487
Handlers: make([]Handler, 0),
14911488
Params: make([]string, 0),
14921489
}
@@ -1919,8 +1916,7 @@ func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) {
19191916
// Set paths
19201917
c.pathOriginal = c.app.getString(fctx.URI().PathOriginal())
19211918
// Set method
1922-
c.method = c.app.getString(fctx.Request.Header.Method())
1923-
c.methodINT = c.app.methodInt(c.method)
1919+
c.methodInt = c.app.methodInt(utils.UnsafeString(fctx.Request.Header.Method()))
19241920
// Attach *fasthttp.RequestCtx to ctx
19251921
c.fasthttp = fctx
19261922
// reset base uri
@@ -1951,8 +1947,8 @@ func (c *DefaultCtx) getBody() []byte {
19511947
}
19521948

19531949
// Methods to use with next stack.
1954-
func (c *DefaultCtx) getMethodINT() int {
1955-
return c.methodINT
1950+
func (c *DefaultCtx) getMethodInt() int {
1951+
return c.methodInt
19561952
}
19571953

19581954
func (c *DefaultCtx) getIndexRoute() int {

ctx_interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type CustomCtx interface {
1717
Reset(fctx *fasthttp.RequestCtx)
1818

1919
// Methods to use with next stack.
20-
getMethodINT() int
20+
getMethodInt() int
2121
getIndexRoute() int
2222
getTreePathHash() int
2323
getDetectionPath() string

ctx_interface_gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/constants.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: constants
33
title: 📋 Constants
44
description: Some constants for Fiber.
5-
sidebar_position: 8
5+
sidebar_position: 9
66
---
77

88
### HTTP methods were copied from net/http

0 commit comments

Comments
 (0)