data

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

package data provides wrappers for response data, optionally including response headers such as ETag and Cache-Control.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContentAndApplyExtraHeaders added in v0.9.0

func GetContentAndApplyExtraHeaders(rw http.ResponseWriter, req *http.Request, d Data, template, language string) (interface{}, error)

GetContentAndApplyExtraHeaders applies all lazy functions to produce the resulting content to be rendered; this value is returned. It also sets any extra response headers.

Types

type Data

type Data interface {
	// Content returns the data as a value that can be processed by encoders such as "encoding/json"
	// The returned values are the data itself, a hash that will be used as the entity tag (if required),
	// and an error if arising. The hash should be blank if not needed.
	//
	// The contentRequired flag is initially false; this gives an opportunity to obtain the entity tag
	// without the data. At this stage, data may be returned only if it is convenient.
	//
	// If necessary, the method will be called a second time, this time with contentRequired true. The
	// data must always be returned in this case. However the etag will be ignored.
	Content(template, language string, contentRequired bool) (interface{}, string, error)

	// Headers returns response headers relating to the data (optional)
	Headers() map[string]string
}

Data provides a source for response content. It is optimised for lazy evaluation, avoiding wasted processing.

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is a simple implementation of Data.

func Lazy

func Lazy(fn func(template, language string, contentRequired bool) (interface{}, string, error)) *Value

Lazy wraps a function that supplies a data value, but only when it is needed.

func Of

func Of(v interface{}, etag ...string) *Value

Of wraps a data value. An optional entity tag can also be passed in. This is often the MD5 sum of the content, or something similar. If this is non-blank, the ETag response header will be sent.

func (*Value) Content

func (v *Value) Content(template, language string, contentRequired bool) (result interface{}, etag string, err error)

func (Value) Expires added in v0.9.0

func (v Value) Expires(at time.Time) *Value

Expires sets the time at which the response becomes stale. MaxAge takes precedence.

func (Value) Headers

func (v Value) Headers() map[string]string

func (Value) LastModified added in v0.9.0

func (v Value) LastModified(at time.Time) *Value

LastModified sets the time at which the content was last modified. This allows for conditional requests, possibly avoiding network traffic. ETag takes precedence.

func (Value) MaxAge added in v0.9.0

func (v Value) MaxAge(max time.Duration) *Value

MaxAge sets the max-age header on the response. This is used to allow caches to avoid repeating the request until the max age has expired, after which time the resource is considered stale.

func (Value) NoCache added in v0.9.0

func (v Value) NoCache() *Value

NoCache sets cache control headers to prevent the response being cached.

func (Value) With

func (v Value) With(hdr string, value string, others ...string) *Value

With returns a copy of v with extra headers attached. These are passed in as key+value pairs. The header names should be in normal form, e.g. "Last-Modified" instead of "last-modified", but this is not mandatory. The values are simple strings, numbers etc. Or they can be func(interface{}) string, in which case they will be called using the result of Content.

Jump to

Keyboard shortcuts

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