Skip to content

Commit d6f5356

Browse files
authored
Merge pull request #195 from Fenny/master
v1.8.1 - Middleware + Compression
2 parents fa8e46f + edf0c7a commit d6f5356

21 files changed

+1018
-111
lines changed

.github/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ First of all, [download](https://golang.org/dl/) and install Go. `1.11` or highe
8888
Installation is done using the [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command:
8989

9090
```bash
91-
go get github.com/gofiber/fiber
91+
go get -u github.com/gofiber/fiber/...
9292
```
9393

9494
## 🤖 Benchmarks
@@ -235,7 +235,7 @@ func main() {
235235
"year": 1999,
236236
})
237237
})
238-
238+
239239
// ...
240240
}
241241
```
@@ -245,20 +245,20 @@ func main() {
245245
```go
246246
func main() {
247247
app := fiber.New()
248-
248+
249249
// Root API route
250250
api := app.Group("/api", cors()) // /api
251-
251+
252252
// API v1 routes
253253
v1 := api.Group("/v1", mysql()) // /api/v1
254254
v1.Get("/list", handler) // /api/v1/list
255255
v1.Get("/user", handler) // /api/v1/user
256-
256+
257257
// API v2 routes
258258
v2 := api.Group("/v2", mongodb()) // /api/v2
259259
v2.Get("/list", handler) // /api/v2/list
260260
v2.Get("/user", handler) // /api/v2/user
261-
261+
262262
// ...
263263
}
264264
```

.github/README_de.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Als erstes, [downloade](https://golang.org/dl/) und installiere Go. `1.11` oder
8888
Die Installation wird durch das [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) Kommando gestartet:
8989

9090
```bash
91-
go get github.com/gofiber/fiber
91+
go get -u github.com/gofiber/fiber/...
9292
```
9393

9494
## 🤖 Benchmarks

.github/README_fr.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Premièrement, [téléchargez](https://golang.org/dl/) et installez Go. Version
8888
L'installation est ensuite lancée via la commande [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them):
8989

9090
```bash
91-
go get github.com/gofiber/fiber
91+
go get -u github.com/gofiber/fiber/...
9292
```
9393

9494
## 🤖 Benchmarks

.github/README_ja.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func main() {
8989
そして、[`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them)コマンドを使用してインストールしてください。
9090

9191
```bash
92-
go get github.com/gofiber/fiber
92+
go get -u github.com/gofiber/fiber/...
9393
```
9494

9595
## 🤖 ベンチマーク

.github/README_ko.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func main() {
8888
[`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) 명령어를 이용해 설치가 완료됩니다:
8989

9090
```bash
91-
go get github.com/gofiber/fiber
91+
go get -u github.com/gofiber/fiber/...
9292
```
9393

9494
## 🤖 벤치마크

.github/README_pt.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Primeiro de tudo, faça o [download](https://golang.org/dl/) e instale o Go. É
8888
A instalação é feita usando o comando [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) :
8989

9090
```bash
91-
go get github.com/gofiber/fiber
91+
go get -u github.com/gofiber/fiber/...
9292
```
9393

9494
## 🤖 Benchmarks

.github/README_tr.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func main() {
8585
[`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) komutunu kullanarak kurulumu tamamlıyoruz:
8686

8787
```bash
88-
go get github.com/gofiber/fiber
88+
go get -u github.com/gofiber/fiber/...
8989
```
9090

9191
## 🤖 Performans Ölçümleri

.github/README_zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func main() {
8484
```bash
8585
export GO111MODULE=on
8686
export GOPROXY=https://goproxy.cn
87-
go get github.com/gofiber/fiber
87+
go get -u github.com/gofiber/fiber/...
8888
```
8989

9090
## 🤖 性能

app.go

+75-56
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,27 @@ type (
5050
ServerHeader string `default:""`
5151
// Enables handler values to be immutable even if you return from handler
5252
Immutable bool `default:"false"`
53+
// Enables GZip / Deflate compression for all responses
54+
Compression bool `default:"false"`
55+
// CompressionLevel int `default:"1"`
5356
// fasthttp settings
54-
GETOnly bool `default:"false"`
55-
IdleTimeout time.Duration `default:"0"`
56-
Concurrency int `default:"256 * 1024"`
57-
ReadTimeout time.Duration `default:"0"`
58-
WriteTimeout time.Duration `default:"0"`
59-
TCPKeepalive bool `default:"false"`
60-
MaxConnsPerIP int `default:"0"`
61-
ReadBufferSize int `default:"4096"`
62-
WriteBufferSize int `default:"4096"`
63-
ConcurrencySleep time.Duration `default:"0"`
64-
DisableKeepAlive bool `default:"false"`
65-
ReduceMemoryUsage bool `default:"false"`
66-
MaxRequestsPerConn int `default:"0"`
67-
TCPKeepalivePeriod time.Duration `default:"0"`
68-
MaxRequestBodySize int `default:"4 * 1024 * 1024"`
69-
NoHeaderNormalizing bool `default:"false"`
70-
NoDefaultContentType bool `default:"false"`
57+
// GETOnly bool `default:"false"`
58+
// IdleTimeout time.Duration `default:"0"`
59+
// Concurrency int `default:"256 * 1024"`
60+
// ReadTimeout time.Duration `default:"0"`
61+
// WriteTimeout time.Duration `default:"0"`
62+
// TCPKeepalive bool `default:"false"`
63+
// MaxConnsPerIP int `default:"0"`
64+
// ReadBufferSize int `default:"4096"`
65+
// WriteBufferSize int `default:"4096"`
66+
// ConcurrencySleep time.Duration `default:"0"`
67+
// DisableKeepAlive bool `default:"false"`
68+
// ReduceMemoryUsage bool `default:"false"`
69+
// MaxRequestsPerConn int `default:"0"`
70+
// TCPKeepalivePeriod time.Duration `default:"0"`
71+
BodyLimit int `default:"4 * 1024 * 1024"`
72+
// NoHeaderNormalizing bool `default:"false"`
73+
// NoDefaultContentType bool `default:"false"`
7174
// template settings
7275
TemplateFolder string `default:""`
7376
TemplateEngine string `default:""`
@@ -104,27 +107,30 @@ func New(settings ...*Settings) (app *App) {
104107
return string(b)
105108
}
106109
}
107-
if opt.Concurrency == 0 {
108-
opt.Concurrency = 256 * 1024
109-
}
110-
if opt.ReadBufferSize == 0 {
111-
opt.ReadBufferSize = 4096
112-
}
113-
if opt.WriteBufferSize == 0 {
114-
opt.WriteBufferSize = 4096
115-
}
116-
if opt.MaxRequestBodySize == 0 {
117-
opt.MaxRequestBodySize = 4 * 1024 * 1024
110+
// if opt.Concurrency == 0 {
111+
// opt.Concurrency = 256 * 1024
112+
// }
113+
// if opt.ReadBufferSize == 0 {
114+
// opt.ReadBufferSize = 4096
115+
// }
116+
// if opt.WriteBufferSize == 0 {
117+
// opt.WriteBufferSize = 4096
118+
// }
119+
if opt.BodyLimit == 0 {
120+
opt.BodyLimit = 4 * 1024 * 1024
118121
}
122+
// if opt.CompressionLevel == 0 {
123+
// opt.CompressionLevel = 1
124+
// }
119125
app.Settings = opt
120126
return
121127
}
122128
app.Settings = &Settings{
123-
Prefork: prefork,
124-
Concurrency: 256 * 1024,
125-
ReadBufferSize: 4096,
126-
WriteBufferSize: 4096,
127-
MaxRequestBodySize: 4 * 1024 * 1024,
129+
Prefork: prefork,
130+
// Concurrency: 256 * 1024,
131+
// ReadBufferSize: 4096,
132+
// WriteBufferSize: 4096,
133+
BodyLimit: 4 * 1024 * 1024,
128134
}
129135
return
130136
}
@@ -335,11 +341,11 @@ func (app *App) Test(request *http.Request) (*http.Response, error) {
335341
func (app *App) prefork(address string) (ln net.Listener, err error) {
336342
// Master proc
337343
if !app.child {
338-
addr, err := net.ResolveTCPAddr("tcp4", address)
344+
addr, err := net.ResolveTCPAddr("tcp", address)
339345
if err != nil {
340346
return ln, err
341347
}
342-
tcplistener, err := net.ListenTCP("tcp4", addr)
348+
tcplistener, err := net.ListenTCP("tcp", addr)
343349
if err != nil {
344350
return ln, err
345351
}
@@ -373,31 +379,44 @@ func (app *App) prefork(address string) (ln net.Listener, err error) {
373379
return ln, err
374380
}
375381

382+
type disableLogger struct{}
383+
384+
func (dl *disableLogger) Printf(format string, args ...interface{}) {
385+
// fmt.Println(fmt.Sprintf(format, args...))
386+
}
387+
376388
func (app *App) newServer() *fasthttp.Server {
377389
return &fasthttp.Server{
378390
Handler: app.handler,
391+
Name: app.Settings.ServerHeader,
392+
// Concurrency: app.Settings.Concurrency,
393+
// SleepWhenConcurrencyLimitsExceeded: app.Settings.ConcurrencySleep,
394+
// DisableKeepalive: app.Settings.DisableKeepAlive,
395+
// ReadBufferSize: app.Settings.ReadBufferSize,
396+
// WriteBufferSize: app.Settings.WriteBufferSize,
397+
// ReadTimeout: app.Settings.ReadTimeout,
398+
// WriteTimeout: app.Settings.WriteTimeout,
399+
// IdleTimeout: app.Settings.IdleTimeout,
400+
// MaxConnsPerIP: app.Settings.MaxConnsPerIP,
401+
// MaxRequestsPerConn: app.Settings.MaxRequestsPerConn,
402+
// TCPKeepalive: app.Settings.TCPKeepalive,
403+
// TCPKeepalivePeriod: app.Settings.TCPKeepalivePeriod,
404+
MaxRequestBodySize: app.Settings.BodyLimit,
405+
// ReduceMemoryUsage: app.Settings.ReduceMemoryUsage,
406+
// GetOnly: app.Settings.GETOnly,
407+
// DisableHeaderNamesNormalizing: app.Settings.NoHeaderNormalizing,
408+
NoDefaultServerHeader: app.Settings.ServerHeader == "",
409+
// NoDefaultContentType: app.Settings.NoDefaultContentType,
410+
Logger: &disableLogger{},
411+
LogAllErrors: false,
379412
ErrorHandler: func(ctx *fasthttp.RequestCtx, err error) {
380-
ctx.Response.SetStatusCode(400)
381-
ctx.Response.SetBodyString("Bad Request")
413+
if err.Error() == "body size exceeds the given limit" {
414+
ctx.Response.SetStatusCode(413)
415+
ctx.Response.SetBodyString("Request Entity Too Large")
416+
} else {
417+
ctx.Response.SetStatusCode(400)
418+
ctx.Response.SetBodyString("Bad Request")
419+
}
382420
},
383-
Name: app.Settings.ServerHeader,
384-
Concurrency: app.Settings.Concurrency,
385-
SleepWhenConcurrencyLimitsExceeded: app.Settings.ConcurrencySleep,
386-
DisableKeepalive: app.Settings.DisableKeepAlive,
387-
ReadBufferSize: app.Settings.ReadBufferSize,
388-
WriteBufferSize: app.Settings.WriteBufferSize,
389-
ReadTimeout: app.Settings.ReadTimeout,
390-
WriteTimeout: app.Settings.WriteTimeout,
391-
IdleTimeout: app.Settings.IdleTimeout,
392-
MaxConnsPerIP: app.Settings.MaxConnsPerIP,
393-
MaxRequestsPerConn: app.Settings.MaxRequestsPerConn,
394-
TCPKeepalive: app.Settings.TCPKeepalive,
395-
TCPKeepalivePeriod: app.Settings.TCPKeepalivePeriod,
396-
MaxRequestBodySize: app.Settings.MaxRequestBodySize,
397-
ReduceMemoryUsage: app.Settings.ReduceMemoryUsage,
398-
GetOnly: app.Settings.GETOnly,
399-
DisableHeaderNamesNormalizing: app.Settings.NoHeaderNormalizing,
400-
NoDefaultServerHeader: app.Settings.ServerHeader == "",
401-
NoDefaultContentType: app.Settings.NoDefaultContentType,
402421
}
403422
}

0 commit comments

Comments
 (0)