2017-02-23 17:08:37 +03:00
|
|
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-05 15:53:26 +03:00
|
|
|
"github.com/vmihailenco/msgpack"
|
2017-02-23 17:08:37 +03:00
|
|
|
)
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// MsgPack contains the given interface object.
|
2017-02-23 17:08:37 +03:00
|
|
|
type MsgPack struct {
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (MsgPack) writes MsgPack ContentType.
|
2017-02-23 17:08:37 +03:00
|
|
|
func (r MsgPack) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, msgpackContentType)
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
|
2017-02-23 17:08:37 +03:00
|
|
|
func (r MsgPack) Render(w http.ResponseWriter) error {
|
|
|
|
return WriteMsgPack(w, r.Data)
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
|
2017-02-23 17:08:37 +03:00
|
|
|
func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
|
|
|
|
writeContentType(w, msgpackContentType)
|
2019-07-05 15:53:26 +03:00
|
|
|
return msgpack.NewEncoder(w).Encode(obj)
|
2017-02-23 17:08:37 +03:00
|
|
|
}
|