Files
MosisService/portal/internal/api/handlers/handlers.go

36 lines
1020 B
Go

// Package handlers contains HTTP request handlers
package handlers
import (
"encoding/json"
"net/http"
)
// ErrorResponse represents an API error response
type ErrorResponse struct {
Error string `json:"error"`
Message string `json:"message,omitempty"`
}
// NotImplemented returns a 501 Not Implemented response
func NotImplemented(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotImplemented)
json.NewEncoder(w).Encode(ErrorResponse{
Error: "not_implemented",
Message: "This endpoint is not yet implemented",
})
}
// JSON writes a JSON response with the given status code
func JSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(data)
}
// Error writes a JSON error response
func Error(w http.ResponseWriter, status int, err string, message string) {
JSON(w, status, ErrorResponse{Error: err, Message: message})
}