feat: add json response
parent
0f740a700a
commit
6fde850d35
10
net.go
10
net.go
|
@ -1,6 +1,7 @@
|
|||
package ksrv
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -35,6 +36,15 @@ func Response(w http.ResponseWriter, status int, body []byte) (int, error) {
|
|||
return w.Write(body)
|
||||
}
|
||||
|
||||
func JSON(w http.ResponseWriter, value interface{}) {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// Abort shorthand for aborting with error, strings and status code could also be passed
|
||||
func Abort(w http.ResponseWriter, errs ...interface{}) (int, error) {
|
||||
code := 500
|
||||
|
|
Loading…
Reference in New Issue