Add helper response function, remove unused func
This commit is contained in:
parent
fa2929994f
commit
ae9492c1ff
|
@ -4,9 +4,9 @@ import (
|
|||
json2 "encoding/json"
|
||||
"fmt"
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/vysiondev/tytanium/global"
|
||||
"github.com/vysiondev/tytanium/logger"
|
||||
"log"
|
||||
"tytanium/global"
|
||||
"tytanium/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -14,6 +14,20 @@ const (
|
|||
jsonContentType = "application/json"
|
||||
)
|
||||
|
||||
type RequestStatus int
|
||||
|
||||
const (
|
||||
RequestStatusOK = iota
|
||||
RequestStatusError
|
||||
RequestStatusInternalError
|
||||
)
|
||||
|
||||
type JSONResponse struct {
|
||||
Status RequestStatus `json:"status"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// SendTextResponse sends a plaintext response to the client along with an HTTP status code.
|
||||
func SendTextResponse(ctx *fasthttp.RequestCtx, msg string, code int) {
|
||||
ctx.Response.Header.SetContentType(plainTextContentType)
|
||||
|
@ -35,8 +49,9 @@ func SendTextResponse(ctx *fasthttp.RequestCtx, msg string, code int) {
|
|||
}
|
||||
|
||||
// SendJSONResponse sends a JSON encoded response to the client along with an HTTP status code of 200 OK.
|
||||
func SendJSONResponse(ctx *fasthttp.RequestCtx, json interface{}) {
|
||||
func SendJSONResponse(ctx *fasthttp.RequestCtx, json interface{}, statusCode int) {
|
||||
ctx.SetContentType(jsonContentType)
|
||||
ctx.SetStatusCode(statusCode)
|
||||
e := json2.NewEncoder(ctx.Response.BodyWriter()).Encode(json)
|
||||
if e != nil {
|
||||
if global.Configuration.Logging.Enabled {
|
||||
|
@ -44,11 +59,12 @@ func SendJSONResponse(ctx *fasthttp.RequestCtx, json interface{}) {
|
|||
}
|
||||
log.Printf(fmt.Sprintf("JSON failed to send! %v", e))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// SendNothing sends 204 No Content.
|
||||
// TODO: remove in the future if not needed.
|
||||
func SendNothing(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusNoContent)
|
||||
func SendInvalidEncryptionKeyResponse(ctx *fasthttp.RequestCtx) {
|
||||
SendJSONResponse(ctx, JSONResponse{
|
||||
Status: RequestStatusError,
|
||||
Data: nil,
|
||||
Message: "Encryption key is invalid, or file contents have been modified.",
|
||||
}, fasthttp.StatusOK)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue