Files
mocksnow/server/handler/fallback.go
Nathan Coad 8112cd08c4
All checks were successful
continuous-integration/drone/push Build is passing
return proper json
2025-03-24 17:46:25 +11:00

42 lines
1.1 KiB
Go

package handler
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func (h *Handler) Fallback(w http.ResponseWriter, r *http.Request) {
h.Logger.Debug("Fallback Request received", "method", r.Method, "path", r.URL.Path)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
defer r.Body.Close()
// Pretty-print JSON to console
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, body, "", " "); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Invalid JSON received. Visit /about for more info."),
})
return
}
h.Logger.Debug("Received JSON payload", "payload", prettyJSON.String())
//fmt.Println("Received JSON payload:")
//fmt.Println(prettyJSON.String())
// Set content type and write back the same JSON
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(prettyJSON.Bytes())
}