Documentation ¶
Overview ¶
Package router provides a router linking uris to handlers taking a context
Package router provides a router linking uris to handlers taking a context
Index ¶
- func Redirect(context Context, path string) error
- func RedirectExternal(context Context, path string) error
- func RedirectStatus(context Context, path string, status int) error
- type ConcreteContext
- func (c *ConcreteContext) Config(key string) string
- func (c *ConcreteContext) Get(key string) interface{}
- func (c *ConcreteContext) Header() http.Header
- func (c *ConcreteContext) Log(message string)
- func (c *ConcreteContext) Logf(format string, v ...interface{})
- func (c *ConcreteContext) Param(key string) string
- func (c *ConcreteContext) ParamFiles(key string) ([]*multipart.FileHeader, error)
- func (c *ConcreteContext) ParamInt(key string) int64
- func (c *ConcreteContext) Params() (Params, error)
- func (c *ConcreteContext) Path() string
- func (c *ConcreteContext) Production() bool
- func (c *ConcreteContext) RenderContext() map[string]interface{}
- func (c *ConcreteContext) Request() *http.Request
- func (c *ConcreteContext) Route() *Route
- func (c *ConcreteContext) Set(key string, data interface{})
- func (c *ConcreteContext) Write(b []byte) (int, error)
- func (c *ConcreteContext) WriteHeader(i int)
- func (c *ConcreteContext) Writer() http.ResponseWriter
- type Config
- type Context
- type ErrHandler
- type Handler
- type Logger
- type Params
- func (p Params) Add(key, value string)
- func (p Params) Blank(key string) bool
- func (p Params) Clean(accepted []string) map[string]string
- func (p Params) Flatten(k string) string
- func (p Params) Get(key string) string
- func (p Params) GetAll(key string) []string
- func (p Params) GetDate(key string, format string) (time.Time, error)
- func (p Params) GetFloat(key string) float64
- func (p Params) GetFloats(key string) []float64
- func (p Params) GetInt(key string) int64
- func (p Params) GetInts(key string) []int64
- func (p Params) GetIntsString(key string) string
- func (p Params) GetUniqueInts(key string) []int64
- func (p Params) Map() map[string]string
- func (p Params) Remove(key string)
- func (p Params) Set(key, value string)
- func (p Params) SetInt(key string, value int64)
- type Route
- func (r *Route) Accept(method string) *Route
- func (r *Route) Delete() *Route
- func (r *Route) Get() *Route
- func (r *Route) MatchMethod(method string) bool
- func (r *Route) MatchPath(path string) bool
- func (r *Route) Method(method string) *Route
- func (r *Route) Methods(permitted ...string) *Route
- func (r *Route) Parse(path string) map[string]string
- func (r *Route) Post() *Route
- func (r *Route) Put() *Route
- func (r *Route) String() string
- type Router
- func (r *Router) Add(pattern string, handler Handler) *Route
- func (r *Router) AddFilter(filter Handler)
- func (r *Router) AddFilterHandler(handler http.Handler)
- func (r *Router) AddFilterHandlerFunc(handler http.HandlerFunc)
- func (r *Router) AddRedirect(pattern string, redirectPath string, status int) *Route
- func (r *Router) Log(message string)
- func (r *Router) Logf(format string, v ...interface{})
- func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- type StatusError
- func BadRequestError(e error, args ...string) *StatusError
- func Error(e error, s int, t string, m string) *StatusError
- func InternalError(e error, args ...string) *StatusError
- func NotAuthorizedError(e error, args ...string) *StatusError
- func NotFoundError(e error, args ...string) *StatusError
- func ToStatusError(e error) *StatusError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Redirect ¶
Redirect uses status 302 StatusFound by default - this is not a permanent redirect We don't accept external or relative paths for security reasons
func RedirectExternal ¶
RedirectExternal redirects setting the status code (for example unauthorized), but does no checks on the path Use with caution and only on completely known paths.
Types ¶
type ConcreteContext ¶
type ConcreteContext struct {
// contains filtered or unexported fields
}
ConcreteContext is the request context, including a writer, the current request etc
func (*ConcreteContext) Config ¶
func (c *ConcreteContext) Config(key string) string
Config returns a key from the context config
func (*ConcreteContext) Get ¶
func (c *ConcreteContext) Get(key string) interface{}
Get retreives arbitrary data for this request
func (*ConcreteContext) Header ¶
func (c *ConcreteContext) Header() http.Header
Header calls our writer and returns the header map that will be sent by WriteHeader.
func (*ConcreteContext) Log ¶
func (c *ConcreteContext) Log(message string)
Log logs the given message using our logger
func (*ConcreteContext) Logf ¶
func (c *ConcreteContext) Logf(format string, v ...interface{})
Logf logs the given message and arguments using our logger
func (*ConcreteContext) Param ¶
func (c *ConcreteContext) Param(key string) string
Param retreives a single param value, ignoring multiple values This may trigger a parse of the request and route
func (*ConcreteContext) ParamFiles ¶
func (c *ConcreteContext) ParamFiles(key string) ([]*multipart.FileHeader, error)
ParamFiles parses the request as multipart, and then returns the file parts for this key NB it calls ParseMultipartForm prior to reading the parts
func (*ConcreteContext) ParamInt ¶
func (c *ConcreteContext) ParamInt(key string) int64
ParamInt retreives a single param value as int, ignoring multiple values This may trigger a parse of the request and route
func (*ConcreteContext) Params ¶
func (c *ConcreteContext) Params() (Params, error)
Params loads and return all the params from the request
func (*ConcreteContext) Path ¶
func (c *ConcreteContext) Path() string
Path returns the path for the request
func (*ConcreteContext) Production ¶
func (c *ConcreteContext) Production() bool
Production returns whether this context is running in production
func (*ConcreteContext) RenderContext ¶
func (c *ConcreteContext) RenderContext() map[string]interface{}
RenderContext returns a context for rendering the view
func (*ConcreteContext) Request ¶
func (c *ConcreteContext) Request() *http.Request
Request returns the current http Request
func (*ConcreteContext) Route ¶
func (c *ConcreteContext) Route() *Route
Route returns the route handling this request
func (*ConcreteContext) Set ¶
func (c *ConcreteContext) Set(key string, data interface{})
Set saves arbitrary data for this request
func (*ConcreteContext) Write ¶
func (c *ConcreteContext) Write(b []byte) (int, error)
Write calls our writer and writes the data to the connection as part of an HTTP reply.
func (*ConcreteContext) WriteHeader ¶
func (c *ConcreteContext) WriteHeader(i int)
WriteHeader calls our writer and sends an HTTP response header with status code.
func (*ConcreteContext) Writer ¶
func (c *ConcreteContext) Writer() http.ResponseWriter
Writer returns the http.ResponseWriter for responding to the request
type Context ¶
type Context interface { // Context acts as a facade on responseWriter http.ResponseWriter // Request returns the http.Request embedded in this context Request() *http.Request // Writer returns the http.ResponseWriter embedded in this context Writer() http.ResponseWriter // Request returns the cleaned path for this request Path() string // Route returns the route handling for this request Route() *Route // Config returns a key from the context config Config(key string) string // Production returns true if we are running in a production environment Production() bool // Params returns all params for a request Params() (Params, error) // Param returns a key from the request params Param(key string) string // ParamInt returns an int64 key from the request params ParamInt(key string) int64 // ParamFiles parses the request as multipart, and then returns the file parts for this key ParamFiles(key string) ([]*multipart.FileHeader, error) // Store arbitrary data for this request Set(key string, data interface{}) // Retreive arbitrary data for this request Get(key string) interface{} // Return the rendering context (our data) RenderContext() map[string]interface{} // Log a message Log(message string) // Log a format and arguments Logf(format string, v ...interface{}) }
Context is a request context wrapping a response writer and the request details
func NewContext ¶ added in v1.5.1
func NewContext(writer http.ResponseWriter, request *http.Request, route *Route, config Config, logger Logger) Context
NewContext returns a new context this should also be used in the Handle function and tests FIXME
type ErrHandler ¶
ErrHandler is used to render a router.Error - used by ErrorHandler on the router
type Handler ¶
Handler is our standard handler function, accepting a router.Context interface, and returning router.Error
type Logger ¶
type Logger interface {
Printf(format string, args ...interface{})
}
Logger Interface for a simple logger (the stdlib log pkg and the fragmenta log pkg conform)
type Params ¶
Params is similar to url.Values, but with a few more utility functions
func (Params) Add ¶
Add adds the value, if necessary appending to any existing values associated with key.
func (Params) Clean ¶ added in v1.3.3
Clean returns the params as a map[string]string, discarding any multiple values, with any params not in the accepted list removed
func (Params) Flatten ¶
Flatten deflates a set of params (of any type) to a comma separated list (only for simple params)
func (Params) Get ¶
Get gets the first value associated with the given key. If there are no values returns the empty string.
func (Params) GetAll ¶
GetAll returns all values associated with the given key - equivalent to params[key].
func (Params) GetDate ¶
GetDate returns the first value associated with a given key as a time, using the given time format.
func (Params) GetFloat ¶ added in v1.3.3
GetFloat returns the first value associated with the given key as an integer. If there is no value or a parse error, it returns 0.0
func (Params) GetFloats ¶ added in v1.3.3
GetFloats returns all values associated with the given key as an array of floats.
func (Params) GetInt ¶
GetInt returns the first value associated with the given key as an integer. If there is no value or a parse error, it returns 0 If the string contains non-numeric characters, they are first stripped
func (Params) GetInts ¶
GetInts returns all values associated with the given key as an array of integers.
func (Params) GetIntsString ¶
GetIntsString returns all values associated with the given key as a comma separated string
func (Params) GetUniqueInts ¶
GetUniqueInts returns all unique non-zero int values associated with the given key as an array of integers
func (Params) Map ¶
Map gets the params as a flat map[string]string, discarding any multiple values.
type Route ¶
type Route struct { // An HTTP handler which accepts a context Handler Handler // If the route is simply a string we match against that Pattern string // Up to three letters to match (before any regexp) for fast decisions on matches PatternShort string // If the route is a regexp, we match that instead (this may have groups etc) Regexp *regexp.Regexp // Param names taken from the Pattern and matching params ParamNames []string // Redirect path - used to redirect if handler is nil RedirectPath string // Redirect status - used to redirect if handler is nil RedirectStatus int // contains filtered or unexported fields }
Route stores information to match a request and build URLs.
func (*Route) MatchMethod ¶
MatchMethod returns true if our list of methods contains method
type Router ¶
type Router struct { // File handler (sends files) FileHandler Handler // Error handler (renders errors) ErrorHandler ErrHandler // The logger passed to actions within the context on each request Logger Logger // The server config passed to actions within the context on each request Config Config // contains filtered or unexported fields }
Router stores and handles the routes
func (*Router) AddFilter ¶
AddFilter adds a new filter to our list of filters to execute before request handlers
func (*Router) AddFilterHandler ¶
AddFilterHandler adds a standard http.Handler to filters wrapped in a ContextHandler
func (*Router) AddFilterHandlerFunc ¶
func (r *Router) AddFilterHandlerFunc(handler http.HandlerFunc)
AddFilterHandlerFunc adds a standard http.HandlerFunc to filters wrapped in a ContextHandler
func (*Router) AddRedirect ¶
AddRedirect adds a new redirect this is just a route with a redirect path set
type StatusError ¶
StatusError wraps a std error and stores more information (status code, display title/msg and caller info)
func BadRequestError ¶
func BadRequestError(e error, args ...string) *StatusError
BadRequestError returns a new StatusError with Status StatusBadRequest and optional Title and Message
func Error ¶
func Error(e error, s int, t string, m string) *StatusError
Error returns a new StatusError with code StatusInternalServerError and a generic message
func InternalError ¶
func InternalError(e error, args ...string) *StatusError
InternalError returns a new StatusError with Status StatusInternalServerError and optional Title and Message Usage: return router.InternalError(err)
func NotAuthorizedError ¶
func NotAuthorizedError(e error, args ...string) *StatusError
NotAuthorizedError returns a new StatusError with Status StatusUnauthorized and optional Title and Message
func NotFoundError ¶
func NotFoundError(e error, args ...string) *StatusError
NotFoundError returns a new StatusError with Status StatusNotFound and optional Title and Message Usage return router.NotFoundError(err,"Optional Title", "Optional user-friendly Message")
func ToStatusError ¶
func ToStatusError(e error) *StatusError
ToStatusError returns a *StatusError or wraps a standard error in a 500 StatusError
func (*StatusError) Error ¶
func (e *StatusError) Error() string
Error returns the underling error string - it should not be shown in production
func (*StatusError) FileLine ¶
func (e *StatusError) FileLine() string
FileLine returns file name and line of error
func (*StatusError) String ¶
func (e *StatusError) String() string
String returns a string represenation of this error, useful for debugging