-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Migrate monitor middleware from Fiber v2 to v3 #3125
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package monitor | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gofiber/fiber/v3" | ||
) | ||
|
||
// Config defines the config for middleware. | ||
type Config struct { | ||
// Metrics page title | ||
// | ||
// 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 | ||
APIOnly bool | ||
|
||
// Next defines a function to skip this middleware when returned true. | ||
// | ||
// 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/[email protected]/dist/Chart.bundle.min.js | ||
ChartJsURL string // TODO: Rename to "ChartJSURL" in v3 | ||
|
||
index string | ||
} | ||
|
||
var ConfigDefault = Config{ | ||
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 || | ||
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(viewBag{ | ||
ConfigDefault.Title, | ||
ConfigDefault.Refresh, | ||
ConfigDefault.FontURL, | ||
ConfigDefault.ChartJsURL, | ||
ConfigDefault.CustomHead, | ||
}) | ||
} | ||
|
||
// Return default config if nothing provided | ||
if len(config) < 1 { | ||
return ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
// Set default values | ||
if cfg.Title == "" { | ||
cfg.Title = ConfigDefault.Title | ||
} | ||
|
||
if cfg.Refresh == 0 { | ||
cfg.Refresh = ConfigDefault.Refresh | ||
} | ||
if cfg.FontURL == "" { | ||
cfg.FontURL = defaultFontURL | ||
} | ||
|
||
if cfg.ChartJsURL == "" { | ||
cfg.ChartJsURL = defaultChartJSURL | ||
} | ||
if cfg.Refresh < minRefresh { | ||
cfg.Refresh = minRefresh | ||
} | ||
|
||
if cfg.Next == nil { | ||
cfg.Next = ConfigDefault.Next | ||
} | ||
|
||
if !cfg.APIOnly { | ||
cfg.APIOnly = ConfigDefault.APIOnly | ||
} | ||
|
||
// update cfg.index with custom title/refresh | ||
cfg.index = newIndex(viewBag{ | ||
title: cfg.Title, | ||
refresh: cfg.Refresh, | ||
fontURL: cfg.FontURL, | ||
chartJSURL: cfg.ChartJsURL, | ||
customHead: cfg.CustomHead, | ||
}) | ||
|
||
return cfg | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize struct field alignment.
The
Config
struct could be optimized for memory usage by rearranging its fields. This change can potentially reduce the struct's memory footprint, improving performance especially in environments where many instances of this struct might be created.Consider rearranging the fields to optimize alignment. Here's a suggested rearrangement:
This rearrangement groups the larger
string
andtime.Duration
types together, placing the smallerbool
and function pointer types appropriately to fit better in memory alignment slots.Committable suggestion
Tools
golangci-lint