2020-11-15 05:51:18 +13:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-02-15 02:40:17 +13:00
|
|
|
"encoding/json"
|
2020-11-15 05:51:18 +13:00
|
|
|
"fmt"
|
2021-02-15 02:40:17 +13:00
|
|
|
"io"
|
2020-11-15 05:51:18 +13:00
|
|
|
"net/http"
|
2020-11-15 06:27:20 +13:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-15 05:51:18 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type ErrResponse struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpJsonRequest(w http.ResponseWriter, r *http.Request, res interface{}) bool {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(res); err != nil {
|
2020-12-28 09:07:09 +13:00
|
|
|
if err == io.EOF {
|
2021-04-25 07:37:43 +12:00
|
|
|
HttpBadRequest(w, "no data provided")
|
2020-12-28 09:07:09 +13:00
|
|
|
} else {
|
|
|
|
HttpBadRequest(w, err)
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:51:18 +13:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpJsonResponse(w http.ResponseWriter, status int, res interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
2020-11-15 06:27:20 +13:00
|
|
|
log.Warn().Err(err).
|
|
|
|
Str("module", "http").
|
|
|
|
Msg("failed writing json error response")
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpError(w http.ResponseWriter, status int, res interface{}) {
|
|
|
|
HttpJsonResponse(w, status, &ErrResponse{
|
|
|
|
Message: fmt.Sprint(res),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpSuccess(w http.ResponseWriter, res ...interface{}) {
|
|
|
|
if len(res) == 0 {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
} else {
|
|
|
|
HttpJsonResponse(w, http.StatusOK, res[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpBadRequest(w http.ResponseWriter, res ...interface{}) {
|
2021-04-25 07:37:43 +12:00
|
|
|
defHttpError(w, http.StatusBadRequest, "bad request", res...)
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
|
2020-11-29 03:00:21 +13:00
|
|
|
func HttpUnauthorized(w http.ResponseWriter, res ...interface{}) {
|
2021-04-25 07:37:43 +12:00
|
|
|
defHttpError(w, http.StatusUnauthorized, "invalid or missing access token", res...)
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
|
2020-11-29 03:00:21 +13:00
|
|
|
func HttpForbidden(w http.ResponseWriter, res ...interface{}) {
|
2021-04-25 07:37:43 +12:00
|
|
|
defHttpError(w, http.StatusForbidden, "access token does not have the required scope", res...)
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func HttpNotFound(w http.ResponseWriter, res ...interface{}) {
|
2021-04-25 07:37:43 +12:00
|
|
|
defHttpError(w, http.StatusNotFound, "resource not found", res...)
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func HttpUnprocessableEntity(w http.ResponseWriter, res ...interface{}) {
|
2021-04-25 07:37:43 +12:00
|
|
|
defHttpError(w, http.StatusUnprocessableEntity, "unprocessable entity", res...)
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
|
2020-11-29 03:00:21 +13:00
|
|
|
func HttpInternalServerError(w http.ResponseWriter, res ...interface{}) {
|
2021-04-25 07:37:43 +12:00
|
|
|
defHttpError(w, http.StatusInternalServerError, "internal server error", res...)
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func defHttpError(w http.ResponseWriter, status int, text string, res ...interface{}) {
|
|
|
|
if len(res) == 0 {
|
|
|
|
HttpError(w, status, text)
|
|
|
|
} else {
|
|
|
|
HttpError(w, status, res[0])
|
|
|
|
}
|
|
|
|
}
|