mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
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 {
|
|
if err == io.EOF {
|
|
HttpBadRequest(w, "No data provided.")
|
|
} else {
|
|
HttpBadRequest(w, err)
|
|
}
|
|
|
|
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 {
|
|
log.Warn().Err(err).
|
|
Str("module", "http").
|
|
Msg("failed writing json error response")
|
|
}
|
|
}
|
|
|
|
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{}) {
|
|
defHttpError(w, http.StatusBadRequest, "Bad Request.", res...)
|
|
}
|
|
|
|
func HttpUnauthorized(w http.ResponseWriter, res ...interface{}) {
|
|
defHttpError(w, http.StatusUnauthorized, "Invalid or missing access token.", res...)
|
|
}
|
|
|
|
func HttpForbidden(w http.ResponseWriter, res ...interface{}) {
|
|
defHttpError(w, http.StatusForbidden, "Access token does not have the required scope.", res...)
|
|
}
|
|
|
|
func HttpNotFound(w http.ResponseWriter, res ...interface{}) {
|
|
defHttpError(w, http.StatusNotFound, "Resource not found.", res...)
|
|
}
|
|
|
|
func HttpUnprocessableEntity(w http.ResponseWriter, res ...interface{}) {
|
|
defHttpError(w, http.StatusUnprocessableEntity, "Unprocessable Entity.", res...)
|
|
}
|
|
|
|
func HttpInternalServerError(w http.ResponseWriter, res ...interface{}) {
|
|
defHttpError(w, http.StatusInternalServerError, "Internal server error.", res...)
|
|
}
|
|
|
|
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])
|
|
}
|
|
}
|