feat(api): implement new api handler

This commit is contained in:
nils måsén 2023-10-21 19:35:41 +02:00
parent 72e437f173
commit 47091761a5
17 changed files with 571 additions and 294 deletions

View file

@ -0,0 +1,41 @@
package prelude
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"net/http"
)
type Response struct {
Body any
Status int
Raw bool
}
func (r *Response) Bytes() ([]byte, error) {
if bytes, raw := r.Body.([]byte); raw {
return bytes, nil
}
if str, raw := r.Body.(string); raw {
return []byte(str), nil
}
return json.MarshalIndent(r.Body, "", " ")
}
var localLog = log.WithField("notify", "no")
func OK(body any) Response {
return Response{
Status: http.StatusOK,
Body: body,
}
}
func Error(err errorResponse) Response {
return Response{
Status: err.Status,
Body: err,
}
}