1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-26 03:52:12 +02:00
2019-04-11 15:07:22 +08:00

39 lines
756 B
Go

package render
import (
"net/http"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
)
var pbContentType = []string{"application/x-protobuf"}
// Render (PB) writes data with protobuf ContentType.
func (r PB) Render(w http.ResponseWriter) error {
if r.TTL <= 0 {
r.TTL = 1
}
return writePB(w, r)
}
// WriteContentType write protobuf ContentType.
func (r PB) WriteContentType(w http.ResponseWriter) {
writeContentType(w, pbContentType)
}
func writePB(w http.ResponseWriter, obj PB) (err error) {
var pbBytes []byte
writeContentType(w, pbContentType)
if pbBytes, err = proto.Marshal(&obj); err != nil {
err = errors.WithStack(err)
return
}
if _, err = w.Write(pbBytes); err != nil {
err = errors.WithStack(err)
}
return
}