2023-11-29 16:00:35 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.velvettear.de/velvettear/loggo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// struct for server responses
|
|
|
|
type response struct {
|
|
|
|
StatusCode int
|
2023-11-30 14:16:39 +01:00
|
|
|
Content string
|
2023-11-29 16:00:35 +01:00
|
|
|
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)
|
|
|
|
}
|