SerializeProtoResponse serializes a protobuf response into an HTTP response.
(w http.ResponseWriter, resp proto.Message, compression CompressionType)
| 271 | |
| 272 | // SerializeProtoResponse serializes a protobuf response into an HTTP response. |
| 273 | func SerializeProtoResponse(w http.ResponseWriter, resp proto.Message, compression CompressionType) error { |
| 274 | data, err := proto.Marshal(resp) |
| 275 | if err != nil { |
| 276 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 277 | return fmt.Errorf("error marshaling proto response: %v", err) |
| 278 | } |
| 279 | |
| 280 | switch compression { |
| 281 | case NoCompression: |
| 282 | case RawSnappy: |
| 283 | data = snappy.Encode(nil, data) |
| 284 | } |
| 285 | |
| 286 | if _, err := w.Write(data); err != nil { |
| 287 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 288 | return fmt.Errorf("error sending proto response: %v", err) |
| 289 | } |
| 290 | return nil |
| 291 | } |