Documentation
¶
Overview ¶
Package router provides HTTP routing functionality for the Velocity framework. It's built on top of gorilla/mux and provides automatic route discovery, RESTful resources, and a clean API for web applications.
Index ¶
- func CurrentRoute(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 Route(name string, params map[string]string) (string, error)
- 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 HandlerFunc
- type MiddlewareFunc
- type RegistrationFunc
- type ResourceRoute
- type RouteConfig
- type RouteNotFoundError
- type Router
- type VelocityRouter
- func (r *VelocityRouter) Delete(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Get(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Group(prefix string) Router
- func (r *VelocityRouter) Handle() http.Handler
- func (r *VelocityRouter) Head(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Options(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Patch(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Post(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Prefix(prefix string)
- func (r *VelocityRouter) Put(path string, handler HandlerFunc) RouteConfig
- func (r *VelocityRouter) Resource(path string, controller interface{}) ResourceRoute
- func (r *VelocityRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *VelocityRouter) Static(directory string)
- func (r *VelocityRouter) 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 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 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 (*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 HandlerFunc ¶
HandlerFunc is the Velocity handler function signature
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc is the Velocity middleware function signature
type RegistrationFunc ¶
type RegistrationFunc func(Router)
RegistrationFunc is a function that registers routes
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 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(prefix string) 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 VelocityRouter ¶
type VelocityRouter struct {
// contains filtered or unexported fields
}
VelocityRouter is the main router implementation using gorilla/mux
func Get ¶
func Get() *VelocityRouter
Get returns the global router instance, creating it if necessary
func (*VelocityRouter) Delete ¶
func (r *VelocityRouter) Delete(path string, handler HandlerFunc) RouteConfig
Delete registers a DELETE route
func (*VelocityRouter) Get ¶
func (r *VelocityRouter) Get(path string, handler HandlerFunc) RouteConfig
Get registers a GET route
func (*VelocityRouter) Group ¶
func (r *VelocityRouter) Group(prefix string) Router
Group creates a new router group with a prefix
func (*VelocityRouter) Handle ¶
func (r *VelocityRouter) Handle() http.Handler
Handle returns the underlying http.Handler
func (*VelocityRouter) Head ¶
func (r *VelocityRouter) Head(path string, handler HandlerFunc) RouteConfig
Head registers a HEAD route
func (*VelocityRouter) Options ¶
func (r *VelocityRouter) Options(path string, handler HandlerFunc) RouteConfig
Options registers an OPTIONS route
func (*VelocityRouter) Patch ¶
func (r *VelocityRouter) Patch(path string, handler HandlerFunc) RouteConfig
Patch registers a PATCH route
func (*VelocityRouter) Post ¶
func (r *VelocityRouter) Post(path string, handler HandlerFunc) RouteConfig
Post registers a POST route
func (*VelocityRouter) Prefix ¶
func (r *VelocityRouter) Prefix(prefix string)
Prefix sets a prefix for all routes in this router
func (*VelocityRouter) Put ¶
func (r *VelocityRouter) Put(path string, handler HandlerFunc) RouteConfig
Put registers a PUT route
func (*VelocityRouter) Resource ¶
func (r *VelocityRouter) Resource(path string, controller interface{}) ResourceRoute
Resource creates RESTful routes for a controller
func (*VelocityRouter) ServeHTTP ¶
func (r *VelocityRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements http.Handler interface
func (*VelocityRouter) Static ¶
func (r *VelocityRouter) Static(directory string)
Static serves static files from the specified directory Files are served at the root level (e.g., /logo.png serves from public/logo.png)
func (*VelocityRouter) Use ¶
func (r *VelocityRouter) Use(middlewares ...MiddlewareFunc) Router
Use adds middleware to the router/group