Documentation
¶
Index ¶
- Constants
- func GetParam(r *http.Request, name string) (string, bool)
- func GetParams(r *http.Request) map[string]string
- func MustGetParam(r *http.Request, name string) string
- func RecoveryMiddleware(next http.Handler) http.Handler
- type DomainConfig
- type DomainInterface
- type GroupConfig
- type GroupInterface
- type Handler
- type Middleware
- type MiddlewareInfo
- type RouteConfig
- func DELETE(path string, handler Handler) RouteConfig
- func GET(path string, handler Handler) RouteConfig
- func OPTIONS(path string, handler Handler) RouteConfig
- func PATCH(path string, handler Handler) RouteConfig
- func POST(path string, handler Handler) RouteConfig
- func PUT(path string, handler Handler) RouteConfig
- type RouteInterface
- type RouterConfig
- type RouterInterface
Constants ¶
const (
// ParamsKey is the key used to store path parameters in the request context
ParamsKey contextKey = "params"
)
Context key for storing path parameters in the request context
Variables ¶
This section is empty.
Functions ¶
func GetParam ¶ added in v0.1.3
GetParam retrieves a path parameter from the request context by name. Returns the parameter value and true if found, or an empty string and false otherwise.
func GetParams ¶ added in v0.1.3
GetParams returns all path parameters as a map. Returns an empty map if no parameters exist.
func MustGetParam ¶ added in v0.1.3
MustGetParam retrieves a path parameter from the request context by name. Panics if the parameter is not found. Use only when you're certain the parameter exists.
func RecoveryMiddleware ¶
RecoveryMiddleware creates a new middleware that recovers from panics. It logs the panic details and returns a 500 Internal Server Error response. This should typically be added as one of the first middlewares in the chain.
Types ¶
type DomainConfig ¶ added in v0.3.0
type DomainConfig struct {
Name string `json:"name,omitempty"`
Patterns []string `json:"patterns"`
Routes []RouteConfig `json:"routes,omitempty"`
Groups []GroupConfig `json:"groups,omitempty"`
BeforeMiddleware []Middleware `json:"-"`
AfterMiddleware []Middleware `json:"-"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
DomainConfig represents a declarative domain configuration
func Domain ¶ added in v0.3.0
func Domain(patterns []string, items ...interface{}) DomainConfig
Domain creates a domain configuration
type DomainInterface ¶
type DomainInterface interface {
// GetPatterns returns the domain patterns that this domain matches against
GetPatterns() []string
// SetPatterns sets the domain patterns for this domain and returns the domain for method chaining
SetPatterns(patterns ...string) DomainInterface
// AddRoute adds a route to this domain and returns the domain for method chaining
AddRoute(route RouteInterface) DomainInterface
// AddRoutes adds multiple routes to this domain and returns the domain for method chaining
AddRoutes(routes []RouteInterface) DomainInterface
// GetRoutes returns all routes that belong to this domain
GetRoutes() []RouteInterface
// AddGroup adds a group to this domain and returns the domain for method chaining
AddGroup(group GroupInterface) DomainInterface
// AddGroups adds multiple groups to this domain and returns the domain for method chaining
AddGroups(groups []GroupInterface) DomainInterface
// GetGroups returns all groups that belong to this domain
GetGroups() []GroupInterface
// AddBeforeMiddlewares adds middleware functions to be executed before any route handler in this domain
// Returns the domain for method chaining
AddBeforeMiddlewares(middleware []Middleware) DomainInterface
// GetBeforeMiddlewares returns all middleware functions that will be executed before any route handler in this domain
GetBeforeMiddlewares() []Middleware
// AddAfterMiddlewares adds middleware functions to be executed after any route handler in this domain
// Returns the domain for method chaining
AddAfterMiddlewares(middleware []Middleware) DomainInterface
// GetAfterMiddlewares returns all middleware functions that will be executed after any route handler in this domain
GetAfterMiddlewares() []Middleware
// Match checks if the given host matches any of this domain's patterns
Match(host string) bool
}
DomainInterface defines the interface for a domain that can have routes and groups. A domain represents a collection of routes and groups that are only accessible when the request's Host header matches the domain's patterns.
func NewDomain ¶
func NewDomain(patterns ...string) DomainInterface
NewDomain creates a new domain with the given patterns
type GroupConfig ¶ added in v0.3.0
type GroupConfig struct {
Name string `json:"name,omitempty"`
Prefix string `json:"prefix"`
Routes []RouteConfig `json:"routes,omitempty"`
Groups []GroupConfig `json:"groups,omitempty"`
BeforeMiddleware []Middleware `json:"-"`
AfterMiddleware []Middleware `json:"-"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
GroupConfig represents a declarative group configuration
func Group ¶ added in v0.3.0
func Group(prefix string, items ...interface{}) GroupConfig
Group creates a group configuration
func (GroupConfig) WithAfterMiddleware ¶ added in v0.3.0
func (g GroupConfig) WithAfterMiddleware(middleware ...Middleware) GroupConfig
WithAfterMiddleware adds after middleware to a group configuration
func (GroupConfig) WithBeforeMiddleware ¶ added in v0.3.0
func (g GroupConfig) WithBeforeMiddleware(middleware ...Middleware) GroupConfig
WithBeforeMiddleware adds before middleware to a group configuration
func (GroupConfig) WithName ¶ added in v0.3.0
func (g GroupConfig) WithName(name string) GroupConfig
WithName adds a name to a group configuration
type GroupInterface ¶
type GroupInterface interface {
// GetPrefix returns the URL path prefix associated with this group.
GetPrefix() string
// SetPrefix sets the URL path prefix for this group and returns the group for method chaining.
SetPrefix(prefix string) GroupInterface
// AddRoute adds a single route to this group and returns the group for method chaining.
AddRoute(route RouteInterface) GroupInterface
// AddRoutes adds multiple routes to this group and returns the group for method chaining.
AddRoutes(routes []RouteInterface) GroupInterface
// GetRoutes returns all routes that belong to this group.
GetRoutes() []RouteInterface
// AddGroup adds a single nested group to this group and returns the group for method chaining.
AddGroup(group GroupInterface) GroupInterface
// AddGroups adds multiple nested groups to this group and returns the group for method chaining.
AddGroups(groups []GroupInterface) GroupInterface
// GetGroups returns all nested groups that belong to this group.
GetGroups() []GroupInterface
// AddBeforeMiddlewares adds middleware functions to be executed before any route handler in this group.
// Returns the group for method chaining.
AddBeforeMiddlewares(middleware []Middleware) GroupInterface
// GetBeforeMiddlewares returns all middleware functions that will be executed before any route handler in this group.
GetBeforeMiddlewares() []Middleware
// AddAfterMiddlewares adds middleware functions to be executed after any route handler in this group.
// Returns the group for method chaining.
AddAfterMiddlewares(middleware []Middleware) GroupInterface
// GetAfterMiddlewares returns all middleware functions that will be executed after any route handler in this group.
GetAfterMiddlewares() []Middleware
}
GroupInterface defines the interface for a group of routes. A group represents a collection of routes that share common properties such as a URL prefix and middleware. Groups can also be nested to create hierarchical route structures.
func NewGroup ¶
func NewGroup() GroupInterface
NewGroup creates and returns a new GroupInterface implementation. This is used to create a new route group that can be added to a router.
type Handler ¶
type Handler func(http.ResponseWriter, *http.Request)
Handler defines the function signature for HTTP request handlers.
type Middleware ¶
Middleware represents a middleware function. It is a function type that takes an http.Handler and returns an http.Handler. Middleware functions can be used to process requests before or after they reach the main handler.
func DefaultMiddlewares ¶
func DefaultMiddlewares() []Middleware
DefaultMiddlewares returns a slice of default middlewares that should be used with the router. Currently, it only includes the RecoveryMiddleware.
type MiddlewareInfo ¶ added in v0.2.0
type MiddlewareInfo struct {
Name string
Func Middleware
}
MiddlewareInfo represents middleware information for display purposes
type RouteConfig ¶ added in v0.3.0
type RouteConfig struct {
Name string `json:"name,omitempty"`
Method string `json:"method,omitempty"`
Path string `json:"path"`
Handler Handler `json:"-"`
BeforeMiddleware []Middleware `json:"-"`
AfterMiddleware []Middleware `json:"-"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
RouteConfig represents a declarative route configuration
func DELETE ¶ added in v0.3.0
func DELETE(path string, handler Handler) RouteConfig
DELETE creates a DELETE route configuration
func GET ¶ added in v0.3.0
func GET(path string, handler Handler) RouteConfig
GET creates a GET route configuration
func OPTIONS ¶ added in v0.3.0
func OPTIONS(path string, handler Handler) RouteConfig
OPTIONS creates an OPTIONS route configuration
func PATCH ¶ added in v0.3.0
func PATCH(path string, handler Handler) RouteConfig
PATCH creates a PATCH route configuration
func POST ¶ added in v0.3.0
func POST(path string, handler Handler) RouteConfig
POST creates a POST route configuration
func PUT ¶ added in v0.3.0
func PUT(path string, handler Handler) RouteConfig
PUT creates a PUT route configuration
func (RouteConfig) WithAfterMiddleware ¶ added in v0.3.0
func (r RouteConfig) WithAfterMiddleware(middleware ...Middleware) RouteConfig
WithAfterMiddleware adds after middleware to a route configuration
func (RouteConfig) WithBeforeMiddleware ¶ added in v0.3.0
func (r RouteConfig) WithBeforeMiddleware(middleware ...Middleware) RouteConfig
WithBeforeMiddleware adds before middleware to a route configuration
func (RouteConfig) WithMetadata ¶ added in v0.3.0
func (r RouteConfig) WithMetadata(key string, value interface{}) RouteConfig
WithMetadata adds metadata to a route configuration
func (RouteConfig) WithName ¶ added in v0.3.0
func (r RouteConfig) WithName(name string) RouteConfig
WithName adds a name to a route configuration
type RouteInterface ¶
type RouteInterface interface {
// GetMethod returns the HTTP method associated with this route.
GetMethod() string
// SetMethod sets the HTTP method for this route and returns the route for method chaining.
SetMethod(method string) RouteInterface
// GetPath returns the URL path pattern associated with this route.
GetPath() string
// SetPath sets the URL path pattern for this route and returns the route for method chaining.
SetPath(path string) RouteInterface
// GetHandler returns the handler function associated with this route.
GetHandler() Handler
// SetHandler sets the handler function for this route and returns the route for method chaining.
SetHandler(handler Handler) RouteInterface
// GetName returns the name identifier associated with this route.
GetName() string
// SetName sets the name identifier for this route and returns the route for method chaining.
SetName(name string) RouteInterface
// AddBeforeMiddlewares adds middleware functions to be executed before the route handler.
// Returns the route for method chaining.
AddBeforeMiddlewares(middleware []Middleware) RouteInterface
// GetBeforeMiddlewares returns all middleware functions that will be executed before the route handler.
GetBeforeMiddlewares() []Middleware
// AddAfterMiddlewares adds middleware functions to be executed after the route handler.
// Returns the route for method chaining.
AddAfterMiddlewares(middleware []Middleware) RouteInterface
// GetAfterMiddlewares returns all middleware functions that will be executed after the route handler.
GetAfterMiddlewares() []Middleware
}
RouteInterface defines the interface for a single route definition. A route represents a mapping between an HTTP method, a URL path pattern, and a handler function. Routes can also have associated middleware that will be executed before or after the handler.
func Delete ¶
func Delete(path string, handler Handler) RouteInterface
Delete creates a new DELETE route with the given path and handler It is a shortcut method that combines setting the method to DELETE, path, and handler.
func Get ¶
func Get(path string, handler Handler) RouteInterface
Get creates a new GET route with the given path and handler It is a shortcut method that combines setting the method to GET, path, and handler.
func NewRoute ¶
func NewRoute() RouteInterface
This is used to create a new route that can be added to a router or group.
func Post ¶
func Post(path string, handler Handler) RouteInterface
Post creates a new POST route with the given path and handler It is a shortcut method that combines setting the method to POST, path, and handler.
func Put ¶
func Put(path string, handler Handler) RouteInterface
Put creates a new PUT route with the given path and handler It is a shortcut method that combines setting the method to PUT, path, and handler.
type RouterConfig ¶ added in v0.3.0
type RouterConfig struct {
Name string `json:"name,omitempty"`
Prefix string `json:"prefix,omitempty"`
Routes []RouteConfig `json:"routes,omitempty"`
Groups []GroupConfig `json:"groups,omitempty"`
Domains []DomainConfig `json:"domains,omitempty"`
BeforeMiddleware []Middleware `json:"-"`
AfterMiddleware []Middleware `json:"-"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
RouterConfig represents a complete declarative router configuration
type RouterInterface ¶
type RouterInterface interface {
// GetPrefix returns the URL path prefix associated with this router.
GetPrefix() string
// SetPrefix sets the URL path prefix for this router and returns the router for method chaining.
// The prefix will be prepended to all routes in this router.
SetPrefix(prefix string) RouterInterface
// AddGroup adds a single group to this router and returns the router for method chaining.
// The group's prefix will be combined with the router's prefix for all routes in the group.
AddGroup(group GroupInterface) RouterInterface
// AddGroups adds multiple groups to this router and returns the router for method chaining.
// Each group's prefix will be combined with the router's prefix for all routes in the group.
AddGroups(groups []GroupInterface) RouterInterface
// GetGroups returns all groups that belong to this router.
// Returns a slice of GroupInterface implementations.
GetGroups() []GroupInterface
// AddRoute adds a single route to this router and returns the router for method chaining.
// The route's path will be prefixed with the router's prefix.
AddRoute(route RouteInterface) RouterInterface
// AddRoutes adds multiple routes to this router and returns the router for method chaining.
// Each route's path will be prefixed with the router's prefix.
AddRoutes(routes []RouteInterface) RouterInterface
// GetRoutes returns all routes that belong to this router.
// Returns a slice of RouteInterface implementations.
GetRoutes() []RouteInterface
// AddBeforeMiddlewares adds middleware functions to be executed before any route handler.
// The middleware functions will be executed in the order they are added.
// Returns the router for method chaining.
AddBeforeMiddlewares(middleware []Middleware) RouterInterface
// GetBeforeMiddlewares returns all middleware functions that will be executed before any route handler.
// Returns a slice of Middleware functions.
GetBeforeMiddlewares() []Middleware
// AddAfterMiddlewares adds middleware functions to be executed after any route handler.
// The middleware functions will be executed in reverse order of how they were added.
// Returns the router for method chaining.
AddAfterMiddlewares(middleware []Middleware) RouterInterface
// GetAfterMiddlewares returns all middleware functions that will be executed after any route handler.
// Returns a slice of Middleware functions.
GetAfterMiddlewares() []Middleware
// AddDomain adds a domain to this router and returns the router for method chaining
AddDomain(domain DomainInterface) RouterInterface
// AddDomains adds multiple domains to this router and returns the router for method chaining
AddDomains(domains []DomainInterface) RouterInterface
// GetDomains returns all domains that belong to this router
GetDomains() []DomainInterface
// List displays the router's configuration in formatted tables for debugging and documentation
// Shows global middleware, domains, direct routes, and route groups
List()
// ServeHTTP implements the http.Handler interface.
// It matches the incoming request to the appropriate route and executes the handler.
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
RouterInterface defines the interface for a router that can handle HTTP requests. A router is responsible for matching incoming HTTP requests to the appropriate route handler and executing any associated middleware.
func NewRouter ¶
func NewRouter() RouterInterface
NewRouter creates and returns a new RouterInterface implementation. This is the main entry point for creating a new router. By default, it includes recovery middleware to handle panics.
func NewRouterFromConfig ¶ added in v0.3.0
func NewRouterFromConfig(config RouterConfig) RouterInterface
NewRouterFromConfig creates a router from a declarative configuration
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
declarative
command
|
|
|
domain
command
|
|
|
path-parameters
command
|