ocdp v1
This commit is contained in:
35
backend/internal/adapter/input/http/rest/utils.go
Normal file
35
backend/internal/adapter/input/http/rest/utils.go
Normal file
@ -0,0 +1,35 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/ocdp/cluster-service/internal/adapter/input/http/dto"
|
||||
)
|
||||
|
||||
// respondJSON 返回 JSON 响应
|
||||
func respondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
// respondError 返回错误响应
|
||||
func respondError(w http.ResponseWriter, statusCode int, error string, message string) {
|
||||
response := &dto.ErrorResponse{
|
||||
Error: error,
|
||||
Message: message,
|
||||
Code: statusCode,
|
||||
}
|
||||
respondJSON(w, statusCode, response)
|
||||
}
|
||||
|
||||
// respondSuccess 返回成功响应
|
||||
func respondSuccess(w http.ResponseWriter, message string, data interface{}) {
|
||||
response := &dto.SuccessResponse{
|
||||
Message: message,
|
||||
Data: data,
|
||||
}
|
||||
respondJSON(w, http.StatusOK, response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user