imc-vibe/backend/internal/api/handlers/response.go
2026-03-21 22:41:23 +01:00

41 lines
947 B
Go

package handlers
import (
"encoding/json"
"net/http"
)
type Response struct {
Data interface{} `json:"data"`
Error string `json:"error,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}
type Meta struct {
Total int `json:"total,omitempty"`
Page int `json:"page,omitempty"`
}
func JSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(Response{Data: data})
}
func Error(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(Response{Error: message})
}
func Success(w http.ResponseWriter, data interface{}) {
JSON(w, http.StatusOK, data)
}
func Created(w http.ResponseWriter, data interface{}) {
JSON(w, http.StatusCreated, data)
}
func NoContent(w http.ResponseWriter) {
w.WriteHeader(http.StatusNoContent)
}