Documentation
¶
Overview ¶
Package router provides HTTP routing functionality for the Velocity framework. It uses a custom radix tree for efficient route matching and supports RESTful resources, middleware, and a clean API for web applications.
Index ¶
- func BuildPath(segments []Segment, params map[string]string) (string, error)
- func CurrentRoute(r *http.Request) string
- func GetParams(r *http.Request) map[string]string
- func GetRouteName(r *http.Request) string
- func LoadRoutes()
- func Param(r *http.Request, name string) string
- func Params(r *http.Request) map[string]string
- func Register(fn RegistrationFunc)
- func RegisterWithPrefix(prefix string, fn RegistrationFunc)
- func ResetGlobalRouter()
- func Route(name string, params map[string]string) (string, error)
- func SetParams(r *http.Request, params map[string]string) *http.Request
- func SetRouteName(r *http.Request, name string) *http.Request
- func Wrap(h HandlerFunc) http.HandlerFunc
- type Context
- func (c *Context) BadRequest(message ...string) error
- func (c *Context) Bind(v interface{}) error
- func (c *Context) Cookie(name string) (*http.Cookie, error)
- func (c *Context) Error(status int, message string) error
- func (c *Context) Forbidden(message ...string) error
- func (c *Context) Get(key string) interface{}
- func (c *Context) GetString(key string) string
- func (c *Context) HTML(status int, html string) error
- func (c *Context) Header(name string) string
- func (c *Context) IP() string
- func (c *Context) IsAjax() bool
- func (c *Context) JSON(status int, data interface{}) error
- func (c *Context) Method() string
- func (c *Context) NoContent() error
- func (c *Context) NotFound(message ...string) error
- func (c *Context) Param(name string) string
- func (c *Context) Path() string
- func (c *Context) Query(name string) string
- func (c *Context) QueryDefault(name, defaultValue string) string
- func (c *Context) Redirect(status int, url string) error
- func (c *Context) Set(key string, value interface{})
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetHeader(name, value string)
- func (c *Context) Status(status int) error
- func (c *Context) String(status int, text string) error
- func (c *Context) Unauthorized(message ...string) error
- func (c *Context) WantsJSON() bool
- type Error
- type GroupDefinition
- func (g *GroupDefinition) AddChild(prefix string) *GroupDefinition
- func (g *GroupDefinition) AddRoute(method, path string, handler HandlerFunc) *RouteDefinition
- func (g *GroupDefinition) CommitToTree(tree *Tree, globalMiddlewares []MiddlewareFunc) error
- func (g *GroupDefinition) FullPrefix() string
- func (g *GroupDefinition) Use(middlewares ...MiddlewareFunc)
- type HandlerFunc
- type MatchResult
- type MiddlewareFunc
- type Node
- type RegistrationFunc
- type ResourceConfig
- type ResourceController
- type ResourceRoute
- type RouteConfig
- type RouteDefinition
- type RouteNotFoundError
- type Router
- type Segment
- type SegmentType
- type Tree
- type VelocityRouter
- type VelocityRouterV2
- func (r *VelocityRouterV2) Any(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Delete(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Get(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Group(prefix string, fn ...func(Router)) Router
- func (r *VelocityRouterV2) Handle() http.Handler
- func (r *VelocityRouterV2) Head(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Match(methods []string, path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Options(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Patch(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Post(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Prefix(prefix string)
- func (r *VelocityRouterV2) Put(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouterV2) Resource(path string, controller interface{}) ResourceRoute
- func (r *VelocityRouterV2) RouteURL(name string, params map[string]string) (string, error)
- func (r *VelocityRouterV2) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *VelocityRouterV2) Static(directory string)
- func (r *VelocityRouterV2) Use(middlewares ...MiddlewareFunc) Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CurrentRoute ¶
CurrentRoute returns the current route name if it exists
func GetRouteName ¶ added in v0.2.0
GetRouteName retrieves the current route name from the request context
func Register ¶
func Register(fn RegistrationFunc)
Register adds a route registration function to be called during initialization
func RegisterWithPrefix ¶
func RegisterWithPrefix(prefix string, fn RegistrationFunc)
RegisterWithPrefix adds a route registration function with a prefix
func ResetGlobalRouter ¶ added in v0.2.0
func ResetGlobalRouter()
ResetGlobalRouter resets the global router (for testing)
func SetParams ¶ added in v0.2.0
SetParams stores route parameters in the request context Returns a new request with the parameters stored
func SetRouteName ¶ added in v0.2.0
SetRouteName stores the current route name in the request context
func Wrap ¶
func Wrap(h HandlerFunc) http.HandlerFunc
Wrap converts a HandlerFunc to http.HandlerFunc
Types ¶
type Context ¶
type Context struct {
Response http.ResponseWriter
Request *http.Request
// contains filtered or unexported fields
}
Context wraps http.Request and http.ResponseWriter with helper methods
func NewContext ¶
func NewContext(w http.ResponseWriter, r *http.Request) *Context
NewContext creates a new Context from http.Request and http.ResponseWriter
func NewContextV2 ¶ added in v0.2.0
func NewContextV2(w http.ResponseWriter, r *http.Request) *Context
NewContextV2 creates a new Context using the new param storage
func (*Context) BadRequest ¶
BadRequest sends a 400 error response
func (*Context) QueryDefault ¶
QueryDefault returns a query parameter or default value if not set
func (*Context) Unauthorized ¶
Unauthorized sends a 401 error response
type GroupDefinition ¶ added in v0.2.0
type GroupDefinition struct {
// contains filtered or unexported fields
}
GroupDefinition holds group configuration for deferred registration
func NewGroupDefinition ¶ added in v0.2.0
func NewGroupDefinition(prefix string, parent *GroupDefinition) *GroupDefinition
NewGroupDefinition creates a new group definition
func (*GroupDefinition) AddChild ¶ added in v0.2.0
func (g *GroupDefinition) AddChild(prefix string) *GroupDefinition
AddChild creates and adds a child group
func (*GroupDefinition) AddRoute ¶ added in v0.2.0
func (g *GroupDefinition) AddRoute(method, path string, handler HandlerFunc) *RouteDefinition
AddRoute adds a route to the group
func (*GroupDefinition) CommitToTree ¶ added in v0.2.0
func (g *GroupDefinition) CommitToTree(tree *Tree, globalMiddlewares []MiddlewareFunc) error
CommitToTree adds all routes in this group and children to the tree
func (*GroupDefinition) FullPrefix ¶ added in v0.2.0
func (g *GroupDefinition) FullPrefix() string
FullPrefix returns the complete prefix including parent prefixes
func (*GroupDefinition) Use ¶ added in v0.2.0
func (g *GroupDefinition) Use(middlewares ...MiddlewareFunc)
Use adds middleware to the group Middleware is applied during CommitToTree, so it works whether called before or after routes
type HandlerFunc ¶
HandlerFunc is the Velocity handler function signature
type MatchResult ¶ added in v0.2.0
type MatchResult struct {
Handler HandlerFunc
Params map[string]string
Name string
Path string
// contains filtered or unexported fields
}
MatchResult contains the matched route info
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc is the Velocity middleware function signature
type Node ¶ added in v0.2.0
type Node struct {
// contains filtered or unexported fields
}
Node represents a node in the radix tree
type RegistrationFunc ¶
type RegistrationFunc func(Router)
RegistrationFunc is a function that registers routes
type ResourceConfig ¶ added in v0.2.0
ResourceConfig maps HTTP verbs to controller methods
func DefaultResourceConfig ¶ added in v0.2.0
func DefaultResourceConfig() []ResourceConfig
DefaultResourceConfig returns the default REST resource configuration
type ResourceController ¶ added in v0.2.0
type ResourceController interface{}
ResourceController defines the interface for a resource controller Controllers can implement any subset of these methods
type ResourceRoute ¶
type ResourceRoute interface {
Only(methods ...string) ResourceRoute
Except(methods ...string) ResourceRoute
}
ResourceRoute represents a resource with configurable methods
type RouteConfig ¶
type RouteConfig interface {
Name(name string) RouteConfig
Use(middlewares ...MiddlewareFunc) RouteConfig
}
RouteConfig represents a single route that can be configured
type RouteDefinition ¶ added in v0.2.0
type RouteDefinition struct {
Method string
Path string
Handler HandlerFunc
Middlewares []MiddlewareFunc
Name string
}
RouteDefinition represents a route before it's committed to the tree
type RouteNotFoundError ¶
type RouteNotFoundError struct {
Name string
}
RouteNotFoundError is returned when a named route doesn't exist
func (*RouteNotFoundError) Error ¶
func (e *RouteNotFoundError) Error() string
type Router ¶
type Router interface {
// HTTP Methods - accepts Context-based handlers
Get(path string, handler HandlerFunc) RouteConfig
Post(path string, handler HandlerFunc) RouteConfig
Put(path string, handler HandlerFunc) RouteConfig
Delete(path string, handler HandlerFunc) RouteConfig
Patch(path string, handler HandlerFunc) RouteConfig
Options(path string, handler HandlerFunc) RouteConfig
Head(path string, handler HandlerFunc) RouteConfig
// Route Management
// Group creates a route group with optional closure for inline route definitions.
// Example: r.Group("/api", func(api Router) { api.Get("/users", handler) })
Group(prefix string, fn ...func(Router)) Router
Prefix(prefix string)
Resource(path string, controller interface{}) ResourceRoute
// Middleware - Context-based
Use(middlewares ...MiddlewareFunc) Router
// Serving
ServeHTTP(w http.ResponseWriter, r *http.Request)
Handle() http.Handler
}
Router defines the interface for HTTP routing
type Segment ¶ added in v0.2.0
type Segment struct {
Type SegmentType
Value string // segment value: "users" for static, "id" for param
Pattern *regexp.Regexp // compiled regex for SegmentRegex
RawPattern string // original pattern string for URL generation
}
Segment represents a parsed path segment
func ParseSegments ¶ added in v0.2.0
ParseSegments parses a path pattern into segments Supports: /static, /{param}, /{param:regex}, /{param:.*}
type SegmentType ¶ added in v0.2.0
type SegmentType int
SegmentType represents the type of path segment
const ( SegmentStatic SegmentType = iota // /users SegmentParam // /{id} SegmentRegex // /{id:[0-9]+} SegmentWildcard // /{path:.*} )
func (SegmentType) String ¶ added in v0.2.0
func (st SegmentType) String() string
String returns the string representation of the segment type
type Tree ¶ added in v0.2.0
type Tree struct {
// contains filtered or unexported fields
}
Tree is a radix tree for route matching
func (*Tree) AllowedMethods ¶ added in v0.2.0
AllowedMethods returns all HTTP methods registered for a path
func (*Tree) Insert ¶ added in v0.2.0
func (t *Tree) Insert(method, path string, handler HandlerFunc) error
Insert adds a route to the tree
func (*Tree) InsertWithName ¶ added in v0.2.0
func (t *Tree) InsertWithName(method, path string, handler HandlerFunc, name string) error
InsertWithName adds a named route to the tree
func (*Tree) Match ¶ added in v0.2.0
func (t *Tree) Match(method, path string) *MatchResult
Match finds a route for the given method and path
type VelocityRouter ¶
type VelocityRouter = VelocityRouterV2
VelocityRouter is an alias for backward compatibility
type VelocityRouterV2 ¶ added in v0.2.0
type VelocityRouterV2 struct {
// contains filtered or unexported fields
}
VelocityRouterV2 is the tree-based router implementation This replaces gorilla/mux with a custom radix tree
func Get ¶
func Get() *VelocityRouterV2
Get returns the global router instance, creating it if necessary
func NewV2 ¶ added in v0.2.0
func NewV2() *VelocityRouterV2
NewV2 creates a new tree-based router instance
func (*VelocityRouterV2) Any ¶ added in v0.2.0
func (r *VelocityRouterV2) Any(path string, handler HandlerFunc) RouteConfig
Any registers a route that matches any HTTP method
func (*VelocityRouterV2) Delete ¶ added in v0.2.0
func (r *VelocityRouterV2) Delete(path string, handler HandlerFunc) RouteConfig
Delete registers a DELETE route
func (*VelocityRouterV2) Get ¶ added in v0.2.0
func (r *VelocityRouterV2) Get(path string, handler HandlerFunc) RouteConfig
Get registers a GET route
func (*VelocityRouterV2) Group ¶ added in v0.2.0
func (r *VelocityRouterV2) Group(prefix string, fn ...func(Router)) Router
Group creates a new router group with a prefix
func (*VelocityRouterV2) Handle ¶ added in v0.2.0
func (r *VelocityRouterV2) Handle() http.Handler
Handle returns the underlying http.Handler
func (*VelocityRouterV2) Head ¶ added in v0.2.0
func (r *VelocityRouterV2) Head(path string, handler HandlerFunc) RouteConfig
Head registers a HEAD route
func (*VelocityRouterV2) Match ¶ added in v0.2.0
func (r *VelocityRouterV2) Match(methods []string, path string, handler HandlerFunc) RouteConfig
Match registers a route for specific HTTP methods
func (*VelocityRouterV2) Options ¶ added in v0.2.0
func (r *VelocityRouterV2) Options(path string, handler HandlerFunc) RouteConfig
Options registers an OPTIONS route
func (*VelocityRouterV2) Patch ¶ added in v0.2.0
func (r *VelocityRouterV2) Patch(path string, handler HandlerFunc) RouteConfig
Patch registers a PATCH route
func (*VelocityRouterV2) Post ¶ added in v0.2.0
func (r *VelocityRouterV2) Post(path string, handler HandlerFunc) RouteConfig
Post registers a POST route
func (*VelocityRouterV2) Prefix ¶ added in v0.2.0
func (r *VelocityRouterV2) Prefix(prefix string)
Prefix sets a prefix for all routes
func (*VelocityRouterV2) Put ¶ added in v0.2.0
func (r *VelocityRouterV2) Put(path string, handler HandlerFunc) RouteConfig
Put registers a PUT route
func (*VelocityRouterV2) Resource ¶ added in v0.2.0
func (r *VelocityRouterV2) Resource(path string, controller interface{}) ResourceRoute
Resource creates RESTful routes for a controller
func (*VelocityRouterV2) RouteURL ¶ added in v0.2.0
RouteURL generates a URL for a named route with the given parameters
func (*VelocityRouterV2) ServeHTTP ¶ added in v0.2.0
func (r *VelocityRouterV2) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements http.Handler interface
func (*VelocityRouterV2) Static ¶ added in v0.2.0
func (r *VelocityRouterV2) Static(directory string)
Static serves static files from the specified directory
func (*VelocityRouterV2) Use ¶ added in v0.2.0
func (r *VelocityRouterV2) Use(middlewares ...MiddlewareFunc) Router
Use adds middleware to the router