-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
🧹 chore: Refactor configuration management for favicon and envvar middlewares #3898
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
🧹 chore: Refactor configuration management for favicon and envvar middlewares #3898
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughRefactors configuration handling in EnvVar and Favicon middleware by moving Config structs to separate config.go files and introducing configDefault helper functions to consolidate default initialization and merge logic. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on refactoring the configuration management for several middleware components, specifically Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3898 +/- ##
==========================================
- Coverage 91.68% 91.67% -0.02%
==========================================
Files 117 119 +2
Lines 10030 10039 +9
==========================================
+ Hits 9196 9203 +7
- Misses 530 531 +1
- Partials 304 305 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Code Review
This pull request effectively refactors the configuration handling for the envvar middleware by moving the Config struct, default values, and a new helper function into a separate config.go file. This improves code organization and simplifies the middleware's New function.
I noticed that the same refactoring pattern was applied to the favicon middleware, which is a great improvement for consistency. However, this was not mentioned in the pull request summary. It would be helpful to update the summary to reflect the full scope of the changes.
I have a couple of suggestions to improve the new configuration helpers.
| if cfg.ExportVars == nil { | ||
| cfg.ExportVars = ConfigDefault.ExportVars | ||
| } |
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.
Assigning ConfigDefault.ExportVars directly shares the map from the global ConfigDefault variable. While it's currently initialized as an empty map, ConfigDefault is a mutable global variable. If any other part of the code were to modify ConfigDefault.ExportVars, it could lead to unexpected side effects for all middleware instances created with a nil ExportVars map. It's safer to initialize a new empty map to prevent such issues.
| if cfg.ExportVars == nil { | |
| cfg.ExportVars = ConfigDefault.ExportVars | |
| } | |
| if cfg.ExportVars == nil { | |
| cfg.ExportVars = map[string]string{} | |
| } |
| if cfg.File == "" { | ||
| cfg.File = ConfigDefault.File | ||
| } |
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.
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.
Pull request overview
This PR refactors the configuration handling for two middleware packages (favicon and envvar) by extracting config structures and defaults into dedicated config.go files. The changes improve code organization without altering functionality.
Key Changes:
- Introduced
config.gofiles for both favicon and envvar middlewares containing Config structs, ConfigDefault variables, and configDefault helper functions - Simplified middleware constructors by delegating config merging to the new configDefault helpers
- Maintained backward compatibility and existing behavior
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| middleware/favicon/config.go | New file containing Config struct, ConfigDefault variable, and configDefault helper function for merging user-provided config with defaults |
| middleware/favicon/favicon.go | Removed Config struct and default merging logic, now delegates to configDefault helper from config.go |
| middleware/envvar/config.go | New file containing Config struct, ConfigDefault variable (with empty map default), and configDefault helper function |
| middleware/envvar/envvar.go | Removed Config struct, now delegates config merging to configDefault helper from config.go |
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
middleware/favicon/config.go (1)
9-72: Config +configDefaultpattern looks solid; one tiny simplification possibleThe structure (exported
Config, exportedConfigDefault, unexportedconfigDefault) is consistent with other middleware and correctly covers all fields used infavicon.go. Defaults forURLandCacheControlare clearly defined here and applied inconfigDefault, which keepsNewsimple.Minor nit: the
Filefield is explicitly defaulted even thoughConfigDefault.Fileis"", so this block is currently a no-op:if cfg.File == "" { cfg.File = ConfigDefault.File }You could drop it until there’s a non‑empty default for
Fileto slightly reduce noise, but it’s harmless either way.middleware/envvar/config.go (1)
3-26: Config helper is correct; small doc and immutability nitsThe config type and
configDefaulthelper correctly ensureExportVarsis always non‑nil, which matches hownewEnvVarconsumes it and keeps the “no config ⇒ expose nothing” behavior.Two minor polish suggestions:
- The comment
// ExportVars specifies the environment variables that should exportreads a bit off; consider “that should be exported” for clarity.- To keep
ConfigDefaultconceptually immutable, you might defaultExportVarsto a fresh empty map instead of reusingConfigDefault.ExportVarswhencfg.ExportVarsis nil, e.g.:if cfg.ExportVars == nil { cfg.ExportVars = map[string]string{} }Current code is still safe since you only read from
ExportVars, so this is just future‑proofing and not required.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
middleware/envvar/config.go(1 hunks)middleware/envvar/envvar.go(1 hunks)middleware/favicon/config.go(1 hunks)middleware/favicon/favicon.go(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
Repo: gofiber/fiber PR: 3016
File: middleware/session/config.go:16-26
Timestamp: 2024-10-08T19:06:06.583Z
Learning: In the session middleware `Config` struct, `Store` is backed by `fiber.Storage`; they are different entities serving distinct purposes in session management.
Applied to files:
middleware/envvar/envvar.go
🧬 Code graph analysis (2)
middleware/envvar/config.go (1)
middleware/favicon/config.go (2)
Config(10-41)ConfigDefault(44-49)
middleware/favicon/config.go (1)
middleware/envvar/config.go (2)
Config(4-7)ConfigDefault(10-12)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: Agent
- GitHub Check: Compare
- GitHub Check: repeated
- GitHub Check: unit (1.25.x, macos-latest)
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: lint
🔇 Additional comments (2)
middleware/favicon/favicon.go (1)
20-25: Centralizing defaults viaconfigDefaultkeeps behavior consistentUsing
cfg := configDefault(config...)here cleanly centralizes favicon defaults (URL, Cache-Control, etc.) while preserving the existing behavior ofNew, and avoids duplicating defaulting logic between files. No issues spotted.middleware/envvar/envvar.go (1)
23-35: UsingconfigDefaultinNewkeeps envvar behavior unchanged and clearerSwitching to
cfg := configDefault(config...)here aligns envvar with the shared config pattern and ensurescfg.ExportVarsis consistently defaulted viaConfigDefaultwhen no config (or a nil map) is supplied. The handler behavior (no exposure whenExportVarsis empty) remains intact. Looks good.
Summary