mirror of
https://github.com/m1k1o/neko.git
synced 2025-06-07 21:32:44 +02:00
http refactor.
This commit is contained in:
parent
4fa11e6a2a
commit
5a7cdd31fe
6 changed files with 300 additions and 138 deletions
|
@ -9,18 +9,18 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
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).WithInternalErr(err).Msg("no data provided")
|
||||
} else {
|
||||
HttpBadRequest(w).WithInternalErr(err).Msg("unable to parse provided data")
|
||||
}
|
||||
func HttpJsonRequest(w http.ResponseWriter, r *http.Request, res interface{}) error {
|
||||
err := json.NewDecoder(r.Body).Decode(res)
|
||||
|
||||
return false
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return true
|
||||
if err == io.EOF {
|
||||
return HttpBadRequest("no data provided").WithInternalErr(err)
|
||||
}
|
||||
|
||||
return HttpBadRequest("unable to parse provided data").WithInternalErr(err)
|
||||
}
|
||||
|
||||
func HttpJsonResponse(w http.ResponseWriter, code int, res interface{}) {
|
||||
|
@ -32,12 +32,14 @@ func HttpJsonResponse(w http.ResponseWriter, code int, res interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func HttpSuccess(w http.ResponseWriter, res ...interface{}) {
|
||||
func HttpSuccess(w http.ResponseWriter, res ...interface{}) error {
|
||||
if len(res) == 0 {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
} else {
|
||||
HttpJsonResponse(w, http.StatusOK, res[0])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HTTPError is an error with a message and an HTTP status code.
|
||||
|
@ -47,8 +49,6 @@ type HTTPError struct {
|
|||
|
||||
InternalErr error `json:"-"`
|
||||
InternalMsg string `json:"-"`
|
||||
|
||||
w http.ResponseWriter `json:"-"`
|
||||
}
|
||||
|
||||
func (e *HTTPError) Error() string {
|
||||
|
@ -84,64 +84,50 @@ func (e *HTTPError) WithInternalMsgf(fmtStr string, args ...interface{}) *HTTPEr
|
|||
}
|
||||
|
||||
// Sends error with custom formated message
|
||||
func (e *HTTPError) Msgf(fmtSt string, args ...interface{}) {
|
||||
func (e *HTTPError) Msgf(fmtSt string, args ...interface{}) *HTTPError {
|
||||
e.Message = fmt.Sprintf(fmtSt, args...)
|
||||
e.Send()
|
||||
return e
|
||||
}
|
||||
|
||||
// Sends error with custom message
|
||||
func (e *HTTPError) Msg(str string) {
|
||||
func (e *HTTPError) Msg(str string) *HTTPError {
|
||||
e.Message = str
|
||||
e.Send()
|
||||
return e
|
||||
}
|
||||
|
||||
// Sends error with default status text
|
||||
func (e *HTTPError) Send() {
|
||||
if e.Message == "" {
|
||||
e.Message = http.StatusText(e.Code)
|
||||
func HttpError(code int, res ...string) *HTTPError {
|
||||
err := &HTTPError{
|
||||
Code: code,
|
||||
Message: http.StatusText(code),
|
||||
}
|
||||
|
||||
logger := log.Error().
|
||||
Err(e.InternalErr).
|
||||
Str("module", "http").
|
||||
Int("code", e.Code)
|
||||
|
||||
message := e.Message
|
||||
if e.InternalMsg != "" {
|
||||
message = e.InternalMsg
|
||||
if len(res) == 1 {
|
||||
err.Message = res[0]
|
||||
}
|
||||
|
||||
logger.Msg(message)
|
||||
HttpJsonResponse(e.w, e.Code, e)
|
||||
return err
|
||||
}
|
||||
|
||||
func HttpError(w http.ResponseWriter, code int) *HTTPError {
|
||||
return &HTTPError{
|
||||
Code: code,
|
||||
w: w,
|
||||
}
|
||||
func HttpBadRequest(res ...string) *HTTPError {
|
||||
return HttpError(http.StatusBadRequest, res...)
|
||||
}
|
||||
|
||||
func HttpBadRequest(w http.ResponseWriter) *HTTPError {
|
||||
return HttpError(w, http.StatusBadRequest)
|
||||
func HttpUnauthorized(res ...string) *HTTPError {
|
||||
return HttpError(http.StatusUnauthorized, res...)
|
||||
}
|
||||
|
||||
func HttpUnauthorized(w http.ResponseWriter) *HTTPError {
|
||||
return HttpError(w, http.StatusUnauthorized)
|
||||
func HttpForbidden(res ...string) *HTTPError {
|
||||
return HttpError(http.StatusForbidden, res...)
|
||||
}
|
||||
|
||||
func HttpForbidden(w http.ResponseWriter) *HTTPError {
|
||||
return HttpError(w, http.StatusForbidden)
|
||||
func HttpNotFound(res ...string) *HTTPError {
|
||||
return HttpError(http.StatusNotFound, res...)
|
||||
}
|
||||
|
||||
func HttpNotFound(w http.ResponseWriter) *HTTPError {
|
||||
return HttpError(w, http.StatusNotFound)
|
||||
func HttpUnprocessableEntity(res ...string) *HTTPError {
|
||||
return HttpError(http.StatusUnprocessableEntity, res...)
|
||||
}
|
||||
|
||||
func HttpUnprocessableEntity(w http.ResponseWriter) *HTTPError {
|
||||
return HttpError(w, http.StatusUnprocessableEntity)
|
||||
}
|
||||
|
||||
func HttpInternalServerError(w http.ResponseWriter, err error) *HTTPError {
|
||||
return HttpError(w, http.StatusInternalServerError).WithInternalErr(err)
|
||||
func HttpInternalServerError(res ...string) *HTTPError {
|
||||
return HttpError(http.StatusInternalServerError, res...)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue