Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Oct 30, 2024
1 parent 721187d commit b0b0359
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
4 changes: 1 addition & 3 deletions monitor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package monitor

import (
"time"

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

// Config defines the config for middleware.
Expand All @@ -26,7 +24,7 @@ type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
Next func(c Context) bool

// Custom HTML Code to Head Section(Before End)
//
Expand Down
18 changes: 18 additions & 0 deletions monitor/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package monitor

import "github.com/gofiber/fiber/v3"

// Context is an interface that abstracts the Fiber context.
type Context interface {
Method(override ...string) string
Get(key string, defaultValue ...string) string
// TODO: return self is a problem
Status(status int) fiber.Ctx
JSON(data any, ctype ...string) error
Next() error
Set(key, val string)
SendString(body string) error
}

// Handler is a function type that takes a Context and returns an error.
type Handler[T Context] func(T) error
6 changes: 3 additions & 3 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (
)

// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
func New[T Context](config ...Config) Handler[T] {
// Set default config
cfg := configDefault(config...)

Expand All @@ -74,9 +74,9 @@ func New(config ...Config) fiber.Handler {

// Return new handler
//nolint:errcheck // Ignore the type-assertion errors
return func(c fiber.Ctx) error {
return func(c T) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(&c) {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}

Expand Down
22 changes: 11 additions & 11 deletions monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Test_Monitor_405(t *testing.T) {

app := fiber.New()

app.Use("/", New())
app.Use("/", New[fiber.Ctx]())

resp, err := app.Test(httptest.NewRequest(fiber.MethodPost, "/", nil))
assert.Equal(t, nil, err)
Expand All @@ -32,7 +32,7 @@ func Test_Monitor_Html(t *testing.T) {
app := fiber.New()

// defaults
app.Get("/", New())
app.Get("/", New[fiber.Ctx]())
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))

assert.Equal(t, nil, err)
Expand All @@ -48,7 +48,7 @@ func Test_Monitor_Html(t *testing.T) {

// custom config
conf := Config{Title: "New " + defaultTitle, Refresh: defaultRefresh + time.Second}
app.Get("/custom", New(conf))
app.Get("/custom", New[fiber.Ctx](conf))
resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/custom", nil))

assert.Equal(t, nil, err)
Expand All @@ -69,7 +69,7 @@ func Test_Monitor_Html_CustomCodes(t *testing.T) {
app := fiber.New()

// defaults
app.Get("/", New())
app.Get("/", New[fiber.Ctx]())
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))

assert.Equal(t, nil, err)
Expand All @@ -91,7 +91,7 @@ func Test_Monitor_Html_CustomCodes(t *testing.T) {
FontURL: "/public/my-font.css",
CustomHead: `<style>body{background:#fff}</style>`,
}
app.Get("/custom", New(conf))
app.Get("/custom", New[fiber.Ctx](conf))
resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/custom", nil))

assert.Equal(t, nil, err)
Expand All @@ -116,7 +116,7 @@ func Test_Monitor_JSON(t *testing.T) {

app := fiber.New()

app.Get("/", New())
app.Get("/", New[fiber.Ctx]())

req := httptest.NewRequest(fiber.MethodGet, "/", nil)
req.Header.Set(fiber.HeaderAccept, fiber.MIMEApplicationJSON)
Expand All @@ -135,7 +135,7 @@ func Test_Monitor_JSON(t *testing.T) {
func Benchmark_Monitor(b *testing.B) {
app := fiber.New()

app.Get("/", New())
app.Get("/", New[fiber.Ctx]())

h := app.Handler()

Expand Down Expand Up @@ -165,8 +165,8 @@ func Test_Monitor_Next(t *testing.T) {

app := fiber.New()

app.Use("/", New(Config{
Next: func(_ *fiber.Ctx) bool {
app.Use("/", New[fiber.Ctx](Config{
Next: func(_ Context) bool {
return true
},
}))
Expand All @@ -180,7 +180,7 @@ func Test_Monitor_Next(t *testing.T) {
func Test_Monitor_APIOnly(t *testing.T) {
app := fiber.New()

app.Get("/", New(Config{
app.Get("/", New[fiber.Ctx](Config{
APIOnly: true,
}))

Expand All @@ -195,4 +195,4 @@ func Test_Monitor_APIOnly(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, true, bytes.Contains(b, []byte("pid")))
assert.Equal(t, true, bytes.Contains(b, []byte("os")))
}
}

0 comments on commit b0b0359

Please sign in to comment.