diff --git a/.github/README.md b/.github/README.md index ef7f4ad6bb..765596600b 100644 --- a/.github/README.md +++ b/.github/README.md @@ -157,8 +157,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -507,10 +507,28 @@ If you want to say **thank you** and/or support the active development of `Fiber + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_de.md b/.github/README_de.md index 42474fe68a..18f4144e65 100644 --- a/.github/README_de.md +++ b/.github/README_de.md @@ -156,8 +156,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -505,10 +505,28 @@ Falls du **danke** sagen möchtest und/oder aktiv die Entwicklung von `fiber` f + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_es.md b/.github/README_es.md index 73abc66fa8..103ba074be 100644 --- a/.github/README_es.md +++ b/.github/README_es.md @@ -156,8 +156,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -486,10 +486,28 @@ Si quiere **agradecer** y/o apoyar el desarrollo activo de la `Fiber`: + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_fr.md b/.github/README_fr.md index 67ffc88a5f..c646ba2d84 100644 --- a/.github/README_fr.md +++ b/.github/README_fr.md @@ -156,8 +156,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -486,10 +486,28 @@ Si vous voulez nous remercier et/ou soutenir le développement actif de `Fiber`: + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_id.md b/.github/README_id.md index 28db2488ea..f6f165da76 100644 --- a/.github/README_id.md +++ b/.github/README_id.md @@ -158,8 +158,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -489,10 +489,28 @@ Apabila anda ingin mengucapkan **terima kasih** dan/atau mendukung pengembangan + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_ja.md b/.github/README_ja.md index 481424f354..7d62f7dc85 100644 --- a/.github/README_ja.md +++ b/.github/README_ja.md @@ -160,8 +160,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -492,10 +492,28 @@ func main() { + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_ko.md b/.github/README_ko.md index 591a602126..6f764f769b 100644 --- a/.github/README_ko.md +++ b/.github/README_ko.md @@ -160,8 +160,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -490,10 +490,28 @@ func main() { + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_pt.md b/.github/README_pt.md index 368b8aaae2..886b382d57 100644 --- a/.github/README_pt.md +++ b/.github/README_pt.md @@ -156,8 +156,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -486,10 +486,28 @@ Se você quer **agradecer** e/ou apoiar o desenvolvimento ativo do `Fiber`: + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_ru.md b/.github/README_ru.md index 89b9fda271..7261f061de 100644 --- a/.github/README_ru.md +++ b/.github/README_ru.md @@ -158,8 +158,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -507,10 +507,28 @@ func main() { + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_tr.md b/.github/README_tr.md index d6acacaade..f2f9ad370e 100644 --- a/.github/README_tr.md +++ b/.github/README_tr.md @@ -156,8 +156,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -487,10 +487,28 @@ Eğer **teşekkür etmek** ve/veya `Fiber` ın aktif geliştirilmesini destekle + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.github/README_zh-CN.md b/.github/README_zh-CN.md index 0733934206..23c48907a1 100644 --- a/.github/README_zh-CN.md +++ b/.github/README_zh-CN.md @@ -156,8 +156,8 @@ func main() { }) // GET /api/register - app.Get("/api*", func(c *fiber.Ctx) { - fmt.Printf("/api%s", c.Params("*")) + app.Get("/api/*", func(c *fiber.Ctx) { + fmt.Printf("/api/%s", c.Params("*")) // => /api/register }) @@ -487,10 +487,28 @@ func main() { + + +
+ +
+ Ray Mayemir +
+
+ +
+ melkorm +
+
+ +
+ Eka Putra +
+

- HenrikBinggl + Henrik Binggl
diff --git a/.travis.yml b/.travis.yml index 1cf1ac741b..09c7dc26ff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,3 @@ script: - GOARCH=386 go build - go test -v - - go test -race -v diff --git a/app.go b/app.go index 2baadff991..f9bbd5a51c 100644 --- a/app.go +++ b/app.go @@ -24,7 +24,7 @@ import ( ) // Version of current package -const Version = "1.8.43" +const Version = "1.9.0" // Map is a shortcut for map[string]interface{} type Map map[string]interface{} @@ -70,32 +70,23 @@ type Group struct { app *App } -// Global variables -var isPrefork, isChild bool - // New creates a new Fiber named instance. // You can pass optional settings when creating a new instance. func New(settings ...*Settings) *App { - // Parse arguments - for _, v := range os.Args[1:] { - if v == "-prefork" { - isPrefork = true - } else if v == "-child" { - isChild = true - } - } + schemaDecoderForm.SetAliasTag("form") + schemaDecoderQuery.SetAliasTag("query") // Create app app := new(App) // Create settings app.Settings = new(Settings) // Set default settings - app.Settings.Prefork = isPrefork + app.Settings.Prefork = isPrefork() app.Settings.BodyLimit = 4 * 1024 * 1024 // If settings exist, set defaults if len(settings) > 0 { app.Settings = settings[0] // Set custom settings if !app.Settings.Prefork { // Default to -prefork flag if false - app.Settings.Prefork = isPrefork + app.Settings.Prefork = isPrefork() } if app.Settings.BodyLimit == 0 { // Default MaxRequestBodySize app.Settings.BodyLimit = 4 * 1024 * 1024 @@ -323,6 +314,25 @@ func (grp *Group) All(path string, handlers ...func(*Ctx)) *Group { return grp } +// Serve can be used to pass a custom listener +// This method does not support the Prefork feature +// You can pass an optional *tls.Config to enable TLS. +func (app *App) Serve(ln net.Listener, tlsconfig ...*tls.Config) error { + // Create fasthttp server + app.server = app.newServer() + // TLS config + if len(tlsconfig) > 0 { + ln = tls.NewListener(ln, tlsconfig[0]) + } + // Preforkin is not available using app.Serve(ln net.Listener) + if app.Settings.Prefork { + fmt.Println("Preforking is not available with 'Serve' please use 'Listen' to enable it.") + } + // Print listening message + fmt.Printf("Fiber v%s listening on %s\n", Version, ln.Addr().String()) + return app.server.Serve(ln) +} + // Listen serves HTTP requests from the given addr or port. // You can pass an optional *tls.Config to enable TLS. func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error { @@ -343,7 +353,7 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error { var ln net.Listener var err error // Prefork enabled - if app.Settings.Prefork && runtime.NumCPU() > 1 { + if app.Settings.Prefork && runtime.NumCPU() > 1 && runtime.GOOS != "windows" { if ln, err = app.prefork(addr); err != nil { return err } @@ -352,14 +362,12 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error { return err } } - // TLS config if len(tlsconfig) > 0 { ln = tls.NewListener(ln, tlsconfig[0]) } - // Print listening message - if !isChild { + if !isChild() { fmt.Printf("Fiber v%s listening on %s\n", Version, addr) } return app.server.Serve(ln) @@ -379,7 +387,7 @@ func (app *App) Shutdown() error { return app.server.Shutdown() } -// Test is used for internal debugging by passing a *http.Request. +// Test is used for internal debugging by passing a *http.Request // Timeout is optional and defaults to 200ms, -1 will disable it completely. func (app *App) Test(request *http.Request, msTimeout ...int) (*http.Response, error) { timeout := 200 @@ -430,7 +438,7 @@ func (app *App) Test(request *http.Request, msTimeout ...int) (*http.Response, e // Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ func (app *App) prefork(address string) (ln net.Listener, err error) { // Master proc - if !isChild { + if !isChild() { addr, err := net.ResolveTCPAddr("tcp", address) if err != nil { return ln, err diff --git a/app_test.go b/app_test.go index 3a531ed1f1..537286fd0b 100644 --- a/app_test.go +++ b/app_test.go @@ -2,11 +2,17 @@ // 📌 API Documentation: https://fiber.wiki // 📝 Github Repository: https://github.com/gofiber/fiber +// go test -v -coverprofile cover.out . +// go tool cover -html=cover.out -o cover.html +// open cover.html + package fiber import ( + "net" "net/http" "testing" + "time" ) var handler = func(c *Ctx) {} @@ -67,6 +73,20 @@ func Test_Methods(t *testing.T) { } +func Test_New(t *testing.T) { + app := New(&Settings{ + Immutable: true, + }) + app.Get("/", func(*Ctx) { + + }) +} + +func Test_Shutdown(t *testing.T) { + app := New() + _ = app.Shutdown() +} + func Test_Static(t *testing.T) { app := New() grp := app.Group("/v1") @@ -167,18 +187,31 @@ func Test_Group(t *testing.T) { is200(t, app, "/test/v1/users", "GET") } -// func Test_Listen(t *testing.T) { -// t.Parallel() -// app := New() -// app.Banner = false -// go func() { -// time.Sleep(1 * time.Second) -// _ = app.Shutdown() -// }() -// app.Listen(3002) -// go func() { -// time.Sleep(1 * time.Second) -// _ = app.Shutdown() -// }() -// app.Listen("3002") -// } +func Test_Listen(t *testing.T) { + app := New() + go func() { + time.Sleep(500 * time.Millisecond) + _ = app.Shutdown() + }() + app.Listen(3002) + go func() { + time.Sleep(500 * time.Millisecond) + _ = app.Shutdown() + }() + app.Listen("3003") +} + +func Test_Serve(t *testing.T) { + app := New(&Settings{ + Prefork: true, + }) + ln, err := net.Listen("tcp4", ":3004") + if err != nil { + t.Fatalf(`%s: %s`, t.Name(), err) + } + go func() { + time.Sleep(500 * time.Millisecond) + _ = app.Shutdown() + }() + app.Serve(ln) +} diff --git a/ctx.go b/ctx.go index 0dea77a8fe..e768b9abab 100644 --- a/ctx.go +++ b/ctx.go @@ -61,7 +61,8 @@ type Cookie struct { // Global variables var jsonParser = jsoniter.ConfigCompatibleWithStandardLibrary -var schemaDecoder = schema.NewDecoder() +var schemaDecoderForm = schema.NewDecoder() +var schemaDecoderQuery = schema.NewDecoder() // Ctx pool var poolCtx = sync.Pool{ @@ -256,7 +257,7 @@ func (ctx *Ctx) Body(key ...string) string { // It supports decoding the following content types based on the Content-Type header: // application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data func (ctx *Ctx) BodyParser(out interface{}) error { - // TODO : Query Params + // get content type ctype := getString(ctx.Fasthttp.Request.Header.ContentType()) // application/json if strings.HasPrefix(ctype, MIMEApplicationJSON) { @@ -272,7 +273,7 @@ func (ctx *Ctx) BodyParser(out interface{}) error { if err != nil { return err } - return schemaDecoder.Decode(out, data) + return schemaDecoderForm.Decode(out, data) } // multipart/form-data if strings.HasPrefix(ctype, MIMEMultipartForm) { @@ -280,9 +281,17 @@ func (ctx *Ctx) BodyParser(out interface{}) error { if err != nil { return err } - return schemaDecoder.Decode(out, data.Value) - + return schemaDecoderForm.Decode(out, data.Value) + } + // query Params + if ctx.Fasthttp.QueryArgs().Len() > 0 { + data := make(map[string][]string) + ctx.Fasthttp.QueryArgs().VisitAll(func(key []byte, val []byte) { + data[getString(key)] = []string{getString(val)} + }) + return schemaDecoderQuery.Decode(out, data) } + return fmt.Errorf("BodyParser: cannot parse content-type: %v", ctype) } diff --git a/ctx_test.go b/ctx_test.go index 51f2cc424b..0deab880db 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -156,11 +156,11 @@ func Test_Body(t *testing.T) { func Test_BodyParser(t *testing.T) { app := New() type Demo struct { - Name string `json:"name"` + Name string `json:"name" xml:"name" form:"name" query:"name"` } app.Post("/test", func(c *Ctx) { d := new(Demo) - err := c.BodyParser(&d) + err := c.BodyParser(d) if err != nil { t.Fatalf(`%s: BodyParser %v`, t.Name(), err) } @@ -176,6 +176,26 @@ func Test_BodyParser(t *testing.T) { if err != nil { t.Fatalf(`%s: %s`, t.Name(), err) } + + // data := url.Values{} + // data.Set("name", "john") + // req = httptest.NewRequest("POST", "/test", strings.NewReader(data.Encode())) + // req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + // req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) + + // _, err = app.Test(req) + // if err != nil { + // t.Fatalf(`%s: %s`, t.Name(), err) + // } + + // req = httptest.NewRequest("POST", "/test", bytes.NewBuffer([]byte(`john`))) + // req.Header.Set("Content-Type", "application/xml") + // req.Header.Set("Content-Length", strconv.Itoa(len([]byte(`john`)))) + + // _, err = app.Test(req) + // if err != nil { + // t.Fatalf(`%s: %s`, t.Name(), err) + // } } func Test_Cookies(t *testing.T) { app := New() diff --git a/go.sum b/go.sum index 276b6cc95c..1c40fec1d9 100644 --- a/go.sum +++ b/go.sum @@ -7,12 +7,19 @@ github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGn github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs= github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= +github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc= +github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w= github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/utils.go b/utils.go index 40166c9dd0..7c23a46951 100644 --- a/utils.go +++ b/utils.go @@ -115,6 +115,26 @@ var getBytesImmutable = func(s string) (b []byte) { return []byte(s) } +// Check if -prefork is in arguments +func isPrefork() bool { + for _, v := range os.Args[1:] { + if v == "-prefork" { + return true + } + } + return false +} + +// Check if -child is in arguments +func isChild() bool { + for _, v := range os.Args[1:] { + if v == "-child" { + return true + } + } + return false +} + // https://golang.org/src/net/net.go#L113 // Helper methods for application#test type testConn struct {