This commit is contained in:
2026-01-25 21:59:00 +08:00
parent 7fd537bef3
commit 4cad3f0250
118 changed files with 30473 additions and 0 deletions

133
pkg/api/response.go Normal file
View File

@@ -0,0 +1,133 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Response represents a standard API response
type Response struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error *ErrorInfo `json:"error,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}
// ErrorInfo contains error details
type ErrorInfo struct {
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
// Meta contains pagination and other metadata
type Meta struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
TotalCount int64 `json:"total_count,omitempty"`
TotalPages int `json:"total_pages,omitempty"`
}
// Success sends a successful response
func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Response{
Success: true,
Data: data,
})
}
// SuccessWithMeta sends a successful response with metadata
func SuccessWithMeta(c *gin.Context, data interface{}, meta *Meta) {
c.JSON(http.StatusOK, Response{
Success: true,
Data: data,
Meta: meta,
})
}
// Created sends a 201 Created response
func Created(c *gin.Context, data interface{}) {
c.JSON(http.StatusCreated, Response{
Success: true,
Data: data,
})
}
// NoContent sends a 204 No Content response
func NoContent(c *gin.Context) {
c.Status(http.StatusNoContent)
}
// Error sends an error response
func Error(c *gin.Context, statusCode int, code, message string) {
c.JSON(statusCode, Response{
Success: false,
Error: &ErrorInfo{
Code: code,
Message: message,
},
})
}
// ErrorWithDetails sends an error response with additional details
func ErrorWithDetails(c *gin.Context, statusCode int, code, message, details string) {
c.JSON(statusCode, Response{
Success: false,
Error: &ErrorInfo{
Code: code,
Message: message,
Details: details,
},
})
}
// BadRequest sends a 400 Bad Request response
func BadRequest(c *gin.Context, message string) {
Error(c, http.StatusBadRequest, "BAD_REQUEST", message)
}
// NotFound sends a 404 Not Found response
func NotFound(c *gin.Context, message string) {
Error(c, http.StatusNotFound, "NOT_FOUND", message)
}
// Conflict sends a 409 Conflict response
func Conflict(c *gin.Context, message string) {
Error(c, http.StatusConflict, "CONFLICT", message)
}
// InternalError sends a 500 Internal Server Error response
func InternalError(c *gin.Context, message string) {
Error(c, http.StatusInternalServerError, "INTERNAL_ERROR", message)
}
// ValidationError sends a 400 response for validation errors
func ValidationError(c *gin.Context, message string) {
Error(c, http.StatusBadRequest, "VALIDATION_ERROR", message)
}
// BadGateway sends a 502 Bad Gateway response
func BadGateway(c *gin.Context, message string) {
Error(c, http.StatusBadGateway, "BAD_GATEWAY", message)
}
// ServiceUnavailable sends a 503 Service Unavailable response
func ServiceUnavailable(c *gin.Context, message string) {
Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", message)
}
// Unauthorized sends a 401 Unauthorized response
func Unauthorized(c *gin.Context, message string) {
Error(c, http.StatusUnauthorized, "UNAUTHORIZED", message)
}
// Forbidden sends a 403 Forbidden response
func Forbidden(c *gin.Context, message string) {
Error(c, http.StatusForbidden, "FORBIDDEN", message)
}
// RequestEntityTooLarge sends a 413 Request Entity Too Large response
func RequestEntityTooLarge(c *gin.Context, message string) {
Error(c, http.StatusRequestEntityTooLarge, "REQUEST_ENTITY_TOO_LARGE", message)
}