Skip to content

Commit

Permalink
Merge pull request #195 from Fenny/master
Browse files Browse the repository at this point in the history
v1.8.1 - Middleware + Compression
  • Loading branch information
Fenny committed Mar 1, 2020
2 parents fa8e46f + edf0c7a commit d6f5356
Show file tree
Hide file tree
Showing 21 changed files with 1,018 additions and 111 deletions.
12 changes: 6 additions & 6 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ First of all, [download](https://golang.org/dl/) and install Go. `1.11` or highe
Installation is done using the [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command:

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 Benchmarks
Expand Down Expand Up @@ -235,7 +235,7 @@ func main() {
"year": 1999,
})
})

// ...
}
```
Expand All @@ -245,20 +245,20 @@ func main() {
```go
func main() {
app := fiber.New()

// Root API route
api := app.Group("/api", cors()) // /api

// API v1 routes
v1 := api.Group("/v1", mysql()) // /api/v1
v1.Get("/list", handler) // /api/v1/list
v1.Get("/user", handler) // /api/v1/user

// API v2 routes
v2 := api.Group("/v2", mongodb()) // /api/v2
v2.Get("/list", handler) // /api/v2/list
v2.Get("/user", handler) // /api/v2/user

// ...
}
```
Expand Down
2 changes: 1 addition & 1 deletion .github/README_de.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Als erstes, [downloade](https://golang.org/dl/) und installiere Go. `1.11` oder
Die Installation wird durch das [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) Kommando gestartet:

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 Benchmarks
Expand Down
2 changes: 1 addition & 1 deletion .github/README_fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Premièrement, [téléchargez](https://golang.org/dl/) et installez Go. Version
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):

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 Benchmarks
Expand Down
2 changes: 1 addition & 1 deletion .github/README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
そして、[`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them)コマンドを使用してインストールしてください。

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 ベンチマーク
Expand Down
2 changes: 1 addition & 1 deletion .github/README_ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func main() {
[`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) 명령어를 이용해 설치가 완료됩니다:

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 벤치마크
Expand Down
2 changes: 1 addition & 1 deletion .github/README_pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Primeiro de tudo, faça o [download](https://golang.org/dl/) e instale o Go. É
A instalação é feita usando o comando [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) :

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 Benchmarks
Expand Down
2 changes: 1 addition & 1 deletion .github/README_tr.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
[`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) komutunu kullanarak kurulumu tamamlıyoruz:

```bash
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 Performans Ölçümleri
Expand Down
2 changes: 1 addition & 1 deletion .github/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
```bash
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
go get github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 性能
Expand Down
131 changes: 75 additions & 56 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,27 @@ type (
ServerHeader string `default:""`
// Enables handler values to be immutable even if you return from handler
Immutable bool `default:"false"`
// Enables GZip / Deflate compression for all responses
Compression bool `default:"false"`
// CompressionLevel int `default:"1"`
// fasthttp settings
GETOnly bool `default:"false"`
IdleTimeout time.Duration `default:"0"`
Concurrency int `default:"256 * 1024"`
ReadTimeout time.Duration `default:"0"`
WriteTimeout time.Duration `default:"0"`
TCPKeepalive bool `default:"false"`
MaxConnsPerIP int `default:"0"`
ReadBufferSize int `default:"4096"`
WriteBufferSize int `default:"4096"`
ConcurrencySleep time.Duration `default:"0"`
DisableKeepAlive bool `default:"false"`
ReduceMemoryUsage bool `default:"false"`
MaxRequestsPerConn int `default:"0"`
TCPKeepalivePeriod time.Duration `default:"0"`
MaxRequestBodySize int `default:"4 * 1024 * 1024"`
NoHeaderNormalizing bool `default:"false"`
NoDefaultContentType bool `default:"false"`
// GETOnly bool `default:"false"`
// IdleTimeout time.Duration `default:"0"`
// Concurrency int `default:"256 * 1024"`
// ReadTimeout time.Duration `default:"0"`
// WriteTimeout time.Duration `default:"0"`
// TCPKeepalive bool `default:"false"`
// MaxConnsPerIP int `default:"0"`
// ReadBufferSize int `default:"4096"`
// WriteBufferSize int `default:"4096"`
// ConcurrencySleep time.Duration `default:"0"`
// DisableKeepAlive bool `default:"false"`
// ReduceMemoryUsage bool `default:"false"`
// MaxRequestsPerConn int `default:"0"`
// TCPKeepalivePeriod time.Duration `default:"0"`
BodyLimit int `default:"4 * 1024 * 1024"`
// NoHeaderNormalizing bool `default:"false"`
// NoDefaultContentType bool `default:"false"`
// template settings
TemplateFolder string `default:""`
TemplateEngine string `default:""`
Expand Down Expand Up @@ -104,27 +107,30 @@ func New(settings ...*Settings) (app *App) {
return string(b)
}
}
if opt.Concurrency == 0 {
opt.Concurrency = 256 * 1024
}
if opt.ReadBufferSize == 0 {
opt.ReadBufferSize = 4096
}
if opt.WriteBufferSize == 0 {
opt.WriteBufferSize = 4096
}
if opt.MaxRequestBodySize == 0 {
opt.MaxRequestBodySize = 4 * 1024 * 1024
// if opt.Concurrency == 0 {
// opt.Concurrency = 256 * 1024
// }
// if opt.ReadBufferSize == 0 {
// opt.ReadBufferSize = 4096
// }
// if opt.WriteBufferSize == 0 {
// opt.WriteBufferSize = 4096
// }
if opt.BodyLimit == 0 {
opt.BodyLimit = 4 * 1024 * 1024
}
// if opt.CompressionLevel == 0 {
// opt.CompressionLevel = 1
// }
app.Settings = opt
return
}
app.Settings = &Settings{
Prefork: prefork,
Concurrency: 256 * 1024,
ReadBufferSize: 4096,
WriteBufferSize: 4096,
MaxRequestBodySize: 4 * 1024 * 1024,
Prefork: prefork,
// Concurrency: 256 * 1024,
// ReadBufferSize: 4096,
// WriteBufferSize: 4096,
BodyLimit: 4 * 1024 * 1024,
}
return
}
Expand Down Expand Up @@ -335,11 +341,11 @@ func (app *App) Test(request *http.Request) (*http.Response, error) {
func (app *App) prefork(address string) (ln net.Listener, err error) {
// Master proc
if !app.child {
addr, err := net.ResolveTCPAddr("tcp4", address)
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return ln, err
}
tcplistener, err := net.ListenTCP("tcp4", addr)
tcplistener, err := net.ListenTCP("tcp", addr)
if err != nil {
return ln, err
}
Expand Down Expand Up @@ -373,31 +379,44 @@ func (app *App) prefork(address string) (ln net.Listener, err error) {
return ln, err
}

type disableLogger struct{}

func (dl *disableLogger) Printf(format string, args ...interface{}) {
// fmt.Println(fmt.Sprintf(format, args...))
}

func (app *App) newServer() *fasthttp.Server {
return &fasthttp.Server{
Handler: app.handler,
Name: app.Settings.ServerHeader,
// Concurrency: app.Settings.Concurrency,
// SleepWhenConcurrencyLimitsExceeded: app.Settings.ConcurrencySleep,
// DisableKeepalive: app.Settings.DisableKeepAlive,
// ReadBufferSize: app.Settings.ReadBufferSize,
// WriteBufferSize: app.Settings.WriteBufferSize,
// ReadTimeout: app.Settings.ReadTimeout,
// WriteTimeout: app.Settings.WriteTimeout,
// IdleTimeout: app.Settings.IdleTimeout,
// MaxConnsPerIP: app.Settings.MaxConnsPerIP,
// MaxRequestsPerConn: app.Settings.MaxRequestsPerConn,
// TCPKeepalive: app.Settings.TCPKeepalive,
// TCPKeepalivePeriod: app.Settings.TCPKeepalivePeriod,
MaxRequestBodySize: app.Settings.BodyLimit,
// ReduceMemoryUsage: app.Settings.ReduceMemoryUsage,
// GetOnly: app.Settings.GETOnly,
// DisableHeaderNamesNormalizing: app.Settings.NoHeaderNormalizing,
NoDefaultServerHeader: app.Settings.ServerHeader == "",
// NoDefaultContentType: app.Settings.NoDefaultContentType,
Logger: &disableLogger{},
LogAllErrors: false,
ErrorHandler: func(ctx *fasthttp.RequestCtx, err error) {
ctx.Response.SetStatusCode(400)
ctx.Response.SetBodyString("Bad Request")
if err.Error() == "body size exceeds the given limit" {
ctx.Response.SetStatusCode(413)
ctx.Response.SetBodyString("Request Entity Too Large")
} else {
ctx.Response.SetStatusCode(400)
ctx.Response.SetBodyString("Bad Request")
}
},
Name: app.Settings.ServerHeader,
Concurrency: app.Settings.Concurrency,
SleepWhenConcurrencyLimitsExceeded: app.Settings.ConcurrencySleep,
DisableKeepalive: app.Settings.DisableKeepAlive,
ReadBufferSize: app.Settings.ReadBufferSize,
WriteBufferSize: app.Settings.WriteBufferSize,
ReadTimeout: app.Settings.ReadTimeout,
WriteTimeout: app.Settings.WriteTimeout,
IdleTimeout: app.Settings.IdleTimeout,
MaxConnsPerIP: app.Settings.MaxConnsPerIP,
MaxRequestsPerConn: app.Settings.MaxRequestsPerConn,
TCPKeepalive: app.Settings.TCPKeepalive,
TCPKeepalivePeriod: app.Settings.TCPKeepalivePeriod,
MaxRequestBodySize: app.Settings.MaxRequestBodySize,
ReduceMemoryUsage: app.Settings.ReduceMemoryUsage,
GetOnly: app.Settings.GETOnly,
DisableHeaderNamesNormalizing: app.Settings.NoHeaderNormalizing,
NoDefaultServerHeader: app.Settings.ServerHeader == "",
NoDefaultContentType: app.Settings.NoDefaultContentType,
}
}

0 comments on commit d6f5356

Please sign in to comment.