Files
open-im-server/pkg/apiresp/http.go
T

26 lines
544 B
Go
Raw Normal View History

2023-06-11 14:36:25 +08:00
package apiresp
import (
"encoding/json"
"net/http"
)
2023-06-11 14:48:30 +08:00
func httpJson(w http.ResponseWriter, data any) {
body, err := json.Marshal(data)
2023-06-11 14:36:25 +08:00
if err != nil {
2023-06-11 14:48:30 +08:00
http.Error(w, "json marshal error: "+err.Error(), http.StatusInternalServerError)
return
2023-06-11 14:36:25 +08:00
}
2023-06-11 14:48:30 +08:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
}
2023-06-11 14:36:25 +08:00
2023-06-11 14:48:30 +08:00
func HttpError(w http.ResponseWriter, err error) {
httpJson(w, ParseError(err))
2023-06-11 14:36:25 +08:00
}
func HttpSuccess(w http.ResponseWriter, data any) {
2023-06-11 14:48:30 +08:00
httpJson(w, ApiSuccess(data))
2023-06-11 14:36:25 +08:00
}