waitress

package module
v0.0.0-...-d43fb72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2014 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Waitress
========

Waitress is a web framework with a focus on embracing the Go standard library.

Read a short piece on why this framework exists:
http://pnelson.ca/blog/building-waitress/

Copyright (c) 2014 by Philip Nelson. See LICENSE for details.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func RedirectTo

func RedirectTo(path string) http.Handler

RedirectTo constructs an http.Handler from a path.

func RedirectToWithCode

func RedirectToWithCode(path string, code int) http.Handler

RedirectToWithCode constructs an http.Handler from a path and status code.

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

func NewContext(w *ResponseWriter, r *http.Request, adapter *router.Adapter) *Context

NewContext returns a NewContext bound to the provided parameters.

func (*Context) Abort

func (ctx *Context) Abort(code int) http.Handler

Abort returns an http.Handler for HTTP status codes greater than 400. Some error codes have not been implemented.

func (*Context) Build

func (ctx *Context) Build(method, name string) *router.Builder

Build returns a Builder from the adapter preconfigured for the request.

func (*Context) ByteHandler

func (ctx *Context) ByteHandler(b []byte) http.Handler

ByteHandler returns an http.Handler that writes the provided bytes.

func (*Context) DecodeJSON

func (ctx *Context) DecodeJSON(i interface{}) error

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

func (ctx *Context) EncodeJSON(i interface{}) ([]byte, error)

EncodeJSON marshals the provided structure into a byte slice.

func (*Context) Header

func (ctx *Context) Header(key, value string)

Header is a shortcut to setting a response header.

func (*Context) QueryInt

func (ctx *Context) QueryInt(key string, def int64, base, size int) (int64, error)

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

func (ctx *Context) QueryInt64(key string, def int64) (int64, error)

QueryInt64 is like QueryInt but specifies base 10 and 64 bit result.

func (*Context) Redirect

func (ctx *Context) Redirect(builder *router.Builder) http.Handler

Redirect will perform an HTTP 303 redirect to the endpoint built by the provided Builder.

func (*Context) RedirectTo

func (ctx *Context) RedirectTo(path string) http.Handler

RedirectTo will perform an HTTP 303 redirect to the provided endpoint. You should provide an absolute URI here.

func (*Context) RedirectToWithCode

func (ctx *Context) RedirectToWithCode(path string, code int) http.Handler

RedirectToWithCode is like RedirectTo but you provide the HTTP status code to be used for redirection.

func (*Context) RedirectWithCode

func (ctx *Context) RedirectWithCode(builder *router.Builder, code int) http.Handler

RedirectWithCode is like Redirect but you provide the HTTP status code to be used for redirection.

func (*Context) Status

func (ctx *Context) Status(code int)

Status configures the response status code to write to the header. The response header will not be written by calling this method.

func (*Context) WriteJSON

func (ctx *Context) WriteJSON(b []byte) http.Handler

WriteJSON sets the response content type to application/json and returns an http.Handler that will write the bytes to the response.

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 BadGateway

func BadGateway() *ErrorResponse

HTTP 502 Bad Gateway

func BadRequest

func BadRequest() *ErrorResponse

HTTP 400 Bad Request

func Conflict

func Conflict() *ErrorResponse

HTTP 409 Conflict

func ExpectationFailed

func ExpectationFailed() *ErrorResponse

HTTP 417 Expectation Failed

func Forbidden

func Forbidden() *ErrorResponse

HTTP 403 Forbidden

func GatewayTimeout

func GatewayTimeout() *ErrorResponse

HTTP 504 Gateway Timeout

func Gone

func Gone() *ErrorResponse

HTTP 410 Gone

func InternalServerError

func InternalServerError() *ErrorResponse

HTTP 500 Internal Server Error

func LengthRequired

func LengthRequired() *ErrorResponse

HTTP 411 Length Required

func NotAcceptable

func NotAcceptable() *ErrorResponse

HTTP 406 Not Acceptable

func NotFound

func NotFound() *ErrorResponse

HTTP 404 Not Found

func NotImplemented

func NotImplemented() *ErrorResponse

HTTP 501 Not Implemented

func PreconditionFailed

func PreconditionFailed() *ErrorResponse

HTTP 412 Precondition Failed

func RequestEntityTooLarge

func RequestEntityTooLarge() *ErrorResponse

HTTP 413 Request Entity Too Large

func RequestTimeout

func RequestTimeout() *ErrorResponse

HTTP 408 Request Timeout

func RequestURITooLong

func RequestURITooLong() *ErrorResponse

HTTP 414 Request URI Too Long

func RequestedRangeNotSatisfiable

func RequestedRangeNotSatisfiable() *ErrorResponse

HTTP 416 Requested Range Not Satisfiable

func ServiceUnavailable

func ServiceUnavailable() *ErrorResponse

HTTP 503 Service Unavailable

func TooManyRequests

func TooManyRequests() *ErrorResponse

HTTP 429 Too Many Requests

func Unauthorized

func Unauthorized() *ErrorResponse

HTTP 401 Unauthorized

func UnprocessableEntity

func UnprocessableEntity() *ErrorResponse

HTTP 422 Unprocessable Entity

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.

func NewFragment

func NewFragment(ctx interface{}) *Fragment

NewFragment returns a new Fragment.

func (*Fragment) Bind

func (f *Fragment) Bind(name string, value interface{})

Bind records a value to bind to the context by a given name.

func (*Fragment) Register

func (f *Fragment) Register(app *Application, prefix, name string) error

Registers the Fragment to the Application under a given URL prefix and name. All recorded actions and bindings are applied to the Application.

func (*Fragment) Route

func (f *Fragment) Route(path, name string, methods []string)

Route records the addition of a new route for when it is registered to an application.

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

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 NewRouter

func NewRouter() *Router

NewRouter returns a new Router.

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) Route

func (r *Router) Route(path, name string, context reflect.Type, methods []string) error

Route registers a route by method name.

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.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL