protobuf.go 645 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2018 Gin Core Team. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package render
  5. import (
  6. "net/http"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. type ProtoBuf struct {
  10. Data interface{}
  11. }
  12. var protobufContentType = []string{"application/x-protobuf"}
  13. func (r ProtoBuf) Render(w http.ResponseWriter) error {
  14. r.WriteContentType(w)
  15. bytes, err := proto.Marshal(r.Data.(proto.Message))
  16. if err != nil {
  17. return err
  18. }
  19. w.Write(bytes)
  20. return nil
  21. }
  22. func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {
  23. writeContentType(w, protobufContentType)
  24. }