From dd22991f161dd7514b488b1d22a078ae108781bb Mon Sep 17 00:00:00 2001 From: Taheri24 Date: Wed, 29 Jun 2022 14:34:43 +0430 Subject: [PATCH 1/4] add some parameters to config for supporting #1936 --- middleware/monitor/config.go | 35 ++-- middleware/monitor/index.go | 257 +++--------------------------- middleware/monitor/index.html.tpl | 229 ++++++++++++++++++++++++++ 3 files changed, 278 insertions(+), 243 deletions(-) create mode 100644 middleware/monitor/index.html.tpl diff --git a/middleware/monitor/config.go b/middleware/monitor/config.go index dbd3186835..ed32f84699 100644 --- a/middleware/monitor/config.go +++ b/middleware/monitor/config.go @@ -28,28 +28,38 @@ type Config struct { // Optional. Default: nil Next func(c *fiber.Ctx) bool - // customized indexHtml + CustomHead string + FontURL string + ChartJsURL string + index string } var ConfigDefault = Config{ - Title: defaultTitle, - Refresh: defaultRefresh, - APIOnly: false, - Next: nil, - index: newIndex(defaultTitle, defaultRefresh), + Title: defaultTitle, + Refresh: defaultRefresh, + FontURL: defaultFontURL, + ChartJsURL: defaultChartJsURL, + CustomHead: defaultCustomHead, + APIOnly: false, + Next: nil, + index: newIndex(viewBag{defaultTitle, defaultRefresh, defaultFontURL, defaultChartJsURL, + defaultCustomHead}), } func configDefault(config ...Config) Config { // Users can change ConfigDefault.Title/Refresh which then // become incompatible with ConfigDefault.index - if ConfigDefault.Title != defaultTitle || ConfigDefault.Refresh != defaultRefresh { + if ConfigDefault.Title != defaultTitle || ConfigDefault.Refresh != defaultRefresh || + ConfigDefault.FontURL != defaultFontURL || ConfigDefault.ChartJsURL != defaultChartJsURL || + ConfigDefault.CustomHead != defaultCustomHead { if ConfigDefault.Refresh < minRefresh { ConfigDefault.Refresh = minRefresh } // update default index with new default title/refresh - ConfigDefault.index = newIndex(ConfigDefault.Title, ConfigDefault.Refresh) + ConfigDefault.index = newIndex(viewBag{ConfigDefault.Title, + ConfigDefault.Refresh, ConfigDefault.FontURL, ConfigDefault.ChartJsURL, ConfigDefault.CustomHead}) } // Return default config if nothing provided @@ -68,7 +78,13 @@ func configDefault(config ...Config) Config { if cfg.Refresh == 0 { cfg.Refresh = ConfigDefault.Refresh } + if cfg.FontURL == "" { + cfg.FontURL = defaultFontURL + } + if cfg.ChartJsURL == "" { + cfg.ChartJsURL = defaultChartJsURL + } if cfg.Title == ConfigDefault.Title && cfg.Refresh == ConfigDefault.Refresh { cfg.index = ConfigDefault.index } else { @@ -76,7 +92,8 @@ func configDefault(config ...Config) Config { cfg.Refresh = minRefresh } // update cfg.index with custom title/refresh - cfg.index = newIndex(cfg.Title, cfg.Refresh) + cfg.index = newIndex(viewBag{cfg.Title, + cfg.Refresh, cfg.FontURL, cfg.ChartJsURL, cfg.CustomHead}) } if cfg.Next == nil { diff --git a/middleware/monitor/index.go b/middleware/monitor/index.go index 4267b7c4e2..379a6c42af 100644 --- a/middleware/monitor/index.go +++ b/middleware/monitor/index.go @@ -6,252 +6,41 @@ import ( "time" ) +type viewBag struct { + title string + refresh time.Duration + fontUrl string + chartJsUrl string + customHead string +} + // returns index with new title/refresh -func newIndex(title string, refresh time.Duration) string { +func newIndex(dat viewBag) string { - timeout := refresh.Milliseconds() - timeoutDiff + timeout := dat.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) + replacer := strings.NewReplacer("$TITLE", dat.title, "$TIMEOUT", ts, + "$FONT_URL", dat.fontUrl, "$CHART_JS_URL", dat.chartJsUrl, "$CUSTOM_HEAD", dat.customHead, + ) + return replacer.Replace(indexHtml) } const ( defaultTitle = "Fiber Monitor" - defaultRefresh = 3 * time.Second - timeoutDiff = 200 // timeout will be Refresh (in millisconds) - timeoutDiff - minRefresh = timeoutDiff * time.Millisecond + defaultRefresh = 3 * time.Second + timeoutDiff = 200 // timeout will be Refresh (in milliseconds) - timeoutDiff + minRefresh = timeoutDiff * time.Millisecond + defaultFontURL = `https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap` + defaultChartJsURL = `https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js` + defaultCustomHead = `` // parametrized by $TITLE and $TIMEOUT - indexHtml = ` - - - - - - - $TITLE - - - -
-

$TITLE

-
-
-
-
CPU Usage
-

0.00%

-
-
- -
-
-
-
-
Memory Usage
-

0.00 MB

-
-
- -
-
-
-
-
Response Time
-

0ms

-
-
- -
-
-
-
-
Open Connections
-

0

-
-
- -
-
-
-
- - -` ) + +// go:embed index.html.tpl +var indexHtml string diff --git a/middleware/monitor/index.html.tpl b/middleware/monitor/index.html.tpl new file mode 100644 index 0000000000..ea7c38ad42 --- /dev/null +++ b/middleware/monitor/index.html.tpl @@ -0,0 +1,229 @@ + + + + + + + + + $TITLE + + + +
+

$TITLE

+
+
+
+
CPU Usage
+

0.00%

+
+
+ +
+
+
+
+
Memory Usage
+

0.00 MB

+
+
+ +
+
+
+
+
Response Time
+

0ms

+
+
+ +
+
+
+
+
Open Connections
+

0

+
+
+ +
+
+
+
+ + + From 7324be5f0d35081e5f002904231bfd9c1a660fcf Mon Sep 17 00:00:00 2001 From: Taheri24 Date: Wed, 29 Jun 2022 18:02:53 +0430 Subject: [PATCH 2/4] remove go:embed --- middleware/monitor/index.go | 232 +++++++++++++++++++++++++++++++++++- 1 file changed, 229 insertions(+), 3 deletions(-) diff --git a/middleware/monitor/index.go b/middleware/monitor/index.go index 379a6c42af..b17c95f9e4 100644 --- a/middleware/monitor/index.go +++ b/middleware/monitor/index.go @@ -39,8 +39,234 @@ const ( defaultCustomHead = `` // parametrized by $TITLE and $TIMEOUT + indexHtml = ` + + + + + + -) + $TITLE + + + +
+

$TITLE

+
+
+
+
CPU Usage
+

0.00%

+
+
+ +
+
+
+
+
Memory Usage
+

0.00 MB

+
+
+ +
+
+
+
+
Response Time
+

0ms

+
+
+ +
+
+
+
+
Open Connections
+

0

+
+
+ +
+
+
+
+ + + +` +) From 18f857f24b18485d50bc3d4d117530179dc46313 Mon Sep 17 00:00:00 2001 From: Taheri24 Date: Wed, 29 Jun 2022 21:34:49 +0430 Subject: [PATCH 3/4] apply code review - 1956#issuecomment-1169811117 --- middleware/monitor/README.md | 23 ++- middleware/monitor/config.go | 14 +- middleware/monitor/index.html.tpl | 229 ----------------------------- middleware/monitor/monitor_test.go | 44 ++++++ 4 files changed, 79 insertions(+), 231 deletions(-) delete mode 100644 middleware/monitor/index.html.tpl diff --git a/middleware/monitor/README.md b/middleware/monitor/README.md index 8ac23e4211..5987aec00b 100644 --- a/middleware/monitor/README.md +++ b/middleware/monitor/README.md @@ -53,7 +53,7 @@ type Config struct { // Optional. Default: 3 seconds Refresh time.Duration - // To disable serving HTML, you can make true this option. + // Whether the service should expose only the monitoring API. // // Optional. Default: false APIOnly bool @@ -62,6 +62,23 @@ type Config struct { // // Optional. Default: nil Next func(c *fiber.Ctx) bool + + // Custom HTML Code to Head Section(Before End) + // + // Optional. Default: empty + CustomHead string + + // FontURL for specify font resource path or URL . also you can use relative path + // + // Optional. Default: https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap + + FontURL string + // ChartJsURL for specify ChartJS library path or URL . also you can use relative path + // + // Optional. Default: https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js + + ChartJsURL string + } ``` @@ -73,5 +90,9 @@ var ConfigDefault = Config{ Refresh: 3 * time.Second, APIOnly: false, Next: nil, + CustomHead:"", + FontURL:"https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap", + ChartJsURL:"https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js" + } ``` diff --git a/middleware/monitor/config.go b/middleware/monitor/config.go index ed32f84699..fda0d8a5b3 100644 --- a/middleware/monitor/config.go +++ b/middleware/monitor/config.go @@ -28,8 +28,20 @@ type Config struct { // Optional. Default: nil Next func(c *fiber.Ctx) bool + // Custom HTML Code to Head Section(Before End) + // + // Optional. Default: empty CustomHead string - FontURL string + + // FontURL for specify font resource path or URL . also you can use relative path + // + // Optional. Default: https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap + + FontURL string + // ChartJsURL for specify ChartJS library path or URL . also you can use relative path + // + // Optional. Default: https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js + ChartJsURL string index string diff --git a/middleware/monitor/index.html.tpl b/middleware/monitor/index.html.tpl deleted file mode 100644 index ea7c38ad42..0000000000 --- a/middleware/monitor/index.html.tpl +++ /dev/null @@ -1,229 +0,0 @@ - - - - - - - - - $TITLE - - - -
-

$TITLE

-
-
-
-
CPU Usage
-

0.00%

-
-
- -
-
-
-
-
Memory Usage
-

0.00 MB

-
-
- -
-
-
-
-
Response Time
-

0ms

-
-
- -
-
-
-
-
Open Connections
-

0

-
-
- -
-
-
-
- - - diff --git a/middleware/monitor/monitor_test.go b/middleware/monitor/monitor_test.go index 6fd4d654c6..7b73e295ed 100644 --- a/middleware/monitor/monitor_test.go +++ b/middleware/monitor/monitor_test.go @@ -61,6 +61,50 @@ func Test_Monitor_Html(t *testing.T) { conf.Refresh.Milliseconds()-timeoutDiff) utils.AssertEqual(t, true, bytes.Contains(buf, []byte(timeoutLine))) } +func Test_Monitor_Html_CustomCodes(t *testing.T) { + t.Parallel() + + app := fiber.New() + + // defaults + app.Get("/", New()) + resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil)) + + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, 200, resp.StatusCode) + utils.AssertEqual(t, fiber.MIMETextHTMLCharsetUTF8, + resp.Header.Get(fiber.HeaderContentType)) + buf, err := ioutil.ReadAll(resp.Body) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte(""+defaultTitle+""))) + timeoutLine := fmt.Sprintf("setTimeout(fetchJSON, %d)", + defaultRefresh.Milliseconds()-timeoutDiff) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte(timeoutLine))) + + // custom config + conf := Config{Title: "New " + defaultTitle, Refresh: defaultRefresh + time.Second, + ChartJsURL: "https://cdnjs.com/libraries/Chart.js", + FontURL: "/public/my-font.css", + CustomHead: ``, + } + app.Get("/custom", New(conf)) + resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/custom", nil)) + + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, 200, resp.StatusCode) + utils.AssertEqual(t, fiber.MIMETextHTMLCharsetUTF8, + resp.Header.Get(fiber.HeaderContentType)) + buf, err = ioutil.ReadAll(resp.Body) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte(""+conf.Title+""))) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte("https://cdnjs.com/libraries/Chart.js"))) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte("/public/my-font.css"))) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte(conf.CustomHead))) + + timeoutLine = fmt.Sprintf("setTimeout(fetchJSON, %d)", + conf.Refresh.Milliseconds()-timeoutDiff) + utils.AssertEqual(t, true, bytes.Contains(buf, []byte(timeoutLine))) +} // go test -run Test_Monitor_JSON -race func Test_Monitor_JSON(t *testing.T) { From 8839884e94d368603f0c9089c0a8f0cb0ae0fc6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Efe=20=C3=87etin?= Date: Wed, 29 Jun 2022 21:37:46 +0300 Subject: [PATCH 4/4] lint --- middleware/monitor/config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/middleware/monitor/config.go b/middleware/monitor/config.go index fda0d8a5b3..a5d08b65ca 100644 --- a/middleware/monitor/config.go +++ b/middleware/monitor/config.go @@ -36,12 +36,11 @@ type Config struct { // FontURL for specify font resource path or URL . also you can use relative path // // Optional. Default: https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap - FontURL string + // ChartJsURL for specify ChartJS library path or URL . also you can use relative path // // Optional. Default: https://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js - ChartJsURL string index string