Skip to content

Commit

Permalink
✨ v3 (feature): convert fiber.Ctx type to interface (#1928)
Browse files Browse the repository at this point in the history
* ✨ v3: convert fiber.Ctx type to interface

* update ctx methods

* add new methods to customize ctx, fix some problems

* update comments.

* fix something
  • Loading branch information
efectn authored Jul 13, 2022
1 parent a2c71e5 commit a458bd3
Show file tree
Hide file tree
Showing 121 changed files with 1,911 additions and 1,437 deletions.
40 changes: 20 additions & 20 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()

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

Expand Down Expand Up @@ -161,31 +161,31 @@ func main() {
app := fiber.New()

// GET /api/register
app.Get("/api/*", func(c *fiber.Ctx) error {
app.Get("/api/*", func(c fiber.Ctx) error {
msg := fmt.Sprintf("%s", c.Params("*"))
return c.SendString(msg) // => ✋ register
})

// GET /flights/LAX-SFO
app.Get("/flights/:from-:to", func(c *fiber.Ctx) error {
app.Get("/flights/:from-:to", func(c fiber.Ctx) error {
msg := fmt.Sprintf("💸 From: %s, To: %s", c.Params("from"), c.Params("to"))
return c.SendString(msg) // => 💸 From: LAX, To: SFO
})

// GET /dictionary.txt
app.Get("/:file.:ext", func(c *fiber.Ctx) error {
app.Get("/:file.:ext", func(c fiber.Ctx) error {
msg := fmt.Sprintf("📃 %s.%s", c.Params("file"), c.Params("ext"))
return c.SendString(msg) // => 📃 dictionary.txt
})

// GET /john/75
app.Get("/:name/:age/:gender?", func(c *fiber.Ctx) error {
app.Get("/:name/:age/:gender?", func(c fiber.Ctx) error {
msg := fmt.Sprintf("👴 %s is %s years old", c.Params("name"), c.Params("age"))
return c.SendString(msg) // => 👴 john is 75 years old
})

// GET /john
app.Get("/:name", func(c *fiber.Ctx) error {
app.Get("/:name", func(c fiber.Ctx) error {
msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
return c.SendString(msg) // => Hello john 👋!
})
Expand All @@ -202,7 +202,7 @@ func main() {
app := fiber.New()

// GET /api/register
app.Get("/api/*", func(c *fiber.Ctx) error {
app.Get("/api/*", func(c fiber.Ctx) error {
msg := fmt.Sprintf("%s", c.Params("*"))
return c.SendString(msg) // => ✋ register
}).Name("api")
Expand Down Expand Up @@ -254,19 +254,19 @@ func main() {
app := fiber.New()

// Match any route
app.Use(func(c *fiber.Ctx) error {
app.Use(func(c fiber.Ctx) error {
fmt.Println("🥇 First handler")
return c.Next()
})

// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) error {
app.Use("/api", func(c fiber.Ctx) error {
fmt.Println("🥈 Second handler")
return c.Next()
})

// GET /api/list
app.Get("/api/list", func(c *fiber.Ctx) error {
app.Get("/api/list", func(c fiber.Ctx) error {
fmt.Println("🥉 Last handler")
return c.SendString("Hello, World 👋!")
})
Expand Down Expand Up @@ -306,7 +306,7 @@ func main() {
})

// And now, you can call template `./views/home.pug` like this:
app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
return c.Render("home", fiber.Map{
"title": "Homepage",
"year": 1999,
Expand All @@ -322,12 +322,12 @@ func main() {
📖 [Group](https://docs.gofiber.io/api/app#group)

```go
func middleware(c *fiber.Ctx) error {
func middleware(c fiber.Ctx) error {
fmt.Println("Don't mind me!")
return c.Next()
}

func handler(c *fiber.Ctx) error {
func handler(c fiber.Ctx) error {
return c.SendString(c.Path())
}

Expand Down Expand Up @@ -416,16 +416,16 @@ func main() {

app.Static("/", "./public")

app.Get("/demo", func(c *fiber.Ctx) error {
app.Get("/demo", func(c fiber.Ctx) error {
return c.SendString("This is a demo!")
})

app.Post("/register", func(c *fiber.Ctx) error {
app.Post("/register", func(c fiber.Ctx) error {
return c.SendString("Welcome!")
})

// Last middleware to match anything
app.Use(func(c *fiber.Ctx) error {
app.Use(func(c fiber.Ctx) error {
return c.SendStatus(404)
// => 404 "Not Found"
})
Expand All @@ -447,12 +447,12 @@ type User struct {
func main() {
app := fiber.New()

app.Get("/user", func(c *fiber.Ctx) error {
app.Get("/user", func(c fiber.Ctx) error {
return c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})

app.Get("/json", func(c *fiber.Ctx) error {
app.Get("/json", func(c fiber.Ctx) error {
return c.JSON(fiber.Map{
"success": true,
"message": "Hi John!",
Expand Down Expand Up @@ -511,7 +511,7 @@ import (
func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
app.Get("/sse", func(c fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
Expand Down Expand Up @@ -554,7 +554,7 @@ func main() {

app.Use(recover.New())

app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
panic("normally this would crash your app")
})

Expand Down
40 changes: 20 additions & 20 deletions .github/README_ckb.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()

app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("سڵاو، دنیا 👋!")
})

Expand Down Expand Up @@ -161,31 +161,31 @@ func main() {
app := fiber.New()

// GET /api/register
app.Get("/api/*", func(c *fiber.Ctx) error {
app.Get("/api/*", func(c fiber.Ctx) error {
msg := fmt.Sprintf("%s", c.Params("*"))
return c.SendString(msg) // => ✋ register
})

// GET /flights/LAX-SFO
app.Get("/flights/:from-:to", func(c *fiber.Ctx) error {
app.Get("/flights/:from-:to", func(c fiber.Ctx) error {
msg := fmt.Sprintf("💸 From: %s, To: %s", c.Params("from"), c.Params("to"))
return c.SendString(msg) // => 💸 From: LAX, To: SFO
})

// GET /dictionary.txt
app.Get("/:file.:ext", func(c *fiber.Ctx) error {
app.Get("/:file.:ext", func(c fiber.Ctx) error {
msg := fmt.Sprintf("📃 %s.%s", c.Params("file"), c.Params("ext"))
return c.SendString(msg) // => 📃 dictionary.txt
})

// GET /john/75
app.Get("/:name/:age/:gender?", func(c *fiber.Ctx) error {
app.Get("/:name/:age/:gender?", func(c fiber.Ctx) error {
msg := fmt.Sprintf("👴 %s is %s years old", c.Params("name"), c.Params("age"))
return c.SendString(msg) // => 👴 john is 75 years old
})

// GET /john
app.Get("/:name", func(c *fiber.Ctx) error {
app.Get("/:name", func(c fiber.Ctx) error {
msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
return c.SendString(msg) // => Hello john 👋!
})
Expand All @@ -202,7 +202,7 @@ func main() {
app := fiber.New()

// GET /api/register
app.Get("/api/*", func(c *fiber.Ctx) error {
app.Get("/api/*", func(c fiber.Ctx) error {
msg := fmt.Sprintf("%s", c.Params("*"))
return c.SendString(msg) // => ✋ register
}).Name("api")
Expand Down Expand Up @@ -254,19 +254,19 @@ func main() {
app := fiber.New()

// Match any route
app.Use(func(c *fiber.Ctx) error {
app.Use(func(c fiber.Ctx) error {
fmt.Println("🥇 First handler")
return c.Next()
})

// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) error {
app.Use("/api", func(c fiber.Ctx) error {
fmt.Println("🥈 Second handler")
return c.Next()
})

// GET /api/list
app.Get("/api/list", func(c *fiber.Ctx) error {
app.Get("/api/list", func(c fiber.Ctx) error {
fmt.Println("🥉 Last handler")
return c.SendString("Hello, World 👋!")
})
Expand Down Expand Up @@ -306,7 +306,7 @@ func main() {
})

// And now, you can call template `./views/home.pug` like this:
app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
return c.Render("home", fiber.Map{
"title": "Homepage",
"year": 1999,
Expand All @@ -322,12 +322,12 @@ func main() {
📖 [Group](https://docs.gofiber.io/api/app#group)

```go
func middleware(c *fiber.Ctx) error {
func middleware(c fiber.Ctx) error {
fmt.Println("Don't mind me!")
return c.Next()
}

func handler(c *fiber.Ctx) error {
func handler(c fiber.Ctx) error {
return c.SendString(c.Path())
}

Expand Down Expand Up @@ -416,16 +416,16 @@ func main() {

app.Static("/", "./public")

app.Get("/demo", func(c *fiber.Ctx) error {
app.Get("/demo", func(c fiber.Ctx) error {
return c.SendString("This is a demo!")
})

app.Post("/register", func(c *fiber.Ctx) error {
app.Post("/register", func(c fiber.Ctx) error {
return c.SendString("Welcome!")
})

// Last middleware to match anything
app.Use(func(c *fiber.Ctx) error {
app.Use(func(c fiber.Ctx) error {
return c.SendStatus(404)
// => 404 "Not Found"
})
Expand All @@ -447,12 +447,12 @@ type User struct {
func main() {
app := fiber.New()

app.Get("/user", func(c *fiber.Ctx) error {
app.Get("/user", func(c fiber.Ctx) error {
return c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})

app.Get("/json", func(c *fiber.Ctx) error {
app.Get("/json", func(c fiber.Ctx) error {
return c.JSON(fiber.Map{
"success": true,
"message": "Hi John!",
Expand Down Expand Up @@ -511,7 +511,7 @@ import (
func main() {
app := fiber.New()

app.Get("/sse", func(c *fiber.Ctx) error {
app.Get("/sse", func(c fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
Expand Down Expand Up @@ -554,7 +554,7 @@ func main() {

app.Use(recover.New())

app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
panic("normally this would crash your app")
})

Expand Down
Loading

1 comment on commit a458bd3

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: a458bd3 Previous: 2a2337d Ratio
Benchmark_Cache 13399 ns/op 49367 B/op 6 allocs/op 366.3 ns/op 16 B/op 2 allocs/op 36.58
Benchmark_Cache_AdditionalHeaders 1187 ns/op 592 B/op 9 allocs/op 445.1 ns/op 16 B/op 2 allocs/op 2.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.