github.com/m3db/m3@v1.5.0/src/x/net/http/response.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package xhttp
    22  
    23  import (
    24  	"encoding/json"
    25  	"errors"
    26  	"net/http"
    27  
    28  	"github.com/gogo/protobuf/jsonpb"
    29  	"github.com/gogo/protobuf/proto"
    30  	"go.uber.org/zap"
    31  )
    32  
    33  const (
    34  	// HeaderContentType is the HTTP Content Type header.
    35  	HeaderContentType = "Content-Type"
    36  
    37  	// ContentTypeJSON is the Content-Type value for a JSON response.
    38  	ContentTypeJSON = "application/json"
    39  
    40  	// ContentTypeFormURLEncoded is the Content-Type value for a URL-encoded form.
    41  	ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
    42  
    43  	// ContentTypeHTMLUTF8 is the Content-Type value for UTF8-encoded HTML.
    44  	ContentTypeHTMLUTF8 = "text/html; charset=utf-8"
    45  
    46  	// ContentTypeProtobuf is the Content-Type value for a Protobuf message.
    47  	ContentTypeProtobuf = "application/x-protobuf"
    48  
    49  	// ContentTypeOctetStream is the Content-Type value for binary data.
    50  	ContentTypeOctetStream = "application/octet-stream"
    51  )
    52  
    53  // WriteJSONResponse writes generic data to the ResponseWriter
    54  func WriteJSONResponse(w http.ResponseWriter, data interface{}, logger *zap.Logger) {
    55  	jsonData, err := json.Marshal(data)
    56  	if err != nil {
    57  		logger.Error("unable to marshal json", zap.Error(err))
    58  		WriteError(w, err)
    59  		return
    60  	}
    61  
    62  	w.Header().Set(HeaderContentType, ContentTypeJSON)
    63  	w.Write(jsonData)
    64  }
    65  
    66  // WriteProtoMsgJSONResponse writes a protobuf message to the ResponseWriter. This uses jsonpb
    67  // for json marshalling, which encodes fields with default values, even with the omitempty tag.
    68  func WriteProtoMsgJSONResponse(w http.ResponseWriter, data proto.Message, logger *zap.Logger) {
    69  	marshaler := jsonpb.Marshaler{EmitDefaults: true}
    70  
    71  	w.Header().Set(HeaderContentType, ContentTypeJSON)
    72  	err := marshaler.Marshal(w, data)
    73  	if err != nil {
    74  		logger.Error("unable to marshal json", zap.Error(err))
    75  		WriteError(w, err)
    76  	}
    77  }
    78  
    79  // WriteUninitializedResponse writes a protobuf message to the ResponseWriter
    80  func WriteUninitializedResponse(w http.ResponseWriter, logger *zap.Logger) {
    81  	logger.Warn("request received before M3DB session is initialized")
    82  	err := NewError(errors.New("connection to M3DB not yet initialized"),
    83  		http.StatusServiceUnavailable)
    84  	WriteError(w, err)
    85  }