Documentation ¶
Index ¶
- type HTTPError
- type HandleRequestFunction
- type Response
- type Router
- func (router *Router) AddErrorMapping(err error, responseCode int, details ...string)
- func (router *Router) AddRequestHandler(method string, path string, handleFunc HandleRequestFunction)
- func (router *Router) AddRequestHandlerWithAuthFunc(method string, path string, handleFunc HandleRequestFunction, ...)
- func (router *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPError ¶
type HTTPError struct { // http status code to return StatusCode int // URL to describe the response code Type string `json:"type"` // name of the response Title string `json:"title"` // additional details for the error, e.g. what went wrong Details []string `json:"details"` }
HTTPError implements error and is used to return 4xx/5xx HTTP responses. It can be converted to Response with the StatusCode of the HTTPError and HTTPError itself as the Body of the response.
func NewHTTPError ¶
NewHTTPError creates a new error with a specified statusCode and any number of details. The Title and Type of the error is set to correspond with the status code.
func (*HTTPError) ToResponse ¶
ToResponse converts the HTTPError to a response to be written.
type HandleRequestFunction ¶
HandleRequestFunction is a custom function to specify the implementation of an HTTP endpoint. It does not receive the http.ResponseWriter for the request since it is written by the requestHandler. The content of this response is specified by the returned Response or the error. If an error is returned, it is mapped to a response by the errorMapper of the Router the function was added to. The Function does receive the path parameters, because they are already extracted for validation purposes.
type Response ¶
type Response struct { // http StatusCode to return StatusCode int // Body of the http request to return. If it is set to string, a plain text response is return. If it is anything // else, the response is returned in JSON format. Body interface{} // http Headers to add to the response Headers map[string]string }
Response is a data struct that depicts the http response to be returned.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
The Router which implements the described features. It implements http.Handler to be compatible with existing HTTP libraries.
func NewRouter ¶
NewRouter creates a new Router with the path of a OpenAPI specification file in YAML or JSON format.
func (*Router) AddErrorMapping ¶
AddErrorMapping adds a custom error that should be mapped to an error response. It uses the HTTPError to create the response. It takes an error and the response code this error should be mapped to. Additionally, any number of details can be specified.
func (*Router) AddRequestHandler ¶
func (router *Router) AddRequestHandler(method string, path string, handleFunc HandleRequestFunction)
AddRequestHandler creates a new requestHandler for a specified method and path. It is used to set an implementation for an endpoint. The function panics, if the endpoint is not specified in the OpenAPI specification
func (*Router) AddRequestHandlerWithAuthFunc ¶ added in v0.2.0
func (router *Router) AddRequestHandlerWithAuthFunc(method string, path string, handleFunc HandleRequestFunction, authFunc openapi3filter.AuthenticationFunc)
AddRequestHandlerWithAuthFunc creates a new requestHandler for a specified method and path. It is used to set an implementation for an endpoint. The function panics, if the endpoint is not specified in the OpenAPI specification. In Addition to AddRequestHandler adds an openapi3filter.AuthenticationFunc which is necessary to validate a request with specified SecurityRequirements. If SecurityRequirements are specified for a resource without openapi3filter.AuthenticationFunc, the router will respond with http.StatusInternalServerError.
func (*Router) ServeHTTP ¶
func (router *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)
Implementation of http.Handler that finds the requestHandler for an incoming request and validates the requests. It also adds the pathParameters to the requests Context so they can be extracted by the requestHandler