mirror of
https://gitlab.durp.info/durfy/modules/durpify.git
synced 2026-05-07 08:00:30 -05:00
update to handle application/yaml
This commit is contained in:
parent
77b7401fd7
commit
c59d09e4c9
1 changed files with 29 additions and 16 deletions
|
|
@ -31,14 +31,14 @@ func (e StandardError) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response interface {
|
type Response interface {
|
||||||
SendResponse(w http.ResponseWriter)
|
SendResponse(w http.ResponseWriter, r *http.Request)
|
||||||
Test(http.Handler)
|
Test(http.Handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (message *StandardMessage) SendReponse(w http.ResponseWriter) {
|
func (message *StandardMessage) SendReponse(w http.ResponseWriter, r *http.Request) {
|
||||||
setHeader(&w, message.Status)
|
setHeader(&w, message.Status)
|
||||||
|
|
||||||
contentType := w.Header().Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
switch contentType {
|
switch contentType {
|
||||||
case "application/yaml":
|
case "application/yaml":
|
||||||
|
|
||||||
|
|
@ -47,22 +47,35 @@ func (message *StandardMessage) SendReponse(w http.ResponseWriter) {
|
||||||
log.Error("Failed to Encode YAML", "error", err)
|
log.Error("Failed to Encode YAML", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
default:
|
||||||
|
|
||||||
// Write the message to the response body.
|
err := json.NewEncoder(w).Encode(message.Message)
|
||||||
err := json.NewEncoder(w).Encode(message.Message)
|
if err != nil {
|
||||||
if err != nil {
|
log.Error("Failed to Encode")
|
||||||
log.Error("Failed to Encode")
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (message *StandardError) SendReponse(w http.ResponseWriter) {
|
func (message *StandardError) SendReponse(w http.ResponseWriter, r *http.Request) {
|
||||||
setHeader(&w, message.Status)
|
setHeader(&w, message.Status)
|
||||||
|
|
||||||
// Write the message to the response body.
|
contentType := r.Header.Get("Content-Type")
|
||||||
err := json.NewEncoder(w).Encode(message)
|
switch contentType {
|
||||||
if err != nil {
|
case "application/yaml":
|
||||||
log.Error("Failed to Encode")
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,7 +130,7 @@ func Make(handler APIFunc) http.HandlerFunc {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var apiErr StandardError
|
var apiErr StandardError
|
||||||
if errors.As(err, &apiErr) {
|
if errors.As(err, &apiErr) {
|
||||||
apiErr.SendReponse(w)
|
apiErr.SendReponse(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp := NewFailureResponse(
|
resp := NewFailureResponse(
|
||||||
|
|
@ -125,13 +138,13 @@ func Make(handler APIFunc) http.HandlerFunc {
|
||||||
http.StatusInternalServerError,
|
http.StatusInternalServerError,
|
||||||
[]string{err.Error()},
|
[]string{err.Error()},
|
||||||
)
|
)
|
||||||
resp.SendReponse(w)
|
resp.SendReponse(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
message, ok := resp.(*StandardMessage)
|
message, ok := resp.(*StandardMessage)
|
||||||
if ok {
|
if ok {
|
||||||
message.SendReponse(w)
|
message.SendReponse(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue