slideshow-api/internal/api/response.go

37 lines
736 B
Go

package api
import (
"encoding/json"
"net/http"
"git.velvettear.de/velvettear/loggo"
)
// struct for server responses
type response struct {
StatusCode int
Content string
error error
}
// send a response
func (response *response) send(writer http.ResponseWriter) {
if response.StatusCode <= 0 {
if response.error != nil {
response.StatusCode = 500
} else {
response.StatusCode = 200
}
}
if response.error != nil {
response.Content = response.error.Error()
}
data, error := json.Marshal(response)
if error != nil {
loggo.Error("encountered an error marshalling a json response", error.Error())
response.StatusCode = 500
return
}
writer.WriteHeader(response.StatusCode)
writer.Write(data)
}