Files
terminalShop-old/pkg/utils/response.go

64 lines
1.5 KiB
Go

package utils
import (
"encoding/json"
"net/http"
"time"
)
// SuccessResponse represents a successful API response
type SuccessResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data"`
}
// ErrorResponse represents an error API response
type ErrorResponse struct {
Success bool `json:"success"`
Error Error `json:"error"`
}
// Error contains error details
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
Details map[string]interface{} `json:"details,omitempty"`
}
// RespondSuccess sends a successful JSON response
func RespondSuccess(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
response := SuccessResponse{
Success: true,
Data: data,
}
_ = json.NewEncoder(w).Encode(response)
}
// RespondError sends an error JSON response
func RespondError(w http.ResponseWriter, statusCode int, code, message string, details map[string]interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
response := ErrorResponse{
Success: false,
Error: Error{
Code: code,
Message: message,
Details: details,
},
}
_ = json.NewEncoder(w).Encode(response)
}
// HealthData represents health check data
type HealthData struct {
Status string `json:"status"`
Timestamp time.Time `json:"timestamp"`
Version string `json:"version"`
}