Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Jul 6, 2024
1 parent 2270ee3 commit c92a9af
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 103 deletions.
4 changes: 1 addition & 3 deletions docs/guide/faster-fiber.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ sidebar_position: 7

## Custom JSON Encoder/Decoder

Since Fiber v2.32.0, we use **encoding/json** as default json library due to stability and producibility. However, the standard library is a bit slow compared to 3rd party libraries. If you're not happy with the performance of **encoding/json**, we recommend you to use these libraries:
Since Fiber v2.32.0, we have adopted `encoding/json` as the default JSON library for its stability and reliability. However, the standard library can be slower than some third-party alternatives. If you find the performance of `encoding/json` unsatisfactory, we suggest considering these libraries:

- [goccy/go-json](https://github.com/goccy/go-json)
- [bytedance/sonic](https://github.com/bytedance/sonic)
- [segmentio/encoding](https://github.com/segmentio/encoding)
- [mailru/easyjson](https://github.com/mailru/easyjson)
- [minio/simdjson-go](https://github.com/minio/simdjson-go)
- [wI2L/jettison](https://github.com/wI2L/jettison)

```go title="Example"
package main
Expand Down
38 changes: 19 additions & 19 deletions docs/guide/grouping.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,43 @@ In general, the Group functionality in Fiber behaves similarly to ExpressJS. Gro

## Paths

Like **Routing**, groups can also have paths that belong to a cluster.
Like `Routing`, groups can also have paths that belong to a cluster.

```go
func main() {
app := fiber.New()
app := fiber.New()

api := app.Group("/api", middleware) // /api
api := app.Group("/api", middleware) // /api

v1 := api.Group("/v1", middleware) // /api/v1
v1.Get("/list", handler) // /api/v1/list
v1.Get("/user", handler) // /api/v1/user
v1 := api.Group("/v1", middleware) // /api/v1
v1.Get("/list", handler) // /api/v1/list
v1.Get("/user", handler) // /api/v1/user

v2 := api.Group("/v2", middleware) // /api/v2
v2.Get("/list", handler) // /api/v2/list
v2.Get("/user", handler) // /api/v2/user
v2 := api.Group("/v2", middleware) // /api/v2
v2.Get("/list", handler) // /api/v2/list
v2.Get("/user", handler) // /api/v2/user

log.Fatal(app.Listen(":3000"))
log.Fatal(app.Listen(":3000"))
}
```

A **Group** of paths can have an optional handler.

```go
func main() {
app := fiber.New()
app := fiber.New()

api := app.Group("/api") // /api
api := app.Group("/api") // /api

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

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

log.Fatal(app.Listen(":3000"))
log.Fatal(app.Listen(":3000"))
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Route paths, combined with a request method, define the endpoints at which reque
```go
// This route path will match requests to the root route, "/":
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("root")
return c.SendString("root")
})

// This route path will match requests to "/about":
Expand Down Expand Up @@ -315,7 +315,7 @@ app.Use(func(c fiber.Ctx) error {
})

app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
return c.SendString("Hello, World!")
})
```

Expand Down
58 changes: 29 additions & 29 deletions docs/guide/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func Convert[T any](value string, convertor func(string) (T, error), defaultValu
```go title="Example"
// GET http://example.com/id/bb70ab33-d455-4a03-8d78-d3c1dacae9ff
app.Get("/id/:id", func(c fiber.Ctx) error {
fiber.Convert(c.Params("id"), uuid.Parse) // UUID(bb70ab33-d455-4a03-8d78-d3c1dacae9ff), nil
fiber.Convert(c.Params("id"), uuid.Parse) // UUID(bb70ab33-d455-4a03-8d78-d3c1dacae9ff), nil


// GET http://example.com/search?id=65f6f54221fb90e6a6b76db7
app.Get("/search", func(c fiber.Ctx) error) {
fiber.Convert(c.Query("id"), mongo.ParseObjectID) // objectid(65f6f54221fb90e6a6b76db7), nil
fiber.Convert(c.Query("id"), uuid.Parse) // uuid.Nil, error(cannot parse given uuid)
fiber.Convert(c.Query("id"), uuid.Parse, mongo.NewObjectID) // new object id generated and return nil as error.
fiber.Convert(c.Query("id"), mongo.ParseObjectID) // objectid(65f6f54221fb90e6a6b76db7), nil
fiber.Convert(c.Query("id"), uuid.Parse) // uuid.Nil, error(cannot parse given uuid)
fiber.Convert(c.Query("id"), uuid.Parse, mongo.NewObjectID) // new object id generated and return nil as error.
}

// ...
Expand All @@ -44,11 +44,11 @@ func GetReqHeader[V any](c Ctx, key string, defaultValue ...V) V

```go title="Example"
app.Get("/search", func(c fiber.Ctx) error {
// curl -X GET http://example.com/search -H "X-Request-ID: 12345" -H "X-Request-Name: John"
GetReqHeader[int](c, "X-Request-ID") // => returns 12345 as integer.
GetReqHeader[string](c, "X-Request-Name") // => returns "John" as string.
GetReqHeader[string](c, "unknownParam", "default") // => returns "default" as string.
// ...
// curl -X GET http://example.com/search -H "X-Request-ID: 12345" -H "X-Request-Name: John"
GetReqHeader[int](c, "X-Request-ID") // => returns 12345 as integer.
GetReqHeader[string](c, "X-Request-Name") // => returns "John" as string.
GetReqHeader[string](c, "unknownParam", "default") // => returns "default" as string.
// ...
})
```

Expand All @@ -68,20 +68,20 @@ func Locals[V any](c Ctx, key any, value ...V) V

```go title="Example"
app.Use("/user/:user/:id", func(c fiber.Ctx) error {
// set local values
fiber.Locals[string](c, "user", "john")
fiber.Locals[int](c, "id", 25)
// ...

return c.Next()
// set local values
fiber.Locals[string](c, "user", "john")
fiber.Locals[int](c, "id", 25)
// ...
return c.Next()
})


app.Get("/user/*", func(c fiber.Ctx) error {
// get local values
name := fiber.Locals[string](c, "user") // john
age := fiber.Locals[int](c, "id") // 25
// ...
// get local values
name := fiber.Locals[string](c, "user") // john
age := fiber.Locals[int](c, "id") // 25
// ...
})
```

Expand All @@ -96,11 +96,11 @@ func Params[V any](c Ctx, key string, defaultValue ...V) V

```go title="Example"
app.Get("/user/:user/:id", func(c fiber.Ctx) error {
// http://example.com/user/john/25
Params[int](c, "id") // => returns 25 as integer.
Params[int](c, "unknownParam", 99) // => returns the default 99 as integer.
// ...
return c.SendString("Hello, " + fiber.Params[string](c, "user"))
// http://example.com/user/john/25
Params[int](c, "id") // => returns 25 as integer.
Params[int](c, "unknownParam", 99) // => returns the default 99 as integer.
// ...
return c.SendString("Hello, " + fiber.Params[string](c, "user"))
})
```

Expand All @@ -115,10 +115,10 @@ func Query[V any](c Ctx, key string, defaultValue ...V) V

```go title="Example"
app.Get("/search", func(c fiber.Ctx) error {
// http://example.com/search?name=john&age=25
Query[string](c, "name") // => returns "john"
Query[int](c, "age") // => returns 25 as integer.
Query[string](c, "unknownParam", "default") // => returns "default" as string.
// ...
// http://example.com/search?name=john&age=25
Query[string](c, "name") // => returns "john"
Query[int](c, "age") // => returns 25 as integer.
Query[string](c, "unknownParam", "default") // => returns "default" as string.
// ...
})
```
20 changes: 10 additions & 10 deletions docs/guide/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ app := fiber.New(fiber.Config{
})

type User struct {
Name string `json:"name" form:"name" query:"name" validate:"required"`
Age int `json:"age" form:"age" query:"age" validate:"gte=0,lte=100"`
Name string `json:"name" form:"name" query:"name" validate:"required"`
Age int `json:"age" form:"age" query:"age" validate:"gte=0,lte=100"`
}

app.Post("/", func(c fiber.Ctx) error {
user := new(User)

// Works with all bind methods - Body, Query, Form, ...
if err := c.Bind().Body(user); err != nil { // <- here you receive the validation errors
return err
}

return c.JSON(user)
user := new(User)
// Works with all bind methods - Body, Query, Form, ...
if err := c.Bind().Body(user); err != nil { // <- here you receive the validation errors
return err
}
return c.JSON(user)
})
```
8 changes: 4 additions & 4 deletions docs/middleware/compress.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ app.Use(compress.New(compress.Config{

// Skip middleware for specific routes
app.Use(compress.New(compress.Config{
Next: func(c fiber.Ctx) bool {
return c.Path() == "/dont_compress"
},
Level: compress.LevelBestSpeed, // 1
Next: func(c fiber.Ctx) bool {
return c.Path() == "/dont_compress"
},
Level: compress.LevelBestSpeed, // 1
}))
```

Expand Down
28 changes: 14 additions & 14 deletions docs/middleware/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,25 @@ If you need to allow wildcard origins, use `AllowOrigins` with a wildcard `"*"`
```go
// dbCheckOrigin checks if the origin is in the list of allowed origins in the database.
func dbCheckOrigin(db *sql.DB, origin string) bool {
// Placeholder query - adjust according to your database schema and query needs
query := "SELECT COUNT(*) FROM allowed_origins WHERE origin = $1"

var count int
err := db.QueryRow(query, origin).Scan(&count)
if err != nil {
// Handle error (e.g., log it); for simplicity, we return false here
return false
}

return count > 0
// Placeholder query - adjust according to your database schema and query needs
query := "SELECT COUNT(*) FROM allowed_origins WHERE origin = $1"
var count int
err := db.QueryRow(query, origin).Scan(&count)
if err != nil {
// Handle error (e.g., log it); for simplicity, we return false here
return false
}
return count > 0
}

// ...

app.Use(cors.New(cors.Config{
AllowOriginsFunc: func(origin string) bool {
return dbCheckOrigin(db, origin)
},
AllowOriginsFunc: func(origin string) bool {
return dbCheckOrigin(db, origin)
},
}))
```

Expand Down
34 changes: 17 additions & 17 deletions docs/middleware/rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ import (
)

func main() {
app := fiber.New()

app.Use(rewrite.New(rewrite.Config{
Rules: map[string]string{
"/old": "/new",
"/old/*": "/new/$1",
},
}))

app.Get("/new", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Get("/new/*", func(c fiber.Ctx) error {
return c.SendString("Wildcard: " + c.Params("*"))
})

app.Listen(":3000")
app := fiber.New()
app.Use(rewrite.New(rewrite.Config{
Rules: map[string]string{
"/old": "/new",
"/old/*": "/new/$1",
},
}))
app.Get("/new", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Get("/new/*", func(c fiber.Ctx) error {
return c.SendString("Wildcard: " + c.Params("*"))
})
app.Listen(":3000")
}

```
Expand Down
1 change: 1 addition & 0 deletions docs/middleware/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ You can use any storage from our [storage](https://github.com/gofiber/storage/)

```go
storage := sqlite3.New() // From github.com/gofiber/storage/sqlite3

store := session.New(session.Config{
Storage: storage,
})
Expand Down
4 changes: 2 additions & 2 deletions docs/middleware/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ You can set `CacheDuration` config property to `-1` to disable caching.
```go
var ConfigDefault = Config{
Index: []string{"index.html"},
CacheDuration: 10 * time.Second,
Index: []string{"index.html"},
CacheDuration: 10 * time.Second,
}
```
6 changes: 3 additions & 3 deletions docs/partials/routing/handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func (app *App) All(path string, handler Handler, middlewares ...Handler) Router
```go title="Examples"
// Simple GET handler
app.Get("/api/list", func(c fiber.Ctx) error {
return c.SendString("I'm a GET request!")
return c.SendString("I'm a GET request!")
})

// Simple POST handler
app.Post("/api/register", func(c fiber.Ctx) error {
return c.SendString("I'm a POST request!")
return c.SendString("I'm a POST request!")
})
```

Expand Down Expand Up @@ -71,7 +71,7 @@ app.Use([]string{"/api", "/home"}, func(c fiber.Ctx) error {

// Attach multiple handlers
app.Use("/api", func(c fiber.Ctx) error {
c.Set("X-Custom-Header", random.String(32))
c.Set("X-Custom-Header", random.String(32))
return c.Next()
}, func(c fiber.Ctx) error {
return c.Next()
Expand Down

0 comments on commit c92a9af

Please sign in to comment.