Documentation ¶
Overview ¶
Package render provides functionality to render responses to http.Handler and http.HandleFunc implementations.
The goal is to unify the way responses are produces and especially API error handling is done.
The package is inspired by 'github.com/go-chi/render'
Index ¶
- Constants
- Variables
- func Data(w http.ResponseWriter, r *http.Request, v []byte)
- func Error(w http.ResponseWriter, r *http.Request, err error, cfg *ErrorConfig)
- func HTML(w http.ResponseWriter, r *http.Request, v string)
- func JSON(w http.ResponseWriter, r *http.Request, v interface{})
- func JSONLD(w http.ResponseWriter, r *http.Request, v string)
- func NTriples(w http.ResponseWriter, r *http.Request, v string)
- func NoContent(w http.ResponseWriter, r *http.Request)
- func PlainText(w http.ResponseWriter, r *http.Request, v string)
- func RDFXML(w http.ResponseWriter, r *http.Request, v string)
- func SetContentType(contentType ContentType) func(next http.Handler) http.Handler
- func Status(r *http.Request, status int)
- func Turtle(w http.ResponseWriter, r *http.Request, v string)
- func XML(w http.ResponseWriter, r *http.Request, v interface{})
- type ContentType
- type ErrorConfig
Constants ¶
const ( ContentTypeUnknown = iota ContentTypePlainText ContentTypeHTML ContentTypeJSON ContentTypeXML ContentTypeForm ContentTypeEventStream ContentTypeNTriples ContentTypeRDFXML ContentTypeTurtle ContentTypeNQuads ContentTypeJSONLD )
ContentTypes handled by this package.
Variables ¶
var ContentTypeCtxKey = &contextKey{"ContentType"}
var DefaultConfig = ErrorConfig{ Log: &log.Logger, StatusCode: http.StatusInternalServerError, Message: "unable to handle request", }
DefaultConfig is a package-level variable set to our default Logger. We do this because it allows you to set your own logger when no logger is supplied with the ErrorConfig.
var StatusCtxKey = &contextKey{"Status"}
StatusCtxKey is a context key to record a future HTTP response status code.
Functions ¶
func Data ¶
func Data(w http.ResponseWriter, r *http.Request, v []byte)
Data writes raw bytes to the response, setting the Content-Type as application/octet-stream.
func Error ¶
func Error(w http.ResponseWriter, r *http.Request, err error, cfg *ErrorConfig)
func HTML ¶
func HTML(w http.ResponseWriter, r *http.Request, v string)
HTML writes a string to the response, setting the Content-Type as text/html.
func JSON ¶
func JSON(w http.ResponseWriter, r *http.Request, v interface{})
JSON marshals 'v' to JSON, automatically escaping HTML and setting the Content-Type as application/json.
func JSONLD ¶
func JSONLD(w http.ResponseWriter, r *http.Request, v string)
JSONLD writes a string to the response, setting the Content-Type as application/ld+json.
func NTriples ¶
func NTriples(w http.ResponseWriter, r *http.Request, v string)
Ntriples writes a string to the response, setting the Content-Type as application/n-triples.
func NoContent ¶
func NoContent(w http.ResponseWriter, r *http.Request)
NoContent returns a HTTP 204 "No Content" response.
func PlainText ¶
func PlainText(w http.ResponseWriter, r *http.Request, v string)
PlainText writes a string to the response, setting the Content-Type as text/plain.
func RDFXML ¶
func RDFXML(w http.ResponseWriter, r *http.Request, v string)
RDFXML writes a string to the response, setting the Content-Type as application/rdf+xml.
func SetContentType ¶
func SetContentType(contentType ContentType) func(next http.Handler) http.Handler
SetContentType is a middleware that forces response Content-Type.
func Status ¶
Status sets a HTTP response status code hint into request context at any point during the request life-cycle. Before the Responder sends its response header it will check the StatusCtxKey
Types ¶
type ContentType ¶
type ContentType int
ContentType is an enumeration of common HTTP content types.
func GetAcceptedContentType ¶
func GetAcceptedContentType(r *http.Request) ContentType
func GetContentType ¶
func GetContentType(s string) ContentType
func GetRequestContentType ¶
func GetRequestContentType(r *http.Request) ContentType
GetRequestContentType is a helper function that returns ContentType based on context or request headers.