shiori/internal/http/response/shortcuts.go
Felipe Martin cc7c75116d
refactor: migrate bookmark static pages to new http server (#775)
* migrate bookmark content route to new http server

* new archive page

* remove unused go generate comment

* database mock

* utils cleanup

* unused var

* domains refactor and tests

* fixed secret key type

* redirect to login on ui errors

* fixed archive folder with storage domain

* webroot documentation

* some bookmark route tests

* fixed error in bookmark domain for non existant bookmarks

* centralice errors

* add coverage data to unittests

* added tests, refactor storage to use afero

* removed mock to avoid increasing complexity

* using deps to copy files around

* remove config usage (to deps)

* remove handler-ui file
2023-12-28 18:18:32 +01:00

43 lines
1.4 KiB
Go

package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
const internalServerErrorMessage = "Internal server error, please contact an administrator"
// New provides a shortcut to a successful response object
func New(ok bool, statusCode int, data interface{}) *Response {
return NewResponse(ok, data, nil, statusCode)
}
// Send provides a shortcut to send a (potentially) successful response
func Send(ctx *gin.Context, statusCode int, data interface{}) {
New(true, statusCode, data).Send(ctx)
}
// SendError provides a shortcut to send an unsuccessful response
func SendError(ctx *gin.Context, statusCode int, data interface{}) {
New(false, statusCode, data).Send(ctx)
}
// SendErrorWithParams the same as above but for errors that require error parameters
func SendErrorWithParams(ctx *gin.Context, statusCode int, data interface{}, errorParams map[string]string) {
NewResponse(false, data, errorParams, statusCode).Send(ctx)
}
// SendInternalServerError directly sends an internal server error response
func SendInternalServerError(ctx *gin.Context) {
SendError(ctx, http.StatusInternalServerError, internalServerErrorMessage)
}
// SendNotFound directly sends a not found response
func RedirectToLogin(ctx *gin.Context, dst string) {
ctx.Redirect(http.StatusFound, "/login?dst="+dst)
}
func NotFound(ctx *gin.Context) {
ctx.AbortWithStatus(http.StatusNotFound)
}