package api import ( "encoding/json" "net/http" ) // Response represents a standard API response type Response struct { Success bool `json:"success"` Data interface{} `json:"data,omitempty"` Error string `json:"error,omitempty"` } // JSON sends a JSON response func JSON(w http.ResponseWriter, status int, data interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) response := Response{ Success: status >= 200 && status < 300, Data: data, } json.NewEncoder(w).Encode(response) } // Error sends an error response func Error(w http.ResponseWriter, status int, message string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) response := Response{ Success: false, Error: message, } json.NewEncoder(w).Encode(response) }