🔥 feat: Add support for configuring the Regex engine on the router#4254
🔥 feat: Add support for configuring the Regex engine on the router#4254
Conversation
- Created RegexEngine and RegexCompiler interfaces - Updated path.go to use configured regex engine - Added RegexEngine field to Config with DefaultRegexEngine - Updated router.go to pass RegexEngine to parseRoute - Added comprehensive tests for regex abstraction - Created example showing how to integrate coregex Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/3e2b7990-e9c5-41f6-afbf-ad29c8bbcfd0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4254 +/- ##
==========================================
- Coverage 91.16% 91.14% -0.02%
==========================================
Files 127 127
Lines 12605 12641 +36
==========================================
+ Hits 11491 11522 +31
- Misses 697 701 +4
- Partials 417 418 +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:
|
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/3e2b7990-e9c5-41f6-afbf-ad29c8bbcfd0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
- Added regex-engines.md guide with coregex integration examples - Documented RegexEngine and RegexCompiler interfaces - Included performance benchmarks and use cases - Added custom implementation examples Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/3e2b7990-e9c5-41f6-afbf-ad29c8bbcfd0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
- Removed examples/regex-engine folder as requested - Updated docs/guide/regex-engines.md to remove reference to examples folder - All markdown linting issues resolved Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/9e71d891-0cc3-4786-8921-d994e500db6f Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Fixed in d7629bf:
|
|
@claude[agent] I'm not sure about the addition of I believe the approach should be as straightforward as our JSONEncoder and Decoder implementations within the app configuration. |
Moved regex types from separate regex.go file into path.go where they are used. This follows the simpler pattern similar to JSONEncoder/Decoder and reduces over-engineering by keeping related code together. Changes: - Removed regex.go and moved RegexEngine/RegexCompiler types to path.go - Moved tests from regex_test.go to path_test.go - All tests pass, no linting issues Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/175129f3-3f03-4a29-9673-b0521797d587 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Simplified in ffc10c3. Removed the separate |
|
@claude[agent] Looking at the guide docs/guide/regex-engines.md and code. Using a different regex library is too complicated. We can simplify this. |
Following feedback to simplify the regex engine abstraction, this commit
refactors the implementation to use a simple function type instead of
interface-based approach.
Key changes:
- Replace RegexEngine interface with RegexHandler function type
- RegexHandler is now: func(pattern string) RegexCompiler
- Add defaultRegexHandler function that wraps regexp.MustCompile
- Update all references from RegexEngine to RegexHandler
- Simplify documentation to show direct usage without adapter types
This follows the same pattern as JSONEncoder/JSONDecoder in the codebase,
making it much simpler for users to adopt alternative regex engines:
Before (required adapter types):
type CoregexEngine struct{}
func (CoregexEngine) MustCompile(pattern string) fiber.RegexCompiler {...}
app := fiber.New(fiber.Config{RegexEngine: CoregexEngine{}})
After (just a function):
app := fiber.New(fiber.Config{
RegexHandler: func(pattern string) fiber.RegexCompiler {
return coregex.MustCompile(pattern)
},
})
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
@claude[agent] Why are you adding a handler, instead of just using // stdlib // coregex — same API The app config option should just take |
There was a problem hiding this comment.
Pull request overview
This PR adds a new fiber.Config.RegexHandler extension point so route regex() constraints can be compiled with a configurable regex engine instead of always using the standard library. It fits into Fiber’s router/path parsing layer by threading the configured compiler through route registration and RoutePatternMatch.
Changes:
- Add
Config.RegexHandlerwith a default ofregexp.MustCompile, and pass it into route parsing during route registration and prefixing. - Generalize regex constraint compilation in
path.goso compiled regex matchers are stored behind an interface. - Add tests and a new guide page describing alternative regex engine usage, plus a number of formatting-only updates in unrelated files.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| router.go | Passes app.config.RegexHandler into route parsing during registration/prefixing. |
| router_test.go | Updates direct parseRoute call sites to the new signature. |
| prefork.go | Formatting-only change. |
| path.go | Introduces RegexHandler/RegexCompiler and uses them for regex() constraints and RoutePatternMatch. |
| path_test.go | Updates parser tests and adds new coverage for configurable regex handlers. |
| middleware/logger/default_logger.go | Formatting-only change. |
| middleware/envvar/envvar_test.go | Formatting-only change. |
| middleware/compress/compress.go | Formatting-only change. |
| helpers.go | Formatting-only change. |
| helpers_test.go | Formatting-only change. |
| domain_test.go | Formatting-only change. |
| docs/guide/regex-engines.md | Adds user-facing documentation for alternative regex engines. |
| ctx_test.go | Formatting-only change. |
| client/request_test.go | Formatting-only change. |
| client/client_test.go | Formatting-only change. |
| app.go | Adds the new public Config.RegexHandler field and its default initialization. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/f9b8dbc7-cc72-4f60-b345-5b668e0ceed6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/f9b8dbc7-cc72-4f60-b345-5b668e0ceed6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/f9b8dbc7-cc72-4f60-b345-5b668e0ceed6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Applied the review-thread follow-ups in 4d138de. |
| if handlerType.Kind() != reflect.Func || handlerValue.IsNil() { | ||
| panic("fiber: Config.RegexHandler must be a non-nil function") | ||
| } | ||
| if handlerType.NumIn() != 1 || handlerType.In(0).Kind() != reflect.String || handlerType.NumOut() != 1 { | ||
| panic("fiber: Config.RegexHandler must have signature func(string) T") |
| // segment when matching incoming requests. | ||
| type Constraint struct { | ||
| RegexCompiler *regexp.Regexp | ||
| RegexCompiler RegexCompiler |
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/bb345d14-e46f-4eb3-80d3-76fb16369d8d Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/bb345d14-e46f-4eb3-80d3-76fb16369d8d Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/bb345d14-e46f-4eb3-80d3-76fb16369d8d Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Applied the follow-up changes in 67f902d. |
Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.