github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/http/response/shortcuts.go (about) 1 package response 2 3 import ( 4 "net/http" 5 6 "github.com/gin-gonic/gin" 7 ) 8 9 const internalServerErrorMessage = "Internal server error, please contact an administrator" 10 11 // New provides a shortcut to a successful response object 12 func New(ok bool, statusCode int, data interface{}) *Response { 13 return NewResponse(ok, data, nil, statusCode) 14 } 15 16 // Send provides a shortcut to send a (potentially) successful response 17 func Send(ctx *gin.Context, statusCode int, data interface{}) { 18 New(true, statusCode, data).Send(ctx) 19 } 20 21 // SendError provides a shortcut to send an unsuccessful response 22 func SendError(ctx *gin.Context, statusCode int, data interface{}) { 23 New(false, statusCode, data).Send(ctx) 24 ctx.Abort() 25 } 26 27 // SendErrorWithParams the same as above but for errors that require error parameters 28 func SendErrorWithParams(ctx *gin.Context, statusCode int, data interface{}, errorParams map[string]string) { 29 NewResponse(false, data, errorParams, statusCode).Send(ctx) 30 } 31 32 // SendInternalServerError directly sends an internal server error response 33 func SendInternalServerError(ctx *gin.Context) { 34 SendError(ctx, http.StatusInternalServerError, internalServerErrorMessage) 35 } 36 37 // SendNotFound directly sends a not found response 38 func RedirectToLogin(ctx *gin.Context, dst string) { 39 ctx.Redirect(http.StatusFound, "/login?dst="+dst) 40 } 41 42 func NotFound(ctx *gin.Context) { 43 ctx.AbortWithStatus(http.StatusNotFound) 44 }