Documentation ¶
Index ¶
- Variables
- func Marshaler(cs []ContentMarshaler, cts ...ContentType) (mf MarshalFunc, ct ContentType)
- func RequestMarshaler(r *http.Request, cs ...ContentMarshaler) (mf MarshalFunc, c ContentType)
- type Archetype
- type ContentMarshaler
- type ContentType
- type Getter
- type GetterFunc
- type HTTPHandler
- type Handler
- type MarshalFunc
- type MediaType
- type Request
- type Responder
- type ResponseWriter
Constants ¶
This section is empty.
Variables ¶
var DefaultArchetype = Archetype{ ContentMarshaler: ContentMarshaler{ "": Json, "application/json": Json, "application/xml": Xml, }, }
Functions ¶
func Marshaler ¶
func Marshaler(cs []ContentMarshaler, cts ...ContentType) (mf MarshalFunc, ct ContentType)
func RequestMarshaler ¶
func RequestMarshaler(r *http.Request, cs ...ContentMarshaler) (mf MarshalFunc, c ContentType)
RequestMarshaler returns the first MarshalFunc mf that matches the MIME types specified in the *http.Request from the []ContentMarshaler cs in order of preference, as well as the selected ContentType. The extension of the request path is assumed to be the most preferable MIME type.
Types ¶
type Archetype ¶
type Archetype struct {
ContentMarshaler
}
func (*Archetype) Bind ¶
func (a *Archetype) Bind()
Function BindFweight binds this Archetype to Fweight's OptionsHandler and MethodNotAllowed handlers.
func (*Archetype) Router ¶
func (a *Archetype) Router(g Getter) *HTTPHandler
func (*Archetype) RouterFunc ¶
func (a *Archetype) RouterFunc(g GetterFunc) *HTTPHandler
type ContentMarshaler ¶
type ContentMarshaler map[MediaType]MarshalFunc
func (ContentMarshaler) Marshaler ¶
func (c ContentMarshaler) Marshaler(cts ...ContentType) (mf MarshalFunc, ct ContentType)
Marshaler returns the MarshalFunc present in c that matches one of the ContentTypes with pattern matching (text/*) in order of preference, as well as the chosen ContentType. If mf is nil ct may be some random data.
func (ContentMarshaler) RequestMarshaler ¶
func (c ContentMarshaler) RequestMarshaler(r *http.Request) (mf MarshalFunc, ct ContentType)
RequestMarshalFunc returns the MarshalFunc mf that matches the *http.Request rq, as well as the selected ContentType. The extension of the request path is assumed to be the most preferable MIME type.
type ContentType ¶
func ParseContentType ¶
func ParseContentType(cts string) (c []ContentType, err error)
Function ParseContentType returns an array of ContentTypes corresponding to the Content-Type header string cts.
type Getter ¶
type Getter interface {
Get(ResponseWriter, *http.Request) interface{}
}
type GetterFunc ¶
type GetterFunc func(ResponseWriter, *http.Request) interface{}
func (GetterFunc) Get ¶
func (g GetterFunc) Get(rs ResponseWriter, r *http.Request) interface{}
type HTTPHandler ¶
func Router ¶
func Router(g Getter) *HTTPHandler
func RouterFunc ¶
func RouterFunc(g GetterFunc) *HTTPHandler
func (*HTTPHandler) Bind ¶
func (h *HTTPHandler) Bind(m MediaType, mf MarshalFunc)
func (HTTPHandler) ServeHTTP ¶
func (h HTTPHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request)
type Handler ¶
type Handler struct { *Archetype ContentMarshaler }
func (Handler) ServeObject ¶
func (h Handler) ServeObject(o interface{}, rw http.ResponseWriter, rq *http.Request)
Serveobject serves an interface 'o' using the handler. If o is empty, the response will be empty.
type MarshalFunc ¶
var Gob MarshalFunc = func(r Responder, rq Request) (err error) { r.ContentType("application/gob") err = gob.NewEncoder(r).Encode(r.I) return }
var Json MarshalFunc = func(r Responder, rq Request) error { r.ContentType("application/json;charset=utf8") return json.NewEncoder(r).Encode(r.I) }
var JsonArray MarshalFunc = func(r Responder, rq Request) (err error) { r.ContentType("application/json;charset=utf8") data, err := jsarray.Marshal(r.I) if err != nil { return } _, err = io.Copy(r, bytes.NewReader(data)) return }
See github.com/TShadwell/jsarray for details.
var Plain MarshalFunc = func(r Responder, rq Request) (err error) { r.ContentType("text/plain;charset=utf8") _, err = io.Copy(r, strings.NewReader(r.I.(string))) return }
Plaintext. The underlying value of v must be string.
var Plane MarshalFunc = func(r Responder, rq Request) (err error) { rand.Seed(time.Now().Unix()) r.ContentType("text/plane") rn := make([]rune, 100) for i, ed := 0, cap(rn); i < ed; i++ { rn[i] = planes[rand.Intn(nPlanes)] } _, err = io.Copy(r, strings.NewReader(string(rn))) return }
Planetext.
🛧🛪🛨🛦🛫🛦 🛫🛨 🛨✈🛦🛧🛦 ✈🛬🛫🛫🛬🛫 🛩🛦✈🛩🛬🛬🛨 🛫 🛬🛨✈ 🛩 🛨
var Xml MarshalFunc = func(r Responder, rq Request) error { r.ContentType("application/xml;charset=utf8") return xml.NewEncoder(r).Encode(r.I) }
func HTMLTemplate ¶
func HTMLTemplate(t *htmltemplate.Template) MarshalFunc
HTMLTemplate returns a MarshalFunc that executes the data on template `t` using the html/template package
func TextTemplate ¶
func TextTemplate(t *texttemplate.Template) MarshalFunc
TextTemplate returns a MarshalFunc that executes the data on template `t` using the text/template package.
func (MarshalFunc) ContentType ¶
func (m MarshalFunc) ContentType(ctt string) MarshalFunc
Overwrites the ContentType of a MarshalFunc.
type Responder ¶
type Responder struct { I interface{} http.ResponseWriter // contains filtered or unexported fields }
func (Responder) ContentType ¶
The first call to this function sets the content type of the response.