Skip to content

Commit

Permalink
📊 add page title to monitor mw (gofiber#1893)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcg authored and trim21 committed Aug 15, 2022
1 parent 66c8177 commit db4040f
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 600 deletions.
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

0 comments on commit db4040f

Please sign in to comment.