modules-durpify/respond/handlers.go

166 lines
3.4 KiB
Go
Raw Permalink Normal View History

2026-03-18 05:33:42 -05:00
package respond
2024-09-02 13:38:46 -05:00
import (
"encoding/json"
"errors"
"fmt"
"net/http"
2025-07-05 07:40:12 -05:00
"github.com/a-h/templ"
"github.com/charmbracelet/log"
"gopkg.in/yaml.v3"
2024-09-02 13:38:46 -05:00
)
type BasicMessage struct {
Message string `json:"message"`
}
type StandardMessage struct {
Message interface{}
Status int `json:"status"`
}
type StandardError struct {
Message string `json:"message"`
Status int `json:"status"`
Description []string `json:"description"`
}
func (e StandardError) Error() string {
return fmt.Sprintf("Api error: %d", e.Status)
}
type Response interface {
2025-07-08 05:57:01 -05:00
SendResponse(w http.ResponseWriter, r *http.Request)
2024-09-02 13:38:46 -05:00
Test(http.Handler)
}
2026-03-18 05:33:42 -05:00
func (message *StandardMessage) SendReponse(
w http.ResponseWriter,
r *http.Request,
) {
2024-09-02 13:38:46 -05:00
setHeader(&w, message.Status)
2025-07-08 05:57:01 -05:00
contentType := r.Header.Get("Content-Type")
2025-07-05 07:40:12 -05:00
switch contentType {
case "application/yaml":
err := yaml.NewEncoder(w).Encode(message.Message)
if err != nil {
log.Error("Failed to Encode YAML", "error", err)
return
}
2025-07-08 05:57:01 -05:00
default:
2025-07-05 07:40:12 -05:00
2025-07-08 05:57:01 -05:00
err := json.NewEncoder(w).Encode(message.Message)
if err != nil {
log.Error("Failed to Encode")
return
}
2024-09-02 13:38:46 -05:00
}
}
2026-03-18 05:33:42 -05:00
func (message *StandardError) SendReponse(
w http.ResponseWriter,
r *http.Request,
) {
2024-09-02 13:38:46 -05:00
setHeader(&w, message.Status)
2025-07-08 05:57:01 -05:00
contentType := r.Header.Get("Content-Type")
switch contentType {
case "application/yaml":
err := yaml.NewEncoder(w).Encode(message.Message)
if err != nil {
log.Error("Failed to Encode YAML", "error", err)
return
}
default:
err := json.NewEncoder(w).Encode(message.Message)
if err != nil {
log.Error("Failed to Encode")
return
}
2024-09-02 13:38:46 -05:00
}
}
2024-09-28 08:27:53 -05:00
// NewFailureResponse returns a new instance of StandardError with the given
// message, status code and description.
func NewFailureResponse(
message string,
status int,
description []string,
) StandardError {
2024-09-02 13:38:46 -05:00
return StandardError{
Message: message,
Status: status,
Description: description,
}
}
2024-09-28 08:27:53 -05:00
// NewMessageResponse returns a new instance of StandardMessage with the given
// message and status code.
2024-09-02 13:38:46 -05:00
func NewMessageResponse(message interface{}, status int) *StandardMessage {
return &StandardMessage{
Message: message,
Status: status,
}
}
2024-09-28 08:27:53 -05:00
// NewBasicResponse returns a new basic instance of StandardMessage with the
// message of OK and Status OK.
2024-09-02 13:38:46 -05:00
func NewBasicResponse() *StandardMessage {
return &StandardMessage{
Message: BasicMessage{
Message: "OK",
},
Status: http.StatusOK,
}
}
// SetHeader sets the HTTP response headers for a JSON response.
func setHeader(w *http.ResponseWriter, statusCode int) {
(*w).WriteHeader(statusCode)
(*w).Header().Set("Content-Type", "application/json")
}
2024-09-28 08:27:53 -05:00
type APIFunc func(
w http.ResponseWriter,
r *http.Request,
2025-07-05 07:40:12 -05:00
) (interface{}, error)
2024-09-02 13:38:46 -05:00
func Make(handler APIFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
resp, err := handler(w, r)
if err != nil {
var apiErr StandardError
if errors.As(err, &apiErr) {
2025-07-08 05:57:01 -05:00
apiErr.SendReponse(w, r)
2024-09-02 13:38:46 -05:00
return
}
resp := NewFailureResponse(
"Internal Server Error",
http.StatusInternalServerError,
[]string{err.Error()},
)
2025-07-08 05:57:01 -05:00
resp.SendReponse(w, r)
2025-07-05 07:40:12 -05:00
return
}
message, ok := resp.(*StandardMessage)
if ok {
2025-07-08 05:57:01 -05:00
message.SendReponse(w, r)
2025-07-05 07:40:12 -05:00
return
}
templMessage, ok := resp.(templ.Component)
if ok {
templMessage.Render(r.Context(), w)
return
2024-09-02 13:38:46 -05:00
}
2025-07-05 07:40:12 -05:00
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal Server Error"))
2024-09-02 13:38:46 -05:00
}
}