response

package
v0.0.0-...-f49f1ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomHTTPErrorHandler

func CustomHTTPErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error)

CustomHTTPErrorHandler 是自定义的HTTP错误处理器

func ForwardResponseMessage

func ForwardResponseMessage(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error)

ForwardResponseMessage is a standard function signature in gRPC-Gateway v2 for intercepting and customizing the response. NOTE: This implementation is kept for reference, but the primary wrapping is handled by the Marshal method above for broader compatibility.

func HTTPStatusFromCode

func HTTPStatusFromCode(code codes.Code) int

HTTPStatusFromCode 将gRPC状态码转换为HTTP状态码

func Setup

func Setup(mux *runtime.ServeMux)

Setup 配置响应系统,包括格式化器和错误处理器

func SetupHTTPErrorHandler

func SetupHTTPErrorHandler(mux *runtime.ServeMux)

SetupHTTPErrorHandler 设置自定义的HTTP错误处理器

func SetupMarshalers

func SetupMarshalers(mux *runtime.ServeMux)

SetupMarshalers 配置响应编码器

func WriteBadRequest

func WriteBadRequest(w http.ResponseWriter, message string, err error)

WriteBadRequest 写入400错误响应

func WriteError

func WriteError(w http.ResponseWriter, code int, message string, err error)

WriteError 写入错误响应

func WriteForbidden

func WriteForbidden(w http.ResponseWriter, message string, err error)

WriteForbidden 写入403错误响应

func WriteInternalServerError

func WriteInternalServerError(w http.ResponseWriter, message string, err error)

WriteInternalServerError 写入500错误响应

func WriteJSON

func WriteJSON(w http.ResponseWriter, data interface{}, statusCode int)

WriteJSON 将数据以JSON格式写入HTTP响应

func WriteNotFound

func WriteNotFound(w http.ResponseWriter, message string, err error)

WriteNotFound 写入404错误响应

func WriteRawData

func WriteRawData(w http.ResponseWriter, data []byte, contentType string)

WriteRawData 直接写入原始数据,不包装

func WriteSuccess

func WriteSuccess(w http.ResponseWriter, data interface{}, message string)

WriteSuccess 写入成功响应

func WriteUnauthorized

func WriteUnauthorized(w http.ResponseWriter, message string, err error)

WriteUnauthorized 写入401错误响应

Types

type CustomMarshaler

type CustomMarshaler struct {
	runtime.Marshaler
}

CustomMarshaler implements the runtime.Marshaler interface

func (*CustomMarshaler) Marshal

func (c *CustomMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal wraps the successful proto.Message into the Wrapper struct.

type FileMarshaler

type FileMarshaler struct{}

FileMarshaler 是文件下载处理器

func (*FileMarshaler) ContentType

func (m *FileMarshaler) ContentType(v interface{}) string

ContentType 返回内容类型

func (*FileMarshaler) Marshal

func (m *FileMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal 处理文件下载

func (*FileMarshaler) NewDecoder

func (m *FileMarshaler) NewDecoder(r io.Reader) runtime.Decoder

NewDecoder 返回一个新的解码器

func (*FileMarshaler) NewEncoder

func (m *FileMarshaler) NewEncoder(w io.Writer) runtime.Encoder

NewEncoder 返回一个新的编码器

func (*FileMarshaler) Unmarshal

func (m *FileMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal 实现 runtime.Marshaler 接口

type JSONMarshaler

type JSONMarshaler struct{}

JSONMarshaler 是标准 JSON 包装器,用于包装响应为统一格式

func (*JSONMarshaler) ContentType

func (m *JSONMarshaler) ContentType(v interface{}) string

ContentType 返回内容类型

func (*JSONMarshaler) Marshal

func (m *JSONMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal 将响应包装为统一的 JSON 格式

func (*JSONMarshaler) NewDecoder

func (m *JSONMarshaler) NewDecoder(r io.Reader) runtime.Decoder

NewDecoder 返回一个新的解码器

func (*JSONMarshaler) NewEncoder

func (m *JSONMarshaler) NewEncoder(w io.Writer) runtime.Encoder

NewEncoder 返回一个新的编码器

func (*JSONMarshaler) Unmarshal

func (m *JSONMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal 实现 runtime.Marshaler 接口

type RawDataMarshaler

type RawDataMarshaler struct{}

RawDataMarshaler 是原始数据处理器,不做任何包装

func (*RawDataMarshaler) ContentType

func (m *RawDataMarshaler) ContentType(v interface{}) string

ContentType 返回内容类型

func (*RawDataMarshaler) Marshal

func (m *RawDataMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal 直接返回原始数据,无包装

func (*RawDataMarshaler) NewDecoder

func (m *RawDataMarshaler) NewDecoder(r io.Reader) runtime.Decoder

NewDecoder 返回一个新的解码器

func (*RawDataMarshaler) NewEncoder

func (m *RawDataMarshaler) NewEncoder(w io.Writer) runtime.Encoder

NewEncoder 返回一个新的编码器

func (*RawDataMarshaler) Unmarshal

func (m *RawDataMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal 实现 runtime.Marshaler 接口

type Wrapper

type Wrapper struct {
	// Status 表示请求状态,"success" 或 "error"
	Status string `json:"status"`
	// Code 表示 HTTP 状态码
	Code int `json:"code"`
	// Message 包含响应的消息说明
	Message string `json:"message"`
	// Data 包含响应的具体数据
	Data interface{} `json:"data,omitempty"`
	// Error 包含详细错误信息,仅在开发环境中返回
	Error interface{} `json:"error,omitempty"`
}

Wrapper 是统一的响应包装结构

func NewBadRequestResponse

func NewBadRequestResponse(message string, err error) *Wrapper

NewBadRequestResponse 创建400错误响应

func NewErrorResponse

func NewErrorResponse(code int, message string, err error) *Wrapper

NewErrorResponse 创建一个错误响应

func NewForbiddenResponse

func NewForbiddenResponse(message string, err error) *Wrapper

NewForbiddenResponse 创建403错误响应

func NewInternalServerErrorResponse

func NewInternalServerErrorResponse(message string, err error) *Wrapper

NewInternalServerErrorResponse 创建500错误响应

func NewNotFoundResponse

func NewNotFoundResponse(message string, err error) *Wrapper

NewNotFoundResponse 创建404错误响应

func NewSuccessResponse

func NewSuccessResponse(data interface{}, message string) *Wrapper

NewSuccessResponse 创建一个成功响应

func NewUnauthorizedResponse

func NewUnauthorizedResponse(message string, err error) *Wrapper

NewUnauthorizedResponse 创建401错误响应

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL