Skip to content

Commit

Permalink
feat: Support Fiber v3 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
simman authored and samber committed Apr 21, 2024
1 parent cc202f5 commit 463cdf1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 79 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ slogfiber.RequestIDHeaderKey = "X-Request-Id"

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/recover"
slogfiber "github.com/samber/slog-fiber"
"log/slog"
)
Expand All @@ -123,7 +123,7 @@ app := fiber.New()
app.Use(slogfiber.New(logger))
app.Use(recover.New())

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

Expand Down Expand Up @@ -187,7 +187,7 @@ app := fiber.New()
app.Use(
slogfiber.NewWithFilters(
logger,
slogfiber.Accept(func (c *fiber.Ctx) bool {
slogfiber.Accept(func (c fiber.Ctx) bool {
return xxx
}),
slogfiber.IgnoreStatus(401, 404),
Expand Down Expand Up @@ -233,7 +233,7 @@ app := fiber.New()
app.Use(slogfiber.New(logger))
app.Use(recover.New())

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

Expand All @@ -253,7 +253,7 @@ app := fiber.New()
app.Use(slogfiber.New(logger.WithGroup("http")))
app.Use(recover.New())

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

Expand All @@ -272,7 +272,7 @@ app := fiber.New()

app.Use(recover.New())

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

Expand All @@ -292,7 +292,7 @@ app := fiber.New()
app.Use(slogfiber.New(logger))
app.Use(recover.New())

app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
// Add an attribute to a single log entry.
slogfiber.AddCustomAttributes(c, slog.String("foo", "bar"))
return c.SendString("Hello, World 👋!")
Expand All @@ -314,7 +314,7 @@ app := fiber.New()
app.Use(slogfiber.New(logger))
app.Use(recover.New())

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

Expand Down
18 changes: 9 additions & 9 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"log/slog"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/recover"
slogfiber "github.com/samber/slog-fiber"
slogformatter "github.com/samber/slog-formatter"
)
Expand Down Expand Up @@ -36,31 +36,31 @@ func main() {
// app.Use(slogfiber.NewWithConfig(logger, config))
app.Use(recover.New())

app.Get("/", func(c *fiber.Ctx) error {
app.Get("/", func(c fiber.Ctx) error {
slogfiber.AddCustomAttributes(c, slog.String("foo", "bar"))
return c.SendString("Hello, World 👋!")
})

app.Get("/crashme", func(c *fiber.Ctx) error {
app.Get("/crashme", func(c fiber.Ctx) error {
return c.Status(400).SendString("Oops i crashed :(")
})

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

app.Post("/bad", func(c *fiber.Ctx) error {
app.Post("/bad", func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusBadRequest)
})
app.Get("/die", func(c *fiber.Ctx) error {
app.Get("/die", func(c fiber.Ctx) error {
panic("killed")
})
app.Post("/force", func(c *fiber.Ctx) error {
app.Post("/force", func(c fiber.Ctx) error {
return fiber.NewError(fiber.StatusUnauthorized)
})

// 404 Handler
app.Use(func(c *fiber.Ctx) error {
app.Use(func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusNotFound)
})

Expand Down
60 changes: 30 additions & 30 deletions filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"regexp"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3"
)

type Filter func(ctx *fiber.Ctx) bool
type Filter func(ctx fiber.Ctx) bool

// Basic
func Accept(filter Filter) Filter { return filter }
func Ignore(filter Filter) Filter { return filter }

// Method
func AcceptMethod(methods ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
reqMethod := strings.ToLower(string(c.Context().Method()))

for _, method := range methods {
Expand All @@ -29,7 +29,7 @@ func AcceptMethod(methods ...string) Filter {
}

func IgnoreMethod(methods ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
reqMethod := strings.ToLower(string(c.Context().Method()))

for _, method := range methods {
Expand All @@ -44,7 +44,7 @@ func IgnoreMethod(methods ...string) Filter {

// Status
func AcceptStatus(statuses ...int) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, status := range statuses {
if status == c.Response().StatusCode() {
return true
Expand All @@ -56,7 +56,7 @@ func AcceptStatus(statuses ...int) Filter {
}

func IgnoreStatus(statuses ...int) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, status := range statuses {
if status == c.Response().StatusCode() {
return false
Expand All @@ -68,32 +68,32 @@ func IgnoreStatus(statuses ...int) Filter {
}

func AcceptStatusGreaterThan(status int) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
return c.Response().StatusCode() > status
}
}

func IgnoreStatusLessThan(status int) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
return c.Response().StatusCode() < status
}
}

func AcceptStatusGreaterThanOrEqual(status int) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
return c.Response().StatusCode() >= status
}
}

func IgnoreStatusLessThanOrEqual(status int) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
return c.Response().StatusCode() <= status
}
}

// Path
func AcceptPath(urls ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, url := range urls {
if c.Path() == url {
return true
Expand All @@ -105,7 +105,7 @@ func AcceptPath(urls ...string) Filter {
}

func IgnorePath(urls ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, url := range urls {
if c.Path() == url {
return false
Expand All @@ -117,7 +117,7 @@ func IgnorePath(urls ...string) Filter {
}

func AcceptPathContains(parts ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, part := range parts {
if strings.Contains(c.Path(), part) {
return true
Expand All @@ -129,7 +129,7 @@ func AcceptPathContains(parts ...string) Filter {
}

func IgnorePathContains(parts ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, part := range parts {
if strings.Contains(c.Path(), part) {
return false
Expand All @@ -141,7 +141,7 @@ func IgnorePathContains(parts ...string) Filter {
}

func AcceptPathPrefix(prefixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(c.Path(), prefix) {
return true
Expand All @@ -153,7 +153,7 @@ func AcceptPathPrefix(prefixs ...string) Filter {
}

func IgnorePathPrefix(prefixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(c.Path(), prefix) {
return false
Expand All @@ -165,7 +165,7 @@ func IgnorePathPrefix(prefixs ...string) Filter {
}

func AcceptPathSuffix(prefixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(c.Path(), prefix) {
return true
Expand All @@ -177,7 +177,7 @@ func AcceptPathSuffix(prefixs ...string) Filter {
}

func IgnorePathSuffix(suffixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, suffix := range suffixs {
if strings.HasSuffix(c.Path(), suffix) {
return false
Expand All @@ -189,7 +189,7 @@ func IgnorePathSuffix(suffixs ...string) Filter {
}

func AcceptPathMatch(regs ...regexp.Regexp) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, reg := range regs {
if reg.Match([]byte(c.Path())) {
return true
Expand All @@ -201,7 +201,7 @@ func AcceptPathMatch(regs ...regexp.Regexp) Filter {
}

func IgnorePathMatch(regs ...regexp.Regexp) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, reg := range regs {
if reg.Match([]byte(c.Path())) {
return false
Expand All @@ -214,7 +214,7 @@ func IgnorePathMatch(regs ...regexp.Regexp) Filter {

// Host
func AcceptHost(hosts ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, host := range hosts {
if c.Hostname() == host {
return true
Expand All @@ -226,7 +226,7 @@ func AcceptHost(hosts ...string) Filter {
}

func IgnoreHost(hosts ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, host := range hosts {
if c.Hostname() == host {
return false
Expand All @@ -238,7 +238,7 @@ func IgnoreHost(hosts ...string) Filter {
}

func AcceptHostContains(parts ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, part := range parts {
if strings.Contains(c.Hostname(), part) {
return true
Expand All @@ -250,7 +250,7 @@ func AcceptHostContains(parts ...string) Filter {
}

func IgnoreHostContains(parts ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, part := range parts {
if strings.Contains(c.Hostname(), part) {
return false
Expand All @@ -262,7 +262,7 @@ func IgnoreHostContains(parts ...string) Filter {
}

func AcceptHostPrefix(prefixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(c.Hostname(), prefix) {
return true
Expand All @@ -274,7 +274,7 @@ func AcceptHostPrefix(prefixs ...string) Filter {
}

func IgnoreHostPrefix(prefixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(c.Hostname(), prefix) {
return false
Expand All @@ -286,7 +286,7 @@ func IgnoreHostPrefix(prefixs ...string) Filter {
}

func AcceptHostSuffix(prefixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(c.Hostname(), prefix) {
return true
Expand All @@ -298,7 +298,7 @@ func AcceptHostSuffix(prefixs ...string) Filter {
}

func IgnoreHostSuffix(suffixs ...string) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, suffix := range suffixs {
if strings.HasSuffix(c.Hostname(), suffix) {
return false
Expand All @@ -310,7 +310,7 @@ func IgnoreHostSuffix(suffixs ...string) Filter {
}

func AcceptHostMatch(regs ...regexp.Regexp) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, reg := range regs {
if reg.Match([]byte(c.Hostname())) {
return true
Expand All @@ -322,7 +322,7 @@ func AcceptHostMatch(regs ...regexp.Regexp) Filter {
}

func IgnoreHostMatch(regs ...regexp.Regexp) Filter {
return func(c *fiber.Ctx) bool {
return func(c fiber.Ctx) bool {
for _, reg := range regs {
if reg.Match([]byte(c.Hostname())) {
return false
Expand Down
Loading

0 comments on commit 463cdf1

Please sign in to comment.