Some checks failed
Deploy / deploy (push) Has been cancelled
Reviewed-on: http://gitea/marcusDenslow/terminalShop/pulls/1
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// SuccessResponse represents a successful API response
|
|
type SuccessResponse struct {
|
|
Success bool `json:"success"`
|
|
Data any `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]any `json:"details,omitempty"`
|
|
}
|
|
|
|
// RespondSuccess sends a successful JSON response
|
|
func RespondSuccess(w http.ResponseWriter, statusCode int, data any) {
|
|
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]any) {
|
|
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"`
|
|
}
|