Files
cloudnest/internal/pkg/response/response.go
T

182 lines
4.2 KiB
Go

package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Response represents the standard API response structure.
// Response 表示标准的 API 响应结构。
//
// This struct provides a consistent format for all API responses.
//
// 此结构体为所有 API 响应提供了一致的格式。
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// Success sends a successful response with data.
// Success 发送包含数据的成功响应。
//
// Parameters:
// - c: The Gin context.
// - data: The data to include in the response.
//
// Note:
// - Sets HTTP status code to 200 OK.
// - Sets code to 0 (success).
//
// 参数:
// - c: Gin 上下文。
// - data: 要包含在响应中的数据。
//
// 注意:
// - 设置 HTTP 状态码为 200 OK。
// - 设置 code 为 0(成功)。
func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Response{
Code: 0,
Message: "success",
Data: data,
})
}
// SuccessMsg sends a successful response with only a message.
// SuccessMsg 发送仅包含消息的成功响应。
//
// Parameters:
// - c: The Gin context.
// - message: The success message.
//
// Note:
// - Sets HTTP status code to 200 OK.
// - Sets code to 0 (success).
//
// 参数:
// - c: Gin 上下文。
// - message: 成功消息。
//
// 注意:
// - 设置 HTTP 状态码为 200 OK。
// - 设置 code 为 0(成功)。
func SuccessMsg(c *gin.Context, message string) {
c.JSON(http.StatusOK, Response{
Code: 0,
Message: message,
})
}
// Error sends an error response with a custom code and message.
// Error 发送包含自定义代码和消息的错误响应。
//
// Parameters:
// - c: The Gin context.
// - code: The custom error code.
// - message: The error message.
//
// Note:
// - Sets HTTP status code to 200 OK.
// - The custom code indicates the specific error type.
//
// 参数:
// - c: Gin 上下文。
// - code: 自定义错误代码。
// - message: 错误消息。
//
// 注意:
// - 设置 HTTP 状态码为 200 OK。
// - 自定义代码表示特定的错误类型。
func Error(c *gin.Context, code int, message string) {
c.JSON(http.StatusOK, Response{
Code: code,
Message: message,
})
}
// BadRequest sends a 400 Bad Request response.
// BadRequest 发送 400 Bad Request 响应。
//
// Parameters:
// - c: The Gin context.
// - message: The error message.
//
// 参数:
// - c: Gin 上下文。
// - message: 错误消息。
func BadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, Response{
Code: 400,
Message: message,
})
}
// Unauthorized sends a 401 Unauthorized response.
// Unauthorized 发送 401 Unauthorized 响应。
//
// Parameters:
// - c: The Gin context.
// - message: The error message.
//
// 参数:
// - c: Gin 上下文。
// - message: 错误消息。
func Unauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, Response{
Code: 401,
Message: message,
})
}
// Forbidden sends a 403 Forbidden response.
// Forbidden 发送 403 Forbidden 响应。
//
// Parameters:
// - c: The Gin context.
// - message: The error message.
//
// 参数:
// - c: Gin 上下文。
// - message: 错误消息。
func Forbidden(c *gin.Context, message string) {
c.JSON(http.StatusForbidden, Response{
Code: 403,
Message: message,
})
}
// NotFound sends a 404 Not Found response.
// NotFound 发送 404 Not Found 响应。
//
// Parameters:
// - c: The Gin context.
// - message: The error message.
//
// 参数:
// - c: Gin 上下文。
// - message: 错误消息。
func NotFound(c *gin.Context, message string) {
c.JSON(http.StatusNotFound, Response{
Code: 404,
Message: message,
})
}
// InternalError sends a 500 Internal Server Error response.
// InternalError 发送 500 Internal Server Error 响应。
//
// Parameters:
// - c: The Gin context.
// - message: The error message.
//
// 参数:
// - c: Gin 上下文。
// - message: 错误消息。
func InternalError(c *gin.Context, message string) {
c.JSON(http.StatusInternalServerError, Response{
Code: 500,
Message: message,
})
}