picgo/corelib/response.go

34 lines
754 B
Go
Raw Normal View History

2024-07-11 21:25:58 +08:00
package corelib
import (
"encoding/json"
"net/http"
)
// JsonResponse 统一的JSON响应结构体
type JsonResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data any `json:"data,omitempty"` // 使用 any 类型来存储任意类型的数据
}
// WriteJsonResponse 用于写入JSON响应
func WriteJsonResponse(w http.ResponseWriter, status int, message string, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
2024-07-11 21:25:58 +08:00
response := JsonResponse{
Status: status,
Message: message,
Data: data,
}
err := json.NewEncoder(w).Encode(response)
if err != nil {
panic(err)
return
}
}
func Success(w http.ResponseWriter) {
WriteJsonResponse(w, 200, "OK", nil)
}