Files
gb-cms/http_response.go
2024-11-09 10:22:37 +08:00

44 lines
899 B
Go

package main
import (
"encoding/json"
"net/http"
)
type Response[T any] struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data T `json:"data"`
}
func httpResponse(w http.ResponseWriter, code int, msg string) {
httpResponseJson(w, MalformedRequest{
Code: code,
Msg: msg,
})
}
func httpResponseJson(w http.ResponseWriter, payload interface{}) {
body, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT")
w.Write(body)
}
func httpResponseOK(w http.ResponseWriter, data interface{}) {
httpResponseJson(w, MalformedRequest{
Code: http.StatusOK,
Msg: "ok",
Data: data,
})
}
func httpResponseError(w http.ResponseWriter, msg string) {
httpResponseJson(w, MalformedRequest{
Code: -1,
Msg: msg,
Data: nil,
})
}