Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃搳 add page title to monitor mw #1893

Merged
merged 1 commit into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions middleware/monitor/README.md
Expand Up @@ -24,23 +24,35 @@ import (

func main() {
app := fiber.New()
app.Get("/dashboard", monitor.New())

app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"}))

log.Fatal(app.Listen(":3000"))
}
```
You can also access the API endpoint with
`curl -X GET -H "Accept: application/json" http://localhost:3000/metrics` which returns:
```json
{"pid":{ "cpu":0.4568381746582226, "ram":20516864, "conns":3 },
"os": { "cpu":8.759124087593099, "ram":3997155328, "conns":44,
"total_ram":8245489664, "load_avg":0.51 }}
```

## Config

```go
// Config defines the config for middleware.
type Config struct {
// Metrics page title
//
// Optional. Default: "Fiber Monitor"
Title string

// To disable serving HTML, you can make true this option.
//
// Optional. Default: false
APIOnly bool

// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Expand All @@ -52,7 +64,8 @@ type Config struct {

```go
var ConfigDefault = Config{
Title: "Fiber Monitor",
APIOnly: false,
Next: nil,
Next: nil,
}
```
41 changes: 40 additions & 1 deletion middleware/monitor/config.go
@@ -1,9 +1,18 @@
package monitor

import "github.com/gofiber/fiber/v2"
import (
"strings"

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

// Config defines the config for middleware.
type Config struct {
// Metrics page title
//
// Optional. Default: "Fiber Monitor"
Title string

// Whether the service should expose only the monitoring API.
//
// Optional. Default: false
Expand All @@ -13,14 +22,36 @@ type Config struct {
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool

// defaultTitle substituted with Title in indexHtml
index string
}

var prevTitle = defaultTitle

var ConfigDefault = Config{
Title: defaultTitle,
APIOnly: false,
Next: nil,
index: indexHtml,
}

func newIndex(title string) string {
if title == defaultTitle {
return indexHtml
}
return strings.ReplaceAll(indexHtml, defaultTitle, title)
}

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)
}

// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
Expand All @@ -30,6 +61,14 @@ func configDefault(config ...Config) Config {
cfg := config[0]

// Set default values
if cfg.Title == "" || cfg.Title == ConfigDefault.Title {
cfg.Title = ConfigDefault.Title
cfg.index = ConfigDefault.index
} else {
// update cfg.index with new title
cfg.index = newIndex(cfg.Title)
}

if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
Expand Down