feat(router): enhance routing capabilities with new features#24
Merged
Conversation
- Added support for custom 404 Not Found and 405 Method Not Allowed handlers via WithNotFound and WithMethodNotAllowed options. - Introduced WithStripSlash and WithRedirectSlash options for handling trailing slashes in routes. - Implemented URL method for generating URLs for named routes with parameter substitution. - Added Route method for inline sub-routing, allowing nested route definitions. - Enhanced RouteEntry to support named routes and improved middleware handling with With method. - Introduced Walk method to iterate over registered routes and Routes method to retrieve a snapshot of all routes. - Updated error handling in DefaultErrorHandler to ensure proper response formatting. - Added tests for new features, including middleware application, route handling, and error scenarios.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Added
RouteEntry.Name(name)andRouter.URL(name, params...)for reverse URL generation with{param}and{param...}placeholder substitutionRouter.Routes()returns a snapshot of all registeredRouteInfoentries;Router.Walk(fn)iterates with early-exit supportRouteInfo.HandlerNamecaptures the original handler's function name viaruntime.FuncForPCfor debugging and documentationValidateParams(handler, constraints...)wraps a handler with path-parameter validation; built-in constraint constructorsInt,UUID,Regex,OneOfWith(middleware...)returns a child group for per-route inline middleware without affecting sibling routesRoute(prefix, fn, middleware...)for inline sub-routing — creates a child group, callsfnto register routes, and returns the group for further useMount(prefix, handler)attaches anhttp.Handler(or*Router) at a prefix withhttp.StripPrefix; sub-router routes and named routes are merged into the parent's route tableStatic(prefix, dir)serves files from a filesystem directory;File(pattern, filePath)serves a single file for GET requestsWithNotFound(handler)andWithMethodNotAllowed(handler)options for custom 404/405 handlers, taking precedence over theErrorHandlerWithStripSlash()silently removes trailing slashes before routing;WithRedirectSlash()sends 301 redirects (mutually exclusive, panics if both set)Changed
Get,Post,Put,Patch,Delete,Head,Optionsand theirFuncvariants) andHandle/HandleFuncnow return*RouteEntryfor optional.Name()chainingregister()accepts an additionalorigFnparameter to capture the original handler name before middleware wrappingChecklist
make all)