From d4cb49b02d0f0e67c42f5ef94dfaad28d35d3bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serhat=20=C5=9Eevki=20Din=C3=A7er?= Date: Sat, 7 May 2022 03:46:06 +0300 Subject: [PATCH] :bar_chart: add refresh period to monitor mw --- middleware/monitor/README.md | 6 +++ middleware/monitor/config.go | 52 +++++++++++--------- middleware/monitor/index.go | 76 +++++++++++++++--------------- middleware/monitor/monitor.go | 4 +- middleware/monitor/monitor_test.go | 30 ++++++++++-- 5 files changed, 104 insertions(+), 64 deletions(-) diff --git a/middleware/monitor/README.md b/middleware/monitor/README.md index 671e3c7352..8ac23e4211 100644 --- a/middleware/monitor/README.md +++ b/middleware/monitor/README.md @@ -48,6 +48,11 @@ type Config struct { // Optional. Default: "Fiber Monitor" Title string + // Refresh period + // + // Optional. Default: 3 seconds + Refresh time.Duration + // To disable serving HTML, you can make true this option. // // Optional. Default: false @@ -65,6 +70,7 @@ type Config struct { ```go var ConfigDefault = Config{ Title: "Fiber Monitor", + Refresh: 3 * time.Second, APIOnly: false, Next: nil, } diff --git a/middleware/monitor/config.go b/middleware/monitor/config.go index b98f73d4e7..dbd3186835 100644 --- a/middleware/monitor/config.go +++ b/middleware/monitor/config.go @@ -1,7 +1,7 @@ package monitor import ( - "strings" + "time" "github.com/gofiber/fiber/v2" ) @@ -13,6 +13,11 @@ type Config struct { // Optional. Default: "Fiber Monitor" Title string + // Refresh period + // + // Optional. Default: 3 seconds + Refresh time.Duration + // Whether the service should expose only the monitoring API. // // Optional. Default: false @@ -23,33 +28,28 @@ type Config struct { // Optional. Default: nil Next func(c *fiber.Ctx) bool - // defaultTitle substituted with Title in indexHtml + // customized indexHtml index string } -var prevTitle = defaultTitle - var ConfigDefault = Config{ Title: defaultTitle, + Refresh: defaultRefresh, APIOnly: false, Next: nil, - index: indexHtml, -} - -func newIndex(title string) string { - if title == defaultTitle { - return indexHtml - } - return strings.ReplaceAll(indexHtml, defaultTitle, title) + index: newIndex(defaultTitle, defaultRefresh), } func configDefault(config ...Config) Config { - // Users can change ConfigDefault.Title which then - // becomes incompatible with ConfigDefault.index - if prevTitle != ConfigDefault.Title { - prevTitle = ConfigDefault.Title - // update default index with new default title - ConfigDefault.index = newIndex(prevTitle) + // Users can change ConfigDefault.Title/Refresh which then + // become incompatible with ConfigDefault.index + if ConfigDefault.Title != defaultTitle || ConfigDefault.Refresh != defaultRefresh { + + if ConfigDefault.Refresh < minRefresh { + ConfigDefault.Refresh = minRefresh + } + // update default index with new default title/refresh + ConfigDefault.index = newIndex(ConfigDefault.Title, ConfigDefault.Refresh) } // Return default config if nothing provided @@ -61,12 +61,22 @@ func configDefault(config ...Config) Config { cfg := config[0] // Set default values - if cfg.Title == "" || cfg.Title == ConfigDefault.Title { + if cfg.Title == "" { cfg.Title = ConfigDefault.Title + } + + if cfg.Refresh == 0 { + cfg.Refresh = ConfigDefault.Refresh + } + + if cfg.Title == ConfigDefault.Title && cfg.Refresh == ConfigDefault.Refresh { cfg.index = ConfigDefault.index } else { - // update cfg.index with new title - cfg.index = newIndex(cfg.Title) + if cfg.Refresh < minRefresh { + cfg.Refresh = minRefresh + } + // update cfg.index with custom title/refresh + cfg.index = newIndex(cfg.Title, cfg.Refresh) } if cfg.Next == nil { diff --git a/middleware/monitor/index.go b/middleware/monitor/index.go index 6313685290..4267b7c4e2 100644 --- a/middleware/monitor/index.go +++ b/middleware/monitor/index.go @@ -1,8 +1,32 @@ package monitor +import ( + "strconv" + "strings" + "time" +) + +// returns index with new title/refresh +func newIndex(title string, refresh time.Duration) string { + + timeout := refresh.Milliseconds() - timeoutDiff + if timeout < timeoutDiff { + timeout = timeoutDiff + } + ts := strconv.FormatInt(timeout, 10) + + index := strings.ReplaceAll(indexHtml, "$TITLE", title) + return strings.ReplaceAll(index, "$TIMEOUT", ts) +} + const ( defaultTitle = "Fiber Monitor" + defaultRefresh = 3 * time.Second + timeoutDiff = 200 // timeout will be Refresh (in millisconds) - timeoutDiff + minRefresh = timeoutDiff * time.Millisecond + + // parametrized by $TITLE and $TIMEOUT indexHtml = ` @@ -10,7 +34,7 @@ const ( - ` + defaultTitle + ` + $TITLE