2018-03-06 03:23:57 +00:00
|
|
|
package commons
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SendRESTResponse(ctx *fasthttp.RequestCtx, response interface{}) {
|
2018-03-07 12:04:28 +00:00
|
|
|
ctx.SetContentType("application/json")
|
2018-03-06 03:23:57 +00:00
|
|
|
|
2018-03-14 09:45:07 +00:00
|
|
|
var body []byte
|
|
|
|
|
|
|
|
switch response.(type) {
|
|
|
|
default:
|
|
|
|
body, _ = json.Marshal(response)
|
|
|
|
case string:
|
|
|
|
body = []byte(response.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetBody(body)
|
2018-03-06 03:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SendRESTError(ctx *fasthttp.RequestCtx, statusCode int, err error) {
|
2018-03-07 12:04:28 +00:00
|
|
|
ctx.SetContentType("application/json")
|
2018-03-06 03:23:57 +00:00
|
|
|
ctx.SetStatusCode(statusCode)
|
|
|
|
|
|
|
|
jRes, _ := json.Marshal(err)
|
|
|
|
ctx.SetBody(jRes)
|
|
|
|
}
|