Documentation
¶
Overview ¶
Package velocity is a high-performance HTTP router and web framework for Go. It features flexible routing, middleware support, and automatic handling of OPTIONS/WebSocket requests.
Basic Usage:
app := velocity.New() router := app.Router("/api") // Add a route router.Get("/users/:id").Handle(func(w http.ResponseWriter, r *http.Request) { params := velocity.GetParams(r) userID := params["id"] // ... handle request }) // Start server app.Listen(8080)
Groups and Middleware:
// Create a group with middleware authMiddleware := func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // ... auth logic next(w, r) } } api := router.Group("/v1", authMiddleware) api.Get("/protected").Handle(handler)
Configuration:
// Configure app options app := velocity.New(velocity.AppConfig{ AllowTrace: true, }) // Configure server with TLS app.Listen(443, velocity.ServerConfig{ CertFile: "cert.pem", KeyFile: "key.pem", })
Available HTTP Methods:
- GET (also handles HEAD requests)
- POST
- PUT
- PATCH
- DELETE
- WebSocket (automatic upgrade detection)
- OPTIONS (automatic handling)
Features:
- Fast routing with radix tree
- Path parameters (/users/:id)
- Middleware support (global and per-route)
- Automatic HEAD and OPTIONS handling
- WebSocket support
- HTTP/2 support with TLS
- Custom 404 and 405 handlers
Index ¶
- func GetParams(r *http.Request) map[string]string
- type App
- func (a *App) Listen(port int, cfg ...ServerConfig) error
- func (a *App) NotAllowed(h http.HandlerFunc)
- func (a *App) NotFound(h http.HandlerFunc)
- func (a *App) Router(path string, mws ...Middleware) *Router
- func (a *App) Routes(print ...bool) []string
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type AppConfig
- type Middleware
- type Router
- func (r *Router) Delete(p string, mws ...Middleware) route
- func (r *Router) Get(p string, mws ...Middleware) route
- func (r *Router) Group(path string, mws ...Middleware) *Router
- func (r *Router) Patch(p string, mws ...Middleware) route
- func (r *Router) Post(p string, mws ...Middleware) route
- func (r *Router) Put(p string, mws ...Middleware) route
- func (r *Router) Websocket(p string, mws ...Middleware) route
- type ServerConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the main router instance that implements http.Handler.
func New ¶
New creates a new App instance with optional configuration.
Example:
app := velocity.New() // or with config app := velocity.New(velocity.AppConfig{AllowTrace: true})
func (*App) Listen ¶
func (a *App) Listen(port int, cfg ...ServerConfig) error
Listen starts the HTTP server on the specified port with optional configuration. The server will use the following defaults if not specified:
- ReadTimeout: 0 (no timeout)
- WriteTimeout: 0 (no timeout)
- IdleTimeout: 0 (no timeout)
Example:
// Basic usage app.Listen(8080) // With timeouts app.Listen(8080, ServerConfig{ ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, }) // With TLS app.Listen(443, ServerConfig{ CertFile: "cert.pem", KeyFile: "key.pem", ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, })
func (*App) NotAllowed ¶
func (a *App) NotAllowed(h http.HandlerFunc)
NotAllowed sets a custom handler for method not allowed responses (405).
func (*App) NotFound ¶
func (a *App) NotFound(h http.HandlerFunc)
NotFound sets a custom handler for not found responses (404).
func (*App) Router ¶
func (a *App) Router(path string, mws ...Middleware) *Router
Router creates a new router group with the given path prefix and optional middleware.
Example:
router := app.Router("/api", authMiddleware)
type AppConfig ¶
type AppConfig struct { // AllowTrace enables automatic handling of TRACE requests AllowTrace bool }
AppConfig holds configuration options for the App.
type Middleware ¶
type Middleware func(next http.HandlerFunc) http.HandlerFunc
Middleware represents a function that wraps an http.HandlerFunc to provide additional functionality before and/or after the handler execution.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router represents a group of routes with a common path prefix and middleware.
func (*Router) Delete ¶
func (r *Router) Delete(p string, mws ...Middleware) route
Delete registers a new DELETE route with the given path and optional middleware.
func (*Router) Get ¶
func (r *Router) Get(p string, mws ...Middleware) route
Get registers a new GET route with the given path and optional middleware.
func (*Router) Group ¶
func (r *Router) Group(path string, mws ...Middleware) *Router
Group creates a new router group with additional path prefix and optional middleware.
Example:
api := router.Group("/v1", authMiddleware)
func (*Router) Patch ¶
func (r *Router) Patch(p string, mws ...Middleware) route
Patch registers a new PATCH route with the given path and optional middleware.
func (*Router) Post ¶
func (r *Router) Post(p string, mws ...Middleware) route
Post registers a new POST route with the given path and optional middleware.
func (*Router) Put ¶
func (r *Router) Put(p string, mws ...Middleware) route
Put registers a new PUT route with the given path and optional middleware.
func (*Router) Websocket ¶
func (r *Router) Websocket(p string, mws ...Middleware) route
Websocket registers a new WebSocket route with the given path and optional middleware.
type ServerConfig ¶
type ServerConfig struct { // Addr specifies the TCP address for the server to listen on Addr string // TLSConfig provides configuration for TLS connections TLSConfig *tls.Config // CertFile and KeyFile are paths to TLS certificate and key files CertFile string KeyFile string // ReadTimeout is the maximum duration for reading the entire request, including the body. // A zero or negative value means there will be no timeout. // Default: 0 (no timeout) ReadTimeout time.Duration // WriteTimeout is the maximum duration before timing out writes of the response. // A zero or negative value means there will be no timeout. // Default: 0 (no timeout) WriteTimeout time.Duration // IdleTimeout is the maximum amount of time to wait for the next request. // If IdleTimeout is zero, the value of ReadTimeout is used. // If both are zero, there is no timeout. // Default: 0 (no timeout) IdleTimeout time.Duration }
ServerConfig provides TLS and server address configuration.
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
Package middleware provides common HTTP middleware functions for the velocity router.
|
Package middleware provides common HTTP middleware functions for the velocity router. |