Skip to content

Commit

Permalink
feat: adding filters
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Sep 15, 2023
1 parent 54e63a4 commit 47de0db
Show file tree
Hide file tree
Showing 3 changed files with 386 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ app.Listen(":4242")
// time=2023-04-10T14:00:00.000+00:00 level=INFO msg="Incoming request" status=200 method=GET path=/ ip=::1 latency=25.958µs user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000+00:00 request-id=229c7fc8-64f5-4467-bc4a-940700503b0d
```

### Filters

```go
import (
"github.com/gofiber/fiber/v2"
slogfiber "github.com/samber/slog-fiber"
"log/slog"
)

// Create a slog logger, which:
// - Logs to stdout.
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

app := fiber.New()
app.Use(
slogfiber.NewWithFilters(
logger,
slogfiber.Accept(func (c *fiber.Ctx) bool {
return xxx
}),
slogfiber.IgnoreStatus(401, 404),
),
)
```

Available filters:
- Accept / Ignore
- AcceptMethod / IgnoreMethod
- AcceptStatus / IgnoreStatus
- AcceptStatusGreaterThan / IgnoreStatusLessThan
- AcceptStatusGreaterThanOrEqual / IgnoreStatusLessThanOrEqual
- AcceptPath / IgnorePath
- AcceptPathContains / IgnorePathContains
- AcceptPathPrefix / IgnorePathPrefix
- AcceptPathSuffix / IgnorePathSuffix
- AcceptPathMatch / IgnorePathMatch
- AcceptHost / IgnoreHost
- AcceptHostContains / IgnoreHostContains
- AcceptHostPrefix / IgnoreHostPrefix
- AcceptHostSuffix / IgnoreHostSuffix
- AcceptHostMatch / IgnoreHostMatch

### Using custom time formatters

```go
Expand Down
334 changes: 334 additions & 0 deletions filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
package slogfiber

import (
"regexp"
"strings"

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

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 {
reqMethod := strings.ToLower(string(c.Context().Method()))

for _, method := range methods {
if strings.ToLower(method) == reqMethod {
return true
}
}

return false
}
}

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

for _, method := range methods {
if strings.ToLower(method) == reqMethod {
return false
}
}

return true
}
}

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

return false
}
}

func IgnoreStatus(statuses ...int) Filter {
return func(c *fiber.Ctx) bool {
for _, status := range statuses {
if status == c.Response().StatusCode() {
return false
}
}

return true
}
}

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

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

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

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

// Path
func AcceptPath(urls ...string) Filter {
return func(c *fiber.Ctx) bool {
for _, url := range urls {
if c.Path() == url {
return true
}
}

return false
}
}

func IgnorePath(urls ...string) Filter {
return func(c *fiber.Ctx) bool {
for _, url := range urls {
if c.Path() == url {
return false
}
}

return true
}
}

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

return false
}
}

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

return true
}
}

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

return false
}
}

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

return true
}
}

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

return false
}
}

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

return true
}
}

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

return false
}
}

func IgnorePathMatch(regs ...regexp.Regexp) Filter {
return func(c *fiber.Ctx) bool {
for _, reg := range regs {
if reg.Match([]byte(c.Path())) {
return false
}
}

return true
}
}

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

return false
}
}

func IgnoreHost(hosts ...string) Filter {
return func(c *fiber.Ctx) bool {
for _, host := range hosts {
if c.Hostname() == host {
return false
}
}

return true
}
}

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

return false
}
}

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

return true
}
}

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

return false
}
}

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

return true
}
}

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

return false
}
}

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

return true
}
}

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

return false
}
}

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

return true
}
}
Loading

0 comments on commit 47de0db

Please sign in to comment.