Documentation
¶
Index ¶
- func CustomHTTPErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, ...)
- func ForwardResponseMessage(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, ...)
- func HTTPStatusFromCode(code codes.Code) int
- func Setup(mux *runtime.ServeMux)
- func SetupHTTPErrorHandler(mux *runtime.ServeMux)
- func SetupMarshalers(mux *runtime.ServeMux)
- func WriteBadRequest(w http.ResponseWriter, message string, err error)
- func WriteError(w http.ResponseWriter, code int, message string, err error)
- func WriteForbidden(w http.ResponseWriter, message string, err error)
- func WriteInternalServerError(w http.ResponseWriter, message string, err error)
- func WriteJSON(w http.ResponseWriter, data interface{}, statusCode int)
- func WriteNotFound(w http.ResponseWriter, message string, err error)
- func WriteRawData(w http.ResponseWriter, data []byte, contentType string)
- func WriteSuccess(w http.ResponseWriter, data interface{}, message string)
- func WriteUnauthorized(w http.ResponseWriter, message string, err error)
- type CustomMarshaler
- type FileMarshaler
- func (m *FileMarshaler) ContentType(v interface{}) string
- func (m *FileMarshaler) Marshal(v interface{}) ([]byte, error)
- func (m *FileMarshaler) NewDecoder(r io.Reader) runtime.Decoder
- func (m *FileMarshaler) NewEncoder(w io.Writer) runtime.Encoder
- func (m *FileMarshaler) Unmarshal(data []byte, v interface{}) error
- type JSONMarshaler
- func (m *JSONMarshaler) ContentType(v interface{}) string
- func (m *JSONMarshaler) Marshal(v interface{}) ([]byte, error)
- func (m *JSONMarshaler) NewDecoder(r io.Reader) runtime.Decoder
- func (m *JSONMarshaler) NewEncoder(w io.Writer) runtime.Encoder
- func (m *JSONMarshaler) Unmarshal(data []byte, v interface{}) error
- type RawDataMarshaler
- func (m *RawDataMarshaler) ContentType(v interface{}) string
- func (m *RawDataMarshaler) Marshal(v interface{}) ([]byte, error)
- func (m *RawDataMarshaler) NewDecoder(r io.Reader) runtime.Decoder
- func (m *RawDataMarshaler) NewEncoder(w io.Writer) runtime.Encoder
- func (m *RawDataMarshaler) Unmarshal(data []byte, v interface{}) error
- type Wrapper
- func NewBadRequestResponse(message string, err error) *Wrapper
- func NewErrorResponse(code int, message string, err error) *Wrapper
- func NewForbiddenResponse(message string, err error) *Wrapper
- func NewInternalServerErrorResponse(message string, err error) *Wrapper
- func NewNotFoundResponse(message string, err error) *Wrapper
- func NewSuccessResponse(data interface{}, message string) *Wrapper
- func NewUnauthorizedResponse(message string, err error) *Wrapper
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 ¶
HTTPStatusFromCode 将gRPC状态码转换为HTTP状态码
func SetupHTTPErrorHandler ¶
SetupHTTPErrorHandler 设置自定义的HTTP错误处理器
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 ¶
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 ¶
NewBadRequestResponse 创建400错误响应
func NewErrorResponse ¶
NewErrorResponse 创建一个错误响应
func NewForbiddenResponse ¶
NewForbiddenResponse 创建403错误响应
func NewInternalServerErrorResponse ¶
NewInternalServerErrorResponse 创建500错误响应
func NewNotFoundResponse ¶
NewNotFoundResponse 创建404错误响应
func NewSuccessResponse ¶
NewSuccessResponse 创建一个成功响应
func NewUnauthorizedResponse ¶
NewUnauthorizedResponse 创建401错误响应