-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_helper.go
74 lines (62 loc) · 1.57 KB
/
server_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package serverhelper
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/danenmao/pterergate-dtf/dtf/errordef"
)
// claim
type CommonMessage struct {
UserName string `json:"username"`
BodyHash string `json:"bodyhash"`
}
// Request
//
// {
// "RequestHeader":{},
// "RequestBody":{}
// }
type CommonRequest struct {
Header RequestHeader `json:"RequestHeader"`
Body string `json:"RequestBody"`
}
type RequestHeader struct {
RequestId string `json:"RequestId"`
Version string `json:"Version"`
BodyHash string `json:"Sign"`
Timestamp string `json:"Timestamp"`
Module string `json:"Module"`
Action string `json:"Action"`
}
// Response
//
// {
// "ResponseHeader":{},
// "ResponseBody":{}
// }
type CommonResponse struct {
Header ResponseHeader `json:"ResponseHeader"`
Body string `json:"ResponseBody"`
}
type ResponseHeader struct {
RequestId string `json:"RequestId"`
Code string `json:"Code"`
Message string `json:"Message"`
}
type IRequest interface{}
type IResponse interface{}
func ReturnErrorResponse(requestId string, code string, message string) IResponse {
return CommonResponse{
Header: ResponseHeader{
RequestId: requestId,
Code: code,
Message: message,
},
}
}
func returnErrorResponse(c *gin.Context, requestId string, code string, msg string) {
response := ReturnErrorResponse(requestId, code, msg)
c.JSON(http.StatusOK, response)
}
func returnInternalErrorResponse(c *gin.Context, requestId string) {
returnErrorResponse(c, requestId, errordef.Error_Msg_InternalError, "internal error occurred")
}