Skip to content

feat(router): enhance routing capabilities with new features#24

Merged
KARTIKrocks merged 1 commit into
mainfrom
feature-router-v016
Apr 6, 2026
Merged

feat(router): enhance routing capabilities with new features#24
KARTIKrocks merged 1 commit into
mainfrom
feature-router-v016

Conversation

@KARTIKrocks
Copy link
Copy Markdown
Owner

  • 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.

Summary

Added

  • router — Named routes via RouteEntry.Name(name) and Router.URL(name, params...) for reverse URL generation with {param} and {param...} placeholder substitution
  • router — Route introspection: Router.Routes() returns a snapshot of all registered RouteInfo entries; Router.Walk(fn) iterates with early-exit support
  • routerRouteInfo.HandlerName captures the original handler's function name via runtime.FuncForPC for debugging and documentation
  • router — Parameter constraints: ValidateParams(handler, constraints...) wraps a handler with path-parameter validation; built-in constraint constructors Int, UUID, Regex, OneOf
  • routerWith(middleware...) returns a child group for per-route inline middleware without affecting sibling routes
  • routerRoute(prefix, fn, middleware...) for inline sub-routing — creates a child group, calls fn to register routes, and returns the group for further use
  • routerMount(prefix, handler) attaches an http.Handler (or *Router) at a prefix with http.StripPrefix; sub-router routes and named routes are merged into the parent's route table
  • routerStatic(prefix, dir) serves files from a filesystem directory; File(pattern, filePath) serves a single file for GET requests
  • routerWithNotFound(handler) and WithMethodNotAllowed(handler) options for custom 404/405 handlers, taking precedence over the ErrorHandler
  • routerWithStripSlash() silently removes trailing slashes before routing; WithRedirectSlash() sends 301 redirects (mutually exclusive, panics if both set)

Changed

  • router — All method helpers (Get, Post, Put, Patch, Delete, Head, Options and their Func variants) and Handle/HandleFunc now return *RouteEntry for optional .Name() chaining
  • routerregister() accepts an additional origFn parameter to capture the original handler name before middleware wrapping

Checklist

  • fmt, vet, lint, test, build passes (make all)
  • New code has tests where appropriate
  • Breaking changes are documented

- 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.
@KARTIKrocks KARTIKrocks self-assigned this Apr 6, 2026
@KARTIKrocks KARTIKrocks merged commit 8b89ef3 into main Apr 6, 2026
10 checks passed
@KARTIKrocks KARTIKrocks deleted the feature-router-v016 branch April 6, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant