Documentation ¶
Overview ¶
Package waitress is an HTTP framework for Go.
The framework and the sub-packages attempt to stay close to net/http but provide abstractions over actions that are performed frequently.
Index ¶
- func RedirectTo(path string) http.Handler
- func RedirectToWithCode(path string, code int) http.Handler
- type Application
- func (app *Application) Dispatch(w http.ResponseWriter, r *http.Request)
- func (app *Application) Mount(prefix, name string, fragment *Fragment) error
- func (app *Application) Recover(w http.ResponseWriter, r *http.Request)
- func (app *Application) Route(path, name string, methods []string) error
- func (app *Application) Run()
- func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Context
- func (ctx *Context) Abort(code int) http.Handler
- func (ctx *Context) Build(method, name string) *router.Builder
- func (ctx *Context) ByteHandler(b []byte) http.Handler
- func (ctx *Context) DecodeJSON(i interface{}) error
- func (ctx *Context) EncodeJSON(i interface{}) ([]byte, error)
- func (ctx *Context) Header(key, value string)
- func (ctx *Context) QueryInt(key string, def int64, base, size int) (int64, error)
- func (ctx *Context) QueryInt64(key string, def int64) (int64, error)
- func (ctx *Context) Redirect(builder *router.Builder) http.Handler
- func (ctx *Context) RedirectTo(path string) http.Handler
- func (ctx *Context) RedirectToWithCode(path string, code int) http.Handler
- func (ctx *Context) RedirectWithCode(builder *router.Builder, code int) http.Handler
- func (ctx *Context) Status(code int)
- func (ctx *Context) WriteJSON(b []byte) http.Handler
- type ErrorResponse
- func BadGateway() *ErrorResponse
- func BadRequest() *ErrorResponse
- func Conflict() *ErrorResponse
- func ExpectationFailed() *ErrorResponse
- func Forbidden() *ErrorResponse
- func GatewayTimeout() *ErrorResponse
- func Gone() *ErrorResponse
- func InternalServerError() *ErrorResponse
- func LengthRequired() *ErrorResponse
- func NotAcceptable() *ErrorResponse
- func NotFound() *ErrorResponse
- func NotImplemented() *ErrorResponse
- func PreconditionFailed() *ErrorResponse
- func RequestEntityTooLarge() *ErrorResponse
- func RequestTimeout() *ErrorResponse
- func RequestURITooLong() *ErrorResponse
- func RequestedRangeNotSatisfiable() *ErrorResponse
- func ServiceUnavailable() *ErrorResponse
- func TooManyRequests() *ErrorResponse
- func Unauthorized() *ErrorResponse
- func UnprocessableEntity() *ErrorResponse
- func UnsupportedMediaType() *ErrorResponse
- type Fragment
- type MethodNotAllowedResponse
- type ResponseWriter
- type Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RedirectTo ¶
RedirectTo constructs an http.Handler from a path.
Types ¶
type Application ¶
type Application struct { *middleware.Builder // The middleware builder is embedded for its methods. *Router // The router is embedded for its methods. // contains filtered or unexported fields }
Application holds the top level configuration.
func New ¶
func New(ctx interface{}) *Application
New returns a new Application preconfigured with some sane defaults.
func (*Application) Dispatch ¶
func (app *Application) Dispatch(w http.ResponseWriter, r *http.Request)
Dispatch kicks off the middleware processing for each request.
func (*Application) Mount ¶
func (app *Application) Mount(prefix, name string, fragment *Fragment) error
Mount registers a Fragment with the Application at a defined prefix.
func (*Application) Recover ¶
func (app *Application) Recover(w http.ResponseWriter, r *http.Request)
Recover will dump a stack trace and process the InternalServerErrorHandler on panic.
func (*Application) Route ¶
func (app *Application) Route(path, name string, methods []string) error
Route registers a route with the application context.
func (*Application) Run ¶
func (app *Application) Run()
Run will serve the application on localhost:3000 for now. Of course, this Application object is just another http.Handler, so you can ListenAndServe on your own.
func (*Application) ServeHTTP ¶
func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface.
type Context ¶
type Context struct { Response *ResponseWriter Request *http.Request // contains filtered or unexported fields }
Context exists as a collection of endpoint helper methods. You have full access to the http.ResponseWriter and http.Request as you would with the standard library.
func NewContext ¶
NewContext returns a NewContext bound to the provided parameters.
func (*Context) Abort ¶
Abort returns an http.Handler for HTTP status codes greater than 400. Some error codes have not been implemented.
func (*Context) ByteHandler ¶
ByteHandler returns an http.Handler that writes the provided bytes.
func (*Context) DecodeJSON ¶
DecodeJSON decodes the request body into a struct. Provide a pointer to a struct as you would with the encoding/json package.
func (*Context) EncodeJSON ¶
EncodeJSON marshals the provided structure into a byte slice.
func (*Context) QueryInt ¶
QueryInt converts a request query parameter to an integer. This is a convenience method for the common scenario of accepting query parameters to modify result sets, like pagination.
func (*Context) QueryInt64 ¶
QueryInt64 is like QueryInt but specifies base 10 and 64 bit result.
func (*Context) Redirect ¶
Redirect will perform an HTTP 303 redirect to the endpoint built by the provided Builder.
func (*Context) RedirectTo ¶
RedirectTo will perform an HTTP 303 redirect to the provided endpoint. You should provide an absolute URI here.
func (*Context) RedirectToWithCode ¶
RedirectToWithCode is like RedirectTo but you provide the HTTP status code to be used for redirection.
func (*Context) RedirectWithCode ¶
RedirectWithCode is like Redirect but you provide the HTTP status code to be used for redirection.
type ErrorResponse ¶
type ErrorResponse struct { Code int `json:"-"` Name string `json:"name"` Message string `json:"message"` }
ErrorResponse is an error that can be written as JSON.
func RequestEntityTooLarge ¶
func RequestEntityTooLarge() *ErrorResponse
HTTP 413 Request Entity Too Large
func RequestedRangeNotSatisfiable ¶
func RequestedRangeNotSatisfiable() *ErrorResponse
HTTP 416 Requested Range Not Satisfiable
func UnsupportedMediaType ¶
func UnsupportedMediaType() *ErrorResponse
HTTP 415 Unsupported Media Type
func (*ErrorResponse) Error ¶
func (e *ErrorResponse) Error() string
Error implements the error interface and returns the error message.
func (*ErrorResponse) ServeHTTP ¶
func (e *ErrorResponse) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface.
type Fragment ¶
type Fragment struct {
// contains filtered or unexported fields
}
A Fragment is a mountable application that records a series of actions and bindings to apply.
type MethodNotAllowedResponse ¶
type MethodNotAllowedResponse struct { *ErrorResponse Allowed []string }
MethodNotAllowedResponse is a special ErrorResponse that provides a list of allowed methods.
func MethodNotAllowed ¶
func MethodNotAllowed(allowed []string) *MethodNotAllowedResponse
HTTP 405 Method Not Allowed
func (*MethodNotAllowedResponse) ServeHTTP ¶
func (e *MethodNotAllowedResponse) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface.
type ResponseWriter ¶
type ResponseWriter struct { http.ResponseWriter // The http.ResponseWriter to be written to. // contains filtered or unexported fields }
ResponseWriter is a wrapper around http.ResponseWriter. Currently, this is only used so that the response status code can be specified before writing the header.
func NewResponseWriter ¶
func NewResponseWriter(w http.ResponseWriter) *ResponseWriter
NewResponseWriter returns a new ResponseWriter.
func (*ResponseWriter) Write ¶
func (w *ResponseWriter) Write(b []byte) (int, error)
Write will write the bytes to the ResponseWriter.
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(code int)
WriteHeader records the response as written and delegates the header writing to the embedded ResponseWriter.
type Router ¶
type Router struct { *router.Router // The waitress/router Router is embedded for its methods. // contains filtered or unexported fields }
A Router is a wrapper over the waitress/router package.
func (*Router) Dispatch ¶
func (r *Router) Dispatch(ctx *Context) router.DispatchFunc
Dispatch returns DispatchFunc that waitress/router expects. The DispatchFunc performs route matching and constructs the context for the endpoint's method receiver.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP implements the http.Handler interface. It binds the router to the current request, creates the context, and dispatches to the DispatchFunc. The response can be a byte slice, a string, an http.Handler, or any function with the method signature of an http.HandlerFunc. If the return value is anything else, the InternalServerErrorHandler will be invoked.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package middleware implements a middleware stack inspired by Rack.
|
Package middleware implements a middleware stack inspired by Rack. |
Package router implements a flexible HTTP routing package.
|
Package router implements a flexible HTTP routing package. |