overflow_server_app/commons/response.go
crusader 920f9f8c30 ing
2018-03-14 18:45:07 +09:00

31 lines
552 B
Go

package commons
import (
"encoding/json"
"github.com/valyala/fasthttp"
)
func SendRESTResponse(ctx *fasthttp.RequestCtx, response interface{}) {
ctx.SetContentType("application/json")
var body []byte
switch response.(type) {
default:
body, _ = json.Marshal(response)
case string:
body = []byte(response.(string))
}
ctx.SetBody(body)
}
func SendRESTError(ctx *fasthttp.RequestCtx, statusCode int, err error) {
ctx.SetContentType("application/json")
ctx.SetStatusCode(statusCode)
jRes, _ := json.Marshal(err)
ctx.SetBody(jRes)
}