Skip to content

Commit

Permalink
✏️ feature: useCDN option for monitor middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Jun 28, 2022
1 parent 43aa457 commit 3f3aa77
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
8 changes: 6 additions & 2 deletions middleware/monitor/README.md
@@ -1,8 +1,6 @@
# Monitor
Monitor middleware for [Fiber](https://github.com/gofiber/fiber) that reports server metrics, inspired by [express-status-monitor](https://github.com/RafalWilinski/express-status-monitor)

:warning: **Warning:** Monitor is still in beta, API might change in the future!

![](https://i.imgur.com/nHAtBpJ.gif)

### Signatures
Expand Down Expand Up @@ -58,6 +56,12 @@ type Config struct {
// Optional. Default: false
APIOnly bool

// UseCDN is an option to get Chart.js from jsdelivr cdn.
// If you dont want to use cdn, Chart.js will be served from another route.
//
// Optional. Default: false
UseCDN bool

// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Expand Down
10 changes: 10 additions & 0 deletions middleware/monitor/chart.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions middleware/monitor/config.go
Expand Up @@ -23,6 +23,12 @@ type Config struct {
// Optional. Default: false
APIOnly bool

// UseCDN is an option to get Chart.js from jsdelivr cdn.
// If you dont want to use cdn, Chart.js will be served from another route.
//
// Optional. Default: false
UseCDN bool

// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Expand Down
17 changes: 16 additions & 1 deletion middleware/monitor/monitor.go
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"os"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -81,6 +82,12 @@ func New(config ...Config) fiber.Handler {
if c.Method() != fiber.MethodGet {
return fiber.ErrMethodNotAllowed
}

if c.Get("Sec-Fetch-Dest") == "script" {
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJavaScriptCharsetUTF8)
return c.Status(fiber.StatusOK).SendString(chartJS)
}

if c.Get(fiber.HeaderAccept) == fiber.MIMEApplicationJSON || cfg.APIOnly {
mutex.Lock()
data.PID.CPU = monitPidCpu.Load().(float64)
Expand All @@ -95,8 +102,16 @@ func New(config ...Config) fiber.Handler {
mutex.Unlock()
return c.Status(fiber.StatusOK).JSON(data)
}

c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8)
return c.Status(fiber.StatusOK).SendString(cfg.index)

// Return index by check UseCDN config field.
if cfg.UseCDN {
return c.Status(fiber.StatusOK).SendString(cfg.index)
}

index := strings.ReplaceAll(cfg.index, "https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js", c.OriginalURL())
return c.Status(fiber.StatusOK).SendString(index)
}
}

Expand Down
44 changes: 43 additions & 1 deletion middleware/monitor/monitor_test.go
Expand Up @@ -130,7 +130,7 @@ func Test_Monitor_Next(t *testing.T) {

// go test -run Test_Monitor_APIOnly -race
func Test_Monitor_APIOnly(t *testing.T) {
//t.Parallel()
t.Parallel()

app := fiber.New()

Expand All @@ -150,3 +150,45 @@ func Test_Monitor_APIOnly(t *testing.T) {
utils.AssertEqual(t, true, bytes.Contains(b, []byte("pid")))
utils.AssertEqual(t, true, bytes.Contains(b, []byte("os")))
}

// go test -run Test_Monitor_UseCDN -race
func Test_Monitor_UseCDN(t *testing.T) {
t.Parallel()

app := fiber.New()

app.Get("/", New(Config{
UseCDN: true,
}))

req := httptest.NewRequest(fiber.MethodGet, "/", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
utils.AssertEqual(t, fiber.MIMETextHTMLCharsetUTF8, resp.Header.Get(fiber.HeaderContentType))

b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, bytes.Contains(b, []byte(`<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js"></script>`)))
}

// go test -run Test_Monitor_Script -race
func Test_Monitor_Script(t *testing.T) {
t.Parallel()

app := fiber.New()

app.Get("/", New())

req := httptest.NewRequest(fiber.MethodGet, "/", nil)
req.Header.Set("Sec-Fetch-Dest", "script")
resp, err := app.Test(req)
fmt.Print(resp.Header.Get("Sec-Fetch-Dest"))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
utils.AssertEqual(t, "script", resp.Request.Header.Get("Sec-Fetch-Dest"))

b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, bytes.Contains(b, []byte(`Chart.js v2.9.0`)))
}

0 comments on commit 3f3aa77

Please sign in to comment.