Documentation ¶
Overview ¶
Package router is a lightweight yet powerful HTTP router. It's built on top of the Golang HTTP package and uses the radix tree to provide routing requirements for modern applications.
Index ¶
- type Context
- type DefaultContext
- func (d *DefaultContext) Bytes(status int, body []byte) error
- func (d *DefaultContext) Empty(status int) error
- func (d *DefaultContext) File(status int, contentType, path string) error
- func (d *DefaultContext) HTML(status int, body string) error
- func (d *DefaultContext) HasParameter(name string) bool
- func (d *DefaultContext) JSON(status int, body interface{}) error
- func (d *DefaultContext) Parameter(name string) string
- func (d *DefaultContext) Parameters() map[string]string
- func (d *DefaultContext) PrettyJSON(status int, body interface{}) error
- func (d *DefaultContext) PrettyXML(status int, body interface{}) error
- func (d *DefaultContext) Redirect(status int, url string) error
- func (d *DefaultContext) Request() *http.Request
- func (d *DefaultContext) Response() http.ResponseWriter
- func (d *DefaultContext) Route() *Route
- func (d *DefaultContext) Text(status int, body string) error
- func (d *DefaultContext) URL(route string, parameters map[string]string) string
- func (d *DefaultContext) XML(status int, body interface{}) error
- type Handler
- type Middleware
- type Route
- type Router
- func (r Router) AddMiddleware(middleware Middleware)
- func (r Router) AddMiddlewares(middlewares []Middleware)
- func (r Router) AddPrefix(prefix string)
- func (r Router) Any(path string, handler Handler) *Route
- func (r Router) DELETE(path string, handler Handler) *Route
- func (r Router) Define(parameter, pattern string)
- func (r Router) Files(path, directory string) *Route
- func (r Router) GET(path string, handler Handler) *Route
- func (r Router) Group(prefix string, middleware []Middleware, body func())
- func (r Router) HEAD(path string, handler Handler) *Route
- func (r Router) Map(method, path string, handler Handler) *Route
- func (r Router) OPTIONS(path string, handler Handler) *Route
- func (r Router) PATCH(path string, handler Handler) *Route
- func (r Router) POST(path string, handler Handler) *Route
- func (r Router) PUT(path string, handler Handler) *Route
- func (r Router) Serve(rw http.ResponseWriter, request *http.Request)
- func (r Router) SetNotFoundHandler(handler Handler)
- func (r Router) Start(address string) error
- func (r Router) WithMiddleware(middleware Middleware, body func())
- func (r Router) WithMiddlewares(middleware []Middleware, body func())
- func (r Router) WithPrefix(prefix string, body func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface { // Route returns the dispatched Route Route() *Route // Request returns the HTTP request. Request() *http.Request // Response return the HTTP responseWriter. Response() http.ResponseWriter // Parameters returns Route parameters. Parameters() map[string]string // Parameter returns a router parameter by name. Parameter(name string) string // HasParameter checks if router parameter exists. HasParameter(name string) bool // URL generates a URL for given route name and actual parameters. // It returns an empty string if it cannot find any route. URL(route string, parameters map[string]string) string // Bytes creates and sends a custom HTTP response. Bytes(status int, body []byte) error // Empty creates and sends an HTTP empty response. Empty(status int) error // Redirect creates and sends an HTTP redirection response. Redirect(status int, url string) error // Text creates and sends an HTTP text response. Text(status int, body string) error // HTML creates and sends an HTTP HTML response. HTML(status int, body string) error // JSON creates and sends an HTTP JSON response. JSON(status int, body interface{}) error // PrettyJSON creates and sends an HTTP JSON (with indents) response. PrettyJSON(status int, body interface{}) error // XML creates and sends an HTTP XML response. XML(status int, body interface{}) error // PrettyXML creates and sends an HTTP XML (with indents) response. PrettyXML(status int, body interface{}) error // File creates and sends an HTTP response that contains a file. File(status int, contentType, path string) error }
Context holds the HTTP request, the HTTP responseWriter, the Route, and the Route parameters.
type DefaultContext ¶
type DefaultContext struct {
// contains filtered or unexported fields
}
DefaultContext is the default implementation of Context interface.
func (*DefaultContext) Bytes ¶
func (d *DefaultContext) Bytes(status int, body []byte) error
Bytes creates and sends a custom HTTP response.
func (*DefaultContext) Empty ¶
func (d *DefaultContext) Empty(status int) error
Empty creates and sends an HTTP empty response.
func (*DefaultContext) File ¶ added in v1.2.0
func (d *DefaultContext) File(status int, contentType, path string) error
File creates and sends an HTTP response that contains a file.
func (*DefaultContext) HTML ¶
func (d *DefaultContext) HTML(status int, body string) error
HTML creates and sends an HTTP HTML response.
func (*DefaultContext) HasParameter ¶
func (d *DefaultContext) HasParameter(name string) bool
HasParameter checks if router parameter exists.
func (*DefaultContext) JSON ¶
func (d *DefaultContext) JSON(status int, body interface{}) error
JSON creates and sends an HTTP JSON response.
func (*DefaultContext) Parameter ¶
func (d *DefaultContext) Parameter(name string) string
Parameter returns a router parameter by name.
func (*DefaultContext) Parameters ¶
func (d *DefaultContext) Parameters() map[string]string
Parameters returns Route parameters.
func (*DefaultContext) PrettyJSON ¶
func (d *DefaultContext) PrettyJSON(status int, body interface{}) error
PrettyJSON creates and sends an HTTP JSON (with indents) response.
func (*DefaultContext) PrettyXML ¶
func (d *DefaultContext) PrettyXML(status int, body interface{}) error
PrettyXML creates and sends an HTTP XML (with indents) response.
func (*DefaultContext) Redirect ¶
func (d *DefaultContext) Redirect(status int, url string) error
Redirect creates and sends an HTTP redirection response.
func (*DefaultContext) Request ¶
func (d *DefaultContext) Request() *http.Request
Request returns the HTTP request.
func (*DefaultContext) Response ¶
func (d *DefaultContext) Response() http.ResponseWriter
Response return the HTTP responseWriter.
func (*DefaultContext) Route ¶
func (d *DefaultContext) Route() *Route
Route returns the dispatched Route
func (*DefaultContext) Text ¶
func (d *DefaultContext) Text(status int, body string) error
Text creates and sends an HTTP text response.
func (*DefaultContext) URL ¶ added in v1.1.0
func (d *DefaultContext) URL(route string, parameters map[string]string) string
URL generates a URL for given route name and actual parameters. It returns an empty string if it cannot find any route.
func (*DefaultContext) XML ¶
func (d *DefaultContext) XML(status int, body interface{}) error
XML creates and sends an HTTP XML response.
type Handler ¶
Handler is an interface for Route handlers (controllers). When the router finds a Route for the incoming HTTP request, it calls the Route's handler.
type Middleware ¶
Middleware is an interface for Route middlewares. It returns a Handler that receives HTTP Context to watch or manipulate and calls the next middlewares/handler.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route holds Route information.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is the entry point of the package. It gets routes, Route parameter patterns, and middlewares then dispatches them. It receives HTTP requests, finds the related Route then runs the Route handler through its middlewares.
func (Router) AddMiddleware ¶
func (r Router) AddMiddleware(middleware Middleware)
AddMiddleware adds a global middlewares for next or all routes.
func (Router) AddMiddlewares ¶
func (r Router) AddMiddlewares(middlewares []Middleware)
AddMiddlewares adds set of global middlewares for next or all routes.
func (Router) Define ¶
Define assigns a regular expression pattern to a Route parameter. After the definition, the router only dispatches the related Route if the request URI matches the pattern.
func (Router) Files ¶ added in v1.2.1
Files defines a new static file server on the given path (URI) for the given directory root. The path (URI) must end with `*` to cover all the existing files and subdirectories.
func (Router) Group ¶
func (r Router) Group(prefix string, middleware []Middleware, body func())
Group creates a group of routes with common attributes. Currently, content and middlewares attributes are supported.
func (Router) Map ¶
Map defines a new Route by HTTP method and path and assigns a handler. The path (URI) may contain Route parameters.
func (Router) Serve ¶
func (r Router) Serve(rw http.ResponseWriter, request *http.Request)
Serve handles the request manually with a given request and a response writer.
func (Router) SetNotFoundHandler ¶
SetNotFoundHandler receives a handler and runs it when user request won't lead to any declared Route. It is the application 404 error handler, indeed.
func (Router) Start ¶
Start runs the HTTP listener and waits for HTTP requests. It should be called after definitions of routes.
func (Router) WithMiddleware ¶
func (r Router) WithMiddleware(middleware Middleware, body func())
WithMiddleware creates a group of routes with common middlewares.
func (Router) WithMiddlewares ¶
func (r Router) WithMiddlewares(middleware []Middleware, body func())
WithMiddlewares creates a group of routes with common set of middlewares.
func (Router) WithPrefix ¶
WithPrefix creates a group of routes with common content.