Initial commit
This commit is contained in:
32
server/internal/handler/common.go
Normal file
32
server/internal/handler/common.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func SendSuccess(w http.ResponseWriter, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
func SendError(w http.ResponseWriter, httpStatus int, bizCode int, message string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(httpStatus)
|
||||
json.NewEncoder(w).Encode(Response{
|
||||
Code: bizCode,
|
||||
Message: message,
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user