Documentation
¶
Index ¶
- Constants
- func BuildMiddlewareChain(handler http.Handler, middlewares []MiddlewareInterface) http.Handler
- func BuildMiddlewareChainFromSlices(handler http.Handler, middlewareSlices ...[]MiddlewareInterface) http.Handler
- func CSSResponse(w http.ResponseWriter, r *http.Request, body string)
- func ExecuteMiddlewareChain(middlewares []MiddlewareInterface, finalHandler http.Handler) http.Handler
- func GetMiddlewareName(middleware StdMiddleware) string
- func GetParam(r *http.Request, name string) (string, bool)
- func GetParams(r *http.Request) map[string]string
- func GetRouteMiddlewareNames(route RouteInterface) []string
- func HTMLResponse(w http.ResponseWriter, r *http.Request, body string)
- func JSONResponse(w http.ResponseWriter, r *http.Request, body string)
- func JSResponse(w http.ResponseWriter, r *http.Request, body string)
- func MustGetParam(r *http.Request, name string) string
- func TextResponse(w http.ResponseWriter, r *http.Request, body string)
- func XMLResponse(w http.ResponseWriter, r *http.Request, body string)
- type CSSHandler
- type Domain
- type DomainInterface
- type ErrorHandler
- type Group
- type GroupInterface
- type HTMLHandler
- type HandlerRegistry
- func (r *HandlerRegistry) AddMiddleware(middleware MiddlewareInterface)
- func (r *HandlerRegistry) AddRoute(route RouteInterface)
- func (r *HandlerRegistry) FindMiddleware(name string) MiddlewareInterface
- func (r *HandlerRegistry) FindRoute(name string) RouteInterface
- func (r *HandlerRegistry) RemoveMiddleware(name string)
- func (r *HandlerRegistry) RemoveRoute(name string)
- type ItemInterface
- type JSHandler
- type JSONHandler
- type Middleware
- type MiddlewareInfo
- type MiddlewareInterface
- func AddMiddlewaresToInterfaces(interfaces []MiddlewareInterface, middlewares []StdMiddleware) []MiddlewareInterface
- func MiddlewareFromFunction(handler StdMiddleware) MiddlewareInterface
- func MiddlewaresToInterfaces(middlewares []StdMiddleware) []MiddlewareInterface
- func NewAnonymousMiddleware(handler StdMiddleware) MiddlewareInterface
- func NewMiddleware(opts ...MiddlewareOption) MiddlewareInterface
- type MiddlewareOption
- type NameInterface
- type Route
- type RouteInterface
- func Delete(path string, handler StdHandler) RouteInterface
- func Get(path string, handler StdHandler) RouteInterface
- func GetCSS(path string, handler CSSHandler) RouteInterface
- func GetHTML(path string, handler HTMLHandler) RouteInterface
- func GetJSON(path string, handler JSONHandler) RouteInterface
- func GetText(path string, handler TextHandler) RouteInterface
- func GetXML(path string, handler XMLHandler) RouteInterface
- func NewRoute() RouteInterface
- func Post(path string, handler StdHandler) RouteInterface
- func PostHTML(path string, handler HTMLHandler) RouteInterface
- func PostJSON(path string, handler JSONHandler) RouteInterface
- func Put(path string, handler StdHandler) RouteInterface
- type RouterInterface
- type StatusInterface
- type StdHandler
- type StdMiddleware
- type StringHandler
- type TextHandler
- type XMLHandler
Constants ¶
const ( StatusEnabled = "enabled" StatusDisabled = "disabled" )
Status constants for declarative configuration
const ( TypeRoute = "route" TypeGroup = "group" TypeDomain = "domain" TypeMiddleware = "middleware" )
Type constants for declarative configuration
const ( MethodGET = "GET" MethodPOST = "POST" MethodPUT = "PUT" MethodPATCH = "PATCH" MethodDELETE = "DELETE" MethodHEAD = "HEAD" MethodOPTIONS = "OPTIONS" )
HTTP method constants for declarative configuration
const ExecutionSequenceKey contextKey = "rtr.execution.sequence"
ExecutionSequenceKey is used to track the execution sequence of middlewares in tests
const ParamsKey contextKey = "rtr.path.params"
ParamsKey is the key used to store path parameters in the request context Using a more specific key to avoid collisions with other packages ParamsKey is the key used to store path parameters in the request context Using a more specific key to avoid collisions with other packages
Variables ¶
This section is empty.
Functions ¶
func BuildMiddlewareChain ¶ added in v0.9.0
func BuildMiddlewareChain(handler http.Handler, middlewares []MiddlewareInterface) http.Handler
BuildMiddlewareChain builds a middleware chain from a single slice of middlewares. The middlewares are applied in order, with the first middleware in the slice being the outermost.
func BuildMiddlewareChainFromSlices ¶ added in v0.9.0
func BuildMiddlewareChainFromSlices(handler http.Handler, middlewareSlices ...[]MiddlewareInterface) http.Handler
BuildMiddlewareChainFromSlices builds a middleware chain from multiple slices of middlewares. The slices are processed in reverse order, with the first slice's middlewares being the outermost. Within each slice, middlewares are applied in reverse order to maintain the correct execution sequence.
func CSSResponse ¶ added in v0.4.0
func CSSResponse(w http.ResponseWriter, r *http.Request, body string)
CSSResponse responds with CSS content and sets the appropriate Content-Type header. It sets the Content-Type to "text/css".
func ExecuteMiddlewareChain ¶ added in v0.6.0
func ExecuteMiddlewareChain(middlewares []MiddlewareInterface, finalHandler http.Handler) http.Handler
ExecuteMiddlewareChain executes a chain of MiddlewareInterface in order. This is a helper function that applies all middleware in the slice to the final handler.
func GetMiddlewareName ¶ added in v0.4.0
func GetMiddlewareName(middleware StdMiddleware) string
GetMiddlewareName attempts to get a readable name for a middleware function
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 GetRouteMiddlewareNames ¶ added in v0.4.0
func GetRouteMiddlewareNames(route RouteInterface) []string
GetRouteMiddlewareNames gets middleware names for a route
func HTMLResponse ¶ added in v0.4.0
func HTMLResponse(w http.ResponseWriter, r *http.Request, body string)
HTMLResponse responds with HTML content and sets the appropriate Content-Type header. It sets the Content-Type to "text/html; charset=utf-8" if not already set.
func JSONResponse ¶ added in v0.4.0
func JSONResponse(w http.ResponseWriter, r *http.Request, body string)
JSONResponse responds with JSON content and sets the appropriate Content-Type header. It sets the Content-Type to "application/json".
func JSResponse ¶ added in v0.4.0
func JSResponse(w http.ResponseWriter, r *http.Request, body string)
JSResponse responds with JavaScript content and sets the appropriate Content-Type header. It sets the Content-Type to "application/javascript".
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 TextResponse ¶ added in v0.4.0
func TextResponse(w http.ResponseWriter, r *http.Request, body string)
TextResponse responds with plain text content and sets the appropriate Content-Type header. It sets the Content-Type to "text/plain; charset=utf-8".
func XMLResponse ¶ added in v0.4.0
func XMLResponse(w http.ResponseWriter, r *http.Request, body string)
XMLResponse responds with XML content and sets the appropriate Content-Type header. It sets the Content-Type to "application/xml".
Types ¶
type CSSHandler ¶ added in v0.4.0
type CSSHandler StringHandler
CSSHandler is a convenience shorthand handler that returns CSS content. Automatically sets Content-Type: "text/css" header. Returns a CSS string that will be wrapped with CSSResponse().
type Domain ¶ added in v0.3.0
type Domain struct { Status string `json:"status,omitempty"` Hosts []string `json:"hosts,omitempty"` Items []ItemInterface `json:"items,omitempty"` // Can contain both Groups and Routes in sequence Middlewares []string `json:"middlewares,omitempty"` Name string `json:"name,omitempty"` }
Domain represents a domain-specific routing configuration
func (Domain) GetMiddlewares ¶ added in v0.6.0
GetMiddlewares implements ItemInterface
func (Domain) MarshalJSON ¶ added in v0.7.0
MarshalJSON implements the json.Marshaler interface for Domain
func (*Domain) UnmarshalJSON ¶ added in v0.7.0
UnmarshalJSON implements the json.Unmarshaler interface for Domain
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 []MiddlewareInterface) DomainInterface // GetBeforeMiddlewares returns all middleware functions that will be executed before any route handler in this domain GetBeforeMiddlewares() []MiddlewareInterface // AddAfterMiddlewares adds middleware functions to be executed after any route handler in this domain // Returns the domain for method chaining AddAfterMiddlewares(middleware []MiddlewareInterface) DomainInterface // GetAfterMiddlewares returns all middleware functions that will be executed after any route handler in this domain GetAfterMiddlewares() []MiddlewareInterface // 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 ErrorHandler ¶ added in v0.4.0
type ErrorHandler func(http.ResponseWriter, *http.Request) error
ErrorHandler is a convenience shorthand handler for error responses. Returns an error that will be handled appropriately. If the returned error is nil, it means no error occurred and the response is successful. Content-Type and status codes are left to specific extensions like 404ErrorHandler.
type Group ¶ added in v0.3.0
type Group struct { Status string `json:"status,omitempty"` Prefix string `json:"prefix,omitempty"` Routes []Route `json:"routes,omitempty"` Middlewares []string `json:"middlewares,omitempty"` Name string `json:"name,omitempty"` }
Group represents a group of routes
func (Group) GetMiddlewares ¶ added in v0.6.0
GetMiddlewares implements ItemInterface
func (Group) MarshalJSON ¶ added in v0.7.0
MarshalJSON implements the json.Marshaler interface for Group
func (*Group) UnmarshalJSON ¶ added in v0.7.0
UnmarshalJSON implements the json.Unmarshaler interface for Group
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 to be executed before any route handler in this group. // Returns the group for method chaining. AddBeforeMiddlewares(middleware []MiddlewareInterface) GroupInterface // GetBeforeMiddlewares returns all middleware that will be executed before any route handler in this group. GetBeforeMiddlewares() []MiddlewareInterface // AddAfterMiddlewares adds middleware to be executed after any route handler in this group // Returns the group for method chaining AddAfterMiddlewares(middleware []MiddlewareInterface) GroupInterface // GetAfterMiddlewares returns all middleware that will be executed after any route handler in this group GetAfterMiddlewares() []MiddlewareInterface }
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 HTMLHandler ¶ added in v0.4.0
type HTMLHandler StringHandler
HTMLHandler is a convenience shorthand handler that returns HTML content. Automatically sets Content-Type: "text/html; charset=utf-8" header. Returns an HTML string that will be wrapped with HTMLResponse().
type HandlerRegistry ¶ added in v0.6.0
type HandlerRegistry struct {
// contains filtered or unexported fields
}
HandlerRegistry maps string names to actual functions
func NewHandlerRegistry ¶ added in v0.6.0
func NewHandlerRegistry() *HandlerRegistry
NewHandlerRegistry creates a new handler registry
func (*HandlerRegistry) AddMiddleware ¶ added in v0.6.0
func (r *HandlerRegistry) AddMiddleware(middleware MiddlewareInterface)
AddMiddleware adds middleware to the registry
func (*HandlerRegistry) AddRoute ¶ added in v0.6.0
func (r *HandlerRegistry) AddRoute(route RouteInterface)
AddRoute adds a route to the registry
func (*HandlerRegistry) FindMiddleware ¶ added in v0.6.0
func (r *HandlerRegistry) FindMiddleware(name string) MiddlewareInterface
FindMiddleware finds middleware by name
func (*HandlerRegistry) FindRoute ¶ added in v0.6.0
func (r *HandlerRegistry) FindRoute(name string) RouteInterface
FindRoute finds a route by name
func (*HandlerRegistry) RemoveMiddleware ¶ added in v0.6.0
func (r *HandlerRegistry) RemoveMiddleware(name string)
RemoveMiddleware removes a middleware factory from the registry
func (*HandlerRegistry) RemoveRoute ¶ added in v0.6.0
func (r *HandlerRegistry) RemoveRoute(name string)
RemoveRoute removes a route handler from the registry
type ItemInterface ¶ added in v0.6.0
type ItemInterface interface { NameInterface StatusInterface GetMiddlewares() []string }
ItemInterface represents a common interface for groups and routes
type JSHandler ¶ added in v0.4.0
type JSHandler StringHandler
JSHandler is a convenience shorthand handler that returns JavaScript content. Automatically sets Content-Type: "application/javascript" header. Returns a JavaScript string that will be wrapped with JSResponse().
type JSONHandler ¶ added in v0.4.0
type JSONHandler StringHandler
JSONHandler is a convenience shorthand handler that returns JSON content. Automatically sets Content-Type: "application/json" header. Returns a JSON string that will be wrapped with JSONResponse().
type Middleware ¶
Middleware represents middleware configuration
type MiddlewareInfo ¶ added in v0.2.0
type MiddlewareInfo struct { Name string Func StdMiddleware }
MiddlewareInfo represents middleware information for display purposes
type MiddlewareInterface ¶ added in v0.6.0
type MiddlewareInterface interface { // GetName returns the name identifier associated with this middleware. // Returns the string name of the middleware, which may be empty for anonymous middleware. GetName() string // SetName sets the name identifier for this middleware and returns the middleware for method chaining. // The name parameter can be used for middleware identification and debugging. SetName(name string) MiddlewareInterface // GetHandler returns the underlying middleware function. // Returns the StdMiddleware function that will be executed. GetHandler() StdMiddleware // SetHandler sets the middleware function and returns the middleware for method chaining. // The handler parameter should be a valid StdMiddleware function. SetHandler(handler StdMiddleware) MiddlewareInterface // Execute applies the middleware to the given handler. // This is equivalent to calling GetHandler()(next). Execute(next http.Handler) http.Handler }
MiddlewareInterface defines the interface for named middleware. It provides a way to associate a name with middleware for better debugging and documentation.
func AddMiddlewaresToInterfaces ¶ added in v0.6.0
func AddMiddlewaresToInterfaces(interfaces []MiddlewareInterface, middlewares []StdMiddleware) []MiddlewareInterface
AddMiddlewaresToInterfaces converts and adds StdMiddleware functions to a MiddlewareInterface slice
func MiddlewareFromFunction ¶ added in v0.6.0
func MiddlewareFromFunction(handler StdMiddleware) MiddlewareInterface
MiddlewareFromFunction converts a StdMiddleware function to a MiddlewareInterface. This is a convenience function for backward compatibility.
func MiddlewaresToInterfaces ¶ added in v0.6.0
func MiddlewaresToInterfaces(middlewares []StdMiddleware) []MiddlewareInterface
MiddlewaresToInterfaces converts a slice of StdMiddleware functions to MiddlewareInterface slice. This is useful for migrating existing code that uses []StdMiddleware to []MiddlewareInterface.
func NewAnonymousMiddleware ¶ added in v0.6.0
func NewAnonymousMiddleware(handler StdMiddleware) MiddlewareInterface
NewAnonymousMiddleware creates a new middleware with the given handler. This is a convenience function that's equivalent to NewMiddleware(WithHandler(handler)). It's maintained for backward compatibility with existing code.
func NewMiddleware ¶ added in v0.6.0
func NewMiddleware(opts ...MiddlewareOption) MiddlewareInterface
NewMiddleware creates a new middleware with the provided options. Example: NewMiddleware(WithName("auth"), WithHandler(myAuthHandler))
type MiddlewareOption ¶ added in v0.7.0
type MiddlewareOption func(*middlewareImpl)
MiddlewareOption defines a function type that can configure a middleware
func WithHandler ¶ added in v0.7.0
func WithHandler(handler StdMiddleware) MiddlewareOption
WithHandler returns a MiddlewareOption that sets the handler function of the middleware
func WithName ¶ added in v0.7.0
func WithName(name string) MiddlewareOption
WithName returns a MiddlewareOption that sets the name of the middleware
type NameInterface ¶ added in v0.6.0
type NameInterface interface {
GetName() string
}
type Route ¶ added in v0.6.0
type Route struct { Status string `json:"status,omitempty"` Path string `json:"path,omitempty"` Method string `json:"method,omitempty"` Handler string `json:"handler,omitempty"` // Standard HTTP handler reference HTMLHandler string `json:"htmlHandler,omitempty"` // HTML handler reference JSONHandler string `json:"jsonHandler,omitempty"` // JSON handler reference CSSHandler string `json:"cssHandler,omitempty"` // CSS handler reference XMLHandler string `json:"xmlHandler,omitempty"` // XML handler reference TextHandler string `json:"textHandler,omitempty"` // Text handler reference JSHandler string `json:"jsHandler,omitempty"` // JavaScript handler reference ErrorHandler string `json:"errorHandler,omitempty"` // Error handler reference Name string `json:"name,omitempty"` Middlewares []string `json:"middlewares,omitempty"` }
Route represents a single route configuration
func (Route) GetMiddlewares ¶ added in v0.6.0
GetMiddlewares implements ItemInterface
func (Route) MarshalJSON ¶ added in v0.7.0
MarshalJSON implements the json.Marshaler interface for Route
func (*Route) UnmarshalJSON ¶ added in v0.7.0
UnmarshalJSON implements the json.Unmarshaler interface for Route
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() StdHandler // SetHandler sets the handler function for this route and returns the route for method chaining. SetHandler(handler StdHandler) RouteInterface // GetStringHandler returns the string handler function associated with this route. GetStringHandler() StringHandler // SetStringHandler sets the string handler function for this route and returns the route for method chaining. SetStringHandler(handler StringHandler) RouteInterface // GetHTMLHandler returns the HTML handler function associated with this route. GetHTMLHandler() HTMLHandler // SetHTMLHandler sets the HTML handler function for this route and returns the route for method chaining. SetHTMLHandler(handler HTMLHandler) RouteInterface // GetJSONHandler returns the JSON handler function associated with this route. GetJSONHandler() JSONHandler // SetJSONHandler sets the JSON handler function for this route and returns the route for method chaining. SetJSONHandler(handler JSONHandler) RouteInterface // GetCSSHandler returns the CSS handler function associated with this route. GetCSSHandler() CSSHandler // SetCSSHandler sets the CSS handler function for this route and returns the route for method chaining. SetCSSHandler(handler CSSHandler) RouteInterface // GetXMLHandler returns the XML handler function associated with this route. GetXMLHandler() XMLHandler // SetXMLHandler sets the XML handler function for this route and returns the route for method chaining. SetXMLHandler(handler XMLHandler) RouteInterface // GetTextHandler returns the text handler function associated with this route. GetTextHandler() TextHandler // SetTextHandler sets the text handler function for this route and returns the route for method chaining. SetTextHandler(handler TextHandler) RouteInterface // GetJSHandler returns the JavaScript handler function associated with this route. GetJSHandler() JSHandler // SetJSHandler sets the JavaScript handler function for this route and returns the route for method chaining. SetJSHandler(handler JSHandler) RouteInterface // GetErrorHandler returns the error handler function associated with this route. GetErrorHandler() ErrorHandler // SetErrorHandler sets the error handler function for this route and returns the route for method chaining. SetErrorHandler(handler ErrorHandler) 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 []MiddlewareInterface) RouteInterface // GetBeforeMiddlewares returns all middleware functions that will be executed before the route handler. GetBeforeMiddlewares() []MiddlewareInterface // AddAfterMiddlewares adds middleware functions to be executed after the route handler. // Returns the route for method chaining. AddAfterMiddlewares(middleware []MiddlewareInterface) RouteInterface // GetAfterMiddlewares returns all middleware functions that will be executed after the route handler. GetAfterMiddlewares() []MiddlewareInterface }
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 StdHandler) 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 StdHandler) 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 GetCSS ¶ added in v0.4.0
func GetCSS(path string, handler CSSHandler) RouteInterface
GetCSS creates a new GET route with the given path and CSS handler It is a shortcut method that combines setting the method to GET, path, and CSS handler.
func GetHTML ¶ added in v0.4.0
func GetHTML(path string, handler HTMLHandler) RouteInterface
GetHTML creates a new GET route with the given path and HTML handler It is a shortcut method that combines setting the method to GET, path, and HTML handler.
func GetJSON ¶ added in v0.4.0
func GetJSON(path string, handler JSONHandler) RouteInterface
GetJSON creates a new GET route with the given path and JSON handler It is a shortcut method that combines setting the method to GET, path, and JSON handler.
func GetText ¶ added in v0.4.0
func GetText(path string, handler TextHandler) RouteInterface
GetText creates a new GET route with the given path and text handler It is a shortcut method that combines setting the method to GET, path, and text handler.
func GetXML ¶ added in v0.4.0
func GetXML(path string, handler XMLHandler) RouteInterface
GetXML creates a new GET route with the given path and XML handler It is a shortcut method that combines setting the method to GET, path, and XML 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 StdHandler) 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 PostHTML ¶ added in v0.4.0
func PostHTML(path string, handler HTMLHandler) RouteInterface
PostHTML creates a new POST route with the given path and HTML handler It is a shortcut method that combines setting the method to POST, path, and HTML handler.
func PostJSON ¶ added in v0.4.0
func PostJSON(path string, handler JSONHandler) RouteInterface
PostJSON creates a new POST route with the given path and JSON handler It is a shortcut method that combines setting the method to POST, path, and JSON handler.
func Put ¶
func Put(path string, handler StdHandler) 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 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 to be executed before any route handler. // The middleware will be executed in the order they are added. // Returns the router for method chaining. AddBeforeMiddlewares(middleware []MiddlewareInterface) RouterInterface // GetBeforeMiddlewares returns all middleware that will be executed before any route handler. // Returns a slice of MiddlewareInterface. GetBeforeMiddlewares() []MiddlewareInterface // AddAfterMiddlewares adds middleware to be executed after any route handler. // The middleware will be executed in reverse order of how they were added. // Returns the router for method chaining. AddAfterMiddlewares(middleware []MiddlewareInterface) RouterInterface // GetAfterMiddlewares returns all middleware that will be executed after any route handler. // Returns a slice of MiddlewareInterface. GetAfterMiddlewares() []MiddlewareInterface // 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. The router starts with no default middlewares - users should add middlewares as needed.
type StatusInterface ¶ added in v0.6.0
type StatusInterface interface {
GetStatus() string
}
type StdHandler ¶ added in v0.6.0
type StdHandler func(http.ResponseWriter, *http.Request)
StdHandler defines the function signature for standard HTTP request handlers. This is the standard Go HTTP handler pattern, distinct from any potential named handler interfaces.
func ErrorHandlerToHandler ¶ added in v0.4.0
func ErrorHandlerToHandler(handler ErrorHandler) StdHandler
ErrorHandlerToHandler converts an ErrorHandler to a standard Handler. If the error handler returns an error, it writes the error message to the response. If the error handler returns nil, it does nothing.
Parameters:
- handler: The error handler function to convert.
Returns:
- A standard Handler function that writes the error message to the response if an error is returned.
func ToStdHandler ¶ added in v0.6.0
func ToStdHandler(handler StringHandler) StdHandler
ToStdHandler converts any string-returning handler to a standard Handler. It simply writes the returned string to the response without setting any headers. The string handler is responsible for setting any headers it needs.
Parameters:
- handler: The string handler function to convert.
Returns:
- A standard Handler function that writes the returned string to the response.
type StdMiddleware ¶ added in v0.6.0
StdMiddleware represents a standard middleware function. It is a function type that takes an http.Handler and returns an http.Handler. StdMiddleware functions can be used to process requests before or after they reach the main handler. This is the standard Go HTTP middleware pattern, distinct from MiddlewareInterface which provides named middleware.
func InterfacesToMiddlewares ¶ added in v0.6.0
func InterfacesToMiddlewares(interfaces []MiddlewareInterface) []StdMiddleware
InterfacesToMiddlewares converts a slice of MiddlewareInterface to StdMiddleware functions. This is useful for backward compatibility when you need to work with the underlying functions.
type StringHandler ¶ added in v0.4.0
type StringHandler func(http.ResponseWriter, *http.Request) string
StringHandler is a convenience shorthand handler for simple string responses. It returns a string that will be written directly to the response without setting any headers. The handler is responsible for setting any headers it needs.
type TextHandler ¶ added in v0.4.0
type TextHandler StringHandler
TextHandler is a convenience shorthand handler that returns plain text content. Automatically sets Content-Type: "text/plain; charset=utf-8" header. Returns a plain text string that will be wrapped with TextResponse().
type XMLHandler ¶ added in v0.4.0
type XMLHandler StringHandler
XMLHandler is a convenience shorthand handler that returns XML content. Automatically sets Content-Type: "application/xml" header. Returns an XML string that will be wrapped with XMLResponse().
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
basic
command
|
|
declarative
command
|
|
domain
command
|
|
handlers
command
|
|
middleware
command
|
|
path-parameters
command
|
|