# README
go-restful
package for building REST-style Web Services using Google Go
REST asks developers to use HTTP methods explicitly and in a way that's consistent with the protocol definition. This basic REST design principle establishes a one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods. According to this mapping:
- GET = Retrieve a representation of a resource
- POST = Create if you are sending content to the server to create a subordinate of the specified resource collection, using some server-side algorithm.
- PUT = Create if you are sending the full content of the specified resource (URI).
- PUT = Update if you are updating the full content of the specified resource.
- DELETE = Delete if you are requesting the server to delete the resource
- PATCH = Update partial content of a resource
- OPTIONS = Get information about the communication options for the request URI
Example
ws := new(restful.WebService)
ws.
Path("/users").
Consumes(restful.MIME_XML, restful.MIME_JSON).
Produces(restful.MIME_JSON, restful.MIME_XML)
ws.Route(ws.GET("/{user-id}").To(u.findUser).
Doc("get a user").
Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")).
Writes(User{}))
...
func (u UserResource) findUser(request *restful.Request, response *restful.Response) {
id := request.PathParameter("user-id")
...
}
Features
- Routes for request → function mapping with path parameter (e.g. {id}) support
- Configurable router:
- Routing algorithm after JSR311 that is implemented using (but doest not accept) regular expressions (See RouterJSR311 which is used by default)
- Fast routing algorithm that allows static elements, regular expressions and dynamic parameters in the URL path (e.g. /meetings/{id} or /static/{subpath:*}, See CurlyRouter)
- Request API for reading structs from JSON/XML and accesing parameters (path,query,header)
- Response API for writing structs to JSON/XML and setting headers
- Filters for intercepting the request → response flow on Service or Route level
- Request-scoped variables using attributes
- Containers for WebServices on different HTTP endpoints
- Content encoding (gzip,deflate) of responses
- Automatic responses on OPTIONS (using a filter)
- Automatic CORS request handling (using a filter)
- API declaration for Swagger UI (see swagger package)
- Panic recovery to produce HTTP 500, customizable using RecoverHandler(...)
Resources
- Documentation on godoc.org
- Code examples
- Example posted on blog
- Design explained on blog
- sourcegraph
- gopkg.in
- showcase: Mora - MongoDB REST Api server
(c) 2012 - 2014, http://ernestmicklei.com. MIT License
Type git shortlog -s
for a full list of contributors.
# Functions
Add registers a new WebService add it to the DefaultContainer.
If ContentType is missing or */* is given then fall back to this type, otherwise a "Unable to unmarshal content of type:" response is returned.
If Accept header matching fails, fall back to this type, otherwise a "406: Not Acceptable" response is returned.
Filter appends a container FilterFunction from the DefaultContainer.
NewCompressingResponseWriter create a CompressingResponseWriter for a known encoding = {gzip,deflate}.
NewContainer creates a new Container using a new ServeMux and default router (RouterJSR311).
NewError returns a ServiceError using the code and reason.
No description provided by the author
Creates a new response based on a http ResponseWriter.
OPTIONSFilter is a filter function that inspects the Http Request for the OPTIONS method and provides the response with a set of allowed methods for the request URL Path.
RegisteredWebServices returns the collections of WebServices from the DefaultContainer.
SetCacheReadEntity controls whether the response data ([]byte) is cached such that ReadEntity is repeatable.
TraceLogger enables detailed logging of Http request matching and filter invocation.
# Constants
BodyParameterKind = indicator of Request parameter type "body".
No description provided by the author
No description provided by the author
FormParameterKind = indicator of Request parameter type "form".
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
HeaderParameterKind = indicator of Request parameter type "header".
Accept or Content-Type used in Consumes() and/or Produces().
Accept or Content-Type used in Consumes() and/or Produces().
PathParameterKind = indicator of Request parameter type "path".
QueryParameterKind = indicator of Request parameter type "query".
# Variables
DefaultContainer is a restful.Container that uses http.DefaultServeMux.
DEPRECATED, use DefaultResponseContentType(mime).
If set the true then panics will not be caught to return HTTP 500.
OBSOLETE : use restful.DefaultContainer.EnableContentEncoding(true) to change this setting.
PrettyPrintResponses controls the indentation feature of XML and JSONserialization in the response methods WriteEntity, WriteAsJson, andWriteAsXml.
# Structs
CompressingResponseWriter is a http.ResponseWriter that can perform content encoding (gzip and zlib).
Container holds a collection of WebServices and a http.ServeMux to dispatch http requests.
CrossOriginResourceSharing is used to create a Container Filter that implements CORS.
CurlyRouter expects Routes with paths that contain zero or more parameters in curly brackets.
FilterChain is a request scoped object to process one or more filters before calling the target RouteFunction.
Parameter is for documententing the parameter used in a Http Request ParameterData kinds are Path,Query and Body.
ParameterData represents the state of a Parameter.
Request is a wrapper for a http Request that provides convenience methods.
Response is a wrapper on the actual http ResponseWriter It provides several convenience methods to prepare and write response content.
No description provided by the author
Route binds a HTTP Method,Path,Consumes combination to a RouteFunction.
RouteBuilder is a helper to construct Routes.
RouterJSR311 implements the flow for matching Requests to Routes (and consequently Resource Functions) as specified by the JSR311 http://jsr311.java.net/nonav/releases/1.1/spec/spec.html.
ServiceError is a transport object to pass information about a non-Http error occurred in a WebService while processing a request.
WebService holds a collection of Route values that bind a Http Method + URL Path to a function.
# Interfaces
A RouteSelector finds the best matching Route given the input HTTP Request.
# Type aliases
FilterFunction definitions must call ProcessFilter on the FilterChain to pass on the control and eventually call the RouteFunction.
RecoverHandleFunction declares functions that can be used to handle a panic situation.
RouteFunction declares the signature of a function that can be bound to a Route.