github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_middleware_handler_response.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"mime"
    11  	"net/http"
    12  
    13  	"github.com/gogf/gf/v2/errors/gcode"
    14  	"github.com/gogf/gf/v2/errors/gerror"
    15  )
    16  
    17  // DefaultHandlerResponse is the default implementation of HandlerResponse.
    18  type DefaultHandlerResponse struct {
    19  	Code    int         `json:"code"    dc:"Error code"`
    20  	Message string      `json:"message" dc:"Error message"`
    21  	Data    interface{} `json:"data"    dc:"Result data for certain request according API definition"`
    22  }
    23  
    24  const (
    25  	contentTypeEventStream  = "text/event-stream"
    26  	contentTypeOctetStream  = "application/octet-stream"
    27  	contentTypeMixedReplace = "multipart/x-mixed-replace"
    28  )
    29  
    30  var (
    31  	// streamContentType is the content types for stream response.
    32  	streamContentType = []string{contentTypeEventStream, contentTypeOctetStream, contentTypeMixedReplace}
    33  )
    34  
    35  // MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
    36  func MiddlewareHandlerResponse(r *Request) {
    37  	r.Middleware.Next()
    38  
    39  	// There's custom buffer content, it then exits current handler.
    40  	if r.Response.BufferLength() > 0 {
    41  		return
    42  	}
    43  
    44  	// It does not output common response content if it is stream response.
    45  	mediaType, _, _ := mime.ParseMediaType(r.Response.Header().Get("Content-Type"))
    46  	for _, ct := range streamContentType {
    47  		if mediaType == ct {
    48  			return
    49  		}
    50  	}
    51  
    52  	var (
    53  		msg  string
    54  		err  = r.GetError()
    55  		res  = r.GetHandlerResponse()
    56  		code = gerror.Code(err)
    57  	)
    58  	if err != nil {
    59  		if code == gcode.CodeNil {
    60  			code = gcode.CodeInternalError
    61  		}
    62  		msg = err.Error()
    63  	} else {
    64  		if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
    65  			msg = http.StatusText(r.Response.Status)
    66  			switch r.Response.Status {
    67  			case http.StatusNotFound:
    68  				code = gcode.CodeNotFound
    69  			case http.StatusForbidden:
    70  				code = gcode.CodeNotAuthorized
    71  			default:
    72  				code = gcode.CodeUnknown
    73  			}
    74  			// It creates error as it can be retrieved by other middlewares.
    75  			err = gerror.NewCode(code, msg)
    76  			r.SetError(err)
    77  		} else {
    78  			code = gcode.CodeOK
    79  		}
    80  	}
    81  
    82  	r.Response.WriteJson(DefaultHandlerResponse{
    83  		Code:    code.Code(),
    84  		Message: msg,
    85  		Data:    res,
    86  	})
    87  }