render

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

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

View Source
const (
	ContentTypeUnknown = iota
	ContentTypePlainText
	ContentTypeHTML
	ContentTypeJSON
	ContentTypeXML
	ContentTypeForm
	ContentTypeEventStream
	ContentTypeNTriples
	ContentTypeRDFXML
	ContentTypeTurtle
	ContentTypeNQuads
	ContentTypeJSONLD
)

ContentTypes handled by this package.

Variables

View Source
var ContentTypeCtxKey = &contextKey{"ContentType"}
View Source
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.

View Source
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

func Status(r *http.Request, status int)

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

func Turtle

func Turtle(w http.ResponseWriter, r *http.Request, v string)

Turtle writes a string to the response, setting the Content-Type as text/turtle.

func XML

func XML(w http.ResponseWriter, r *http.Request, v interface{})

XML marshals 'v' to JSON, setting the Content-Type as application/xml. It will automatically prepend a generic XML header (see encoding/xml.Header) if one is not found in the first 100 bytes of 'v'.

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.

type ErrorConfig

type ErrorConfig struct {
	Log           *zerolog.Logger
	StatusCode    int
	Message       string
	DatasetID     string
	OrgID         string
	PreventBubble bool // prevent message from being logged or sent to sentry
}

Jump to

Keyboard shortcuts

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